package NGCP::Panel::Controller::API::CallListsItem;
use NGCP::Panel::Utils::Generic qw(:all);

use Sipwise::Base;

use HTTP::Headers qw();
use HTTP::Status qw(:constants);
use NGCP::Panel::Utils::API::Calllist;

use NGCP::Panel::Utils::ValidateJSON qw();
use DateTime::TimeZone;
require Catalyst::ActionRole::ACL;
require NGCP::Panel::Role::HTTPMethods;
require Catalyst::ActionRole::RequireSSL;

sub allowed_methods{
    return [qw/GET OPTIONS HEAD/];
}

use parent qw/NGCP::Panel::Role::EntitiesItem NGCP::Panel::Role::API::CallLists/;

sub resource_name{
    return 'calllists';
}

sub dispatch_path{
    return '/api/calllists/';
}

sub relation{
    return 'http://purl.org/sipwise/ngcp-api/#rel-calllists';
}

__PACKAGE__->set_config({
    allowed_roles => [qw/admin reseller subscriberadmin subscriber/],
});

sub GET :Allow {
    my ($self, $c, $id) = @_;
    {

        my $schema = $c->model('DB');
        last unless $self->valid_id($c, $id);

        my $owner = NGCP::Panel::Utils::API::Calllist::get_owner_data($self, $c, $schema);
        last unless $owner;
        my $href_data = $owner->{subscriber} ?
            "subscriber_id=".$owner->{subscriber}->id :
            "customer_id=".$owner->{customer}->id;

        my $item = $self->item_by_id($c, $id);
        last unless $self->resource_exists($c, calllist => $item);

        my $hal = $self->hal_from_item($c, $item, $owner, undef, $href_data);

        my $response = HTTP::Response->new(HTTP_OK, undef, HTTP::Headers->new(
            (map { # XXX Data::HAL must be able to generate links with multiple relations
                s|rel="(http://purl.org/sipwise/ngcp-api/#rel-resellers)"|rel="item $1"|r =~
                s/rel=self/rel="item self"/r;
            } $hal->http_headers),
        ), $hal->as_json);
        $c->response->headers($response->headers);
        $c->response->body($response->content);
        return;
    }
    return;
}

1;

# vim: set tabstop=4 expandtab:
