package CTSMS::BulkProcessor::RestRequests::ctsms::shared::SearchService::Criterion;
use strict;

## no critic

use CTSMS::BulkProcessor::ConnectorPool qw(
    get_ctsms_restapi

);

use CTSMS::BulkProcessor::RestProcessor qw(
    copy_row
    get_query_string

);

use CTSMS::BulkProcessor::RestConnectors::CtsmsRestApi qw(_get_api);
use CTSMS::BulkProcessor::RestItem qw();


require Exporter;
our @ISA = qw(Exporter CTSMS::BulkProcessor::RestItem);
our @EXPORT_OK = qw(
    get_items
    get_items_path

);



my $default_restapi = \&get_ctsms_restapi;
my $get_items_path_query = sub {
    my ($criteria_id) = @_;
    return 'search/' . $criteria_id . '/criterions/';
};

my $fieldnames = [
    "booleanValue",
    "criteria",
    "dateValue",
    "floatValue",
    "id",
    "longValue",
    "position",
    "property",
    "restriction",
    "stringValue",
    "tie",
    "timeValue",
    "timestampValue",
];

sub new {

    my $class = shift;
    my $self = CTSMS::BulkProcessor::RestItem->new($class,$fieldnames);

    copy_row($self,shift,$fieldnames);

    return $self;

}

sub get_items {

    my ($criteria_id,$load_recursive,$restapi,$headers) = @_;
    my $api = _get_api($restapi,$default_restapi);
    return builditems_fromrows($api->get(&$get_items_path_query($criteria_id),$headers),$load_recursive,$restapi);

}

sub builditems_fromrows {

    my ($rows,$load_recursive,$restapi) = @_;

    my $item;

    if (defined $rows and ref $rows eq 'ARRAY') {
        my @items = ();
        foreach my $row (@$rows) {
            $item = __PACKAGE__->new($row);

            # transformations go here ...

            push @items,$item;
        }
        return \@items;
    } elsif (defined $rows and ref $rows eq 'HASH') {
        $item = __PACKAGE__->new($rows);
        return $item;
    }
    return undef;

}

sub get_items_path {

    my ($criteria_id) = @_;
    return &$get_items_path_query($criteria_id);

}

sub TO_JSON {

    my $self = shift;
    return { %{$self} };

}

1;
