package Koha::REST::V1::Acquisitions::Orders;

# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.

use Modern::Perl;

use Mojo::Base 'Mojolicious::Controller';

use Koha::Acquisition::Orders;
use Koha::DateUtils;

use Clone 'clone';
use JSON qw(decode_json);
use Scalar::Util qw( blessed );
use Try::Tiny;

=head1 NAME

Koha::REST::V1::Acquisitions::Orders

=head1 API

=head2 Methods

=head3 list

Controller function that handles listing Koha::Acquisition::Order objects

=cut

sub list {

    my $c = shift->openapi->valid_input or return;

    return try {

        my $only_active = delete $c->validation->output->{only_active};
        my $order_id    = delete $c->validation->output->{order_id};

        my $orders_rs;

        if ( $only_active ) {
            $orders_rs = Koha::Acquisition::Orders->filter_by_active;
        }
        else {
            $orders_rs = Koha::Acquisition::Orders->new;
        }

        $orders_rs = $orders_rs->filter_by_id_including_transfers({ ordernumber => $order_id })
            if $order_id;

        my $args = $c->validation->output;
        my $attributes = {};

        # Extract reserved params
        my ( $filtered_params, $reserved_params, $path_params ) = $c->extract_reserved_params($args);
        # Look for embeds
        my $embed = $c->stash('koha.embed');
        my $fixed_embed = clone($embed);
        if ( exists $fixed_embed->{biblio} ) {
            # Add biblioitems to prefetch
            # FIXME remove if we merge biblio + biblioitems
            $fixed_embed->{biblio}->{children}->{biblioitem} = {};
            $c->stash('koha.embed', $fixed_embed);
        }

        # If no pagination parameters are passed, default
        $reserved_params->{_per_page} //= C4::Context->preference('RESTdefaultPageSize');
        $reserved_params->{_page}     //= 1;

        unless ( $reserved_params->{_per_page} == -1 ) {
            # Merge pagination into query attributes
            $c->dbic_merge_pagination(
                {
                    filter => $attributes,
                    params => $reserved_params
                }
            );
        }

        # Generate prefetches for embedded stuff
        $c->dbic_merge_prefetch(
            {
                attributes => $attributes,
                result_set => $orders_rs
            }
        );

        # Call the to_model function by reference, if defined
        if ( defined $filtered_params ) {

            # Apply the mapping function to the passed params
            $filtered_params = $orders_rs->attributes_from_api($filtered_params);
            $filtered_params = $c->build_query_params( $filtered_params, $reserved_params );
        }

        if ( defined $path_params ) {

            # Apply the mapping function to the passed params
            $filtered_params //= {};
            $path_params = $orders_rs->attributes_from_api($path_params);
            foreach my $param (keys %{$path_params}) {
                $filtered_params->{$param} = $path_params->{$param};
            }
        }

        if ( defined $reserved_params->{q} || defined $reserved_params->{query} || defined $reserved_params->{'x-koha-query'}) {
            $filtered_params //={};
            my @query_params_array;
            my $query_params;
            if ( exists $reserved_params->{query} and defined $reserved_params->{query} ) {
                push @query_params_array, fix_query({ query => $reserved_params->{query} });
            }
            if ( exists $reserved_params->{q} and defined $reserved_params->{q}) {
                push @query_params_array, fix_query({ query => decode_json($reserved_params->{q}) });
            }
            if ( exists $reserved_params->{'x-koha-query'} and defined $reserved_params->{'x-koha-query'} ) {
                push @query_params_array, fix_query({ query => decode_json($reserved_params->{'x-koha-query'}) });;
            }

            if(scalar(@query_params_array) > 1) {
                $query_params = {'-and' => \@query_params_array};
            }
            else {
                $query_params = $query_params_array[0];
            }

            $filtered_params = $c->merge_q_params( $filtered_params, $query_params, $orders_rs );
        }

        # Perform search
        my $orders = $orders_rs->search( $filtered_params, $attributes );

        if ($orders->is_paged) {
            $c->add_pagination_headers({
                total => $orders->pager->total_entries,
                params => $args,
            });
        }
        else {
            $c->add_pagination_headers({
                total => $orders->count,
                params => $args,
            });
        }

        return $c->render(
            status  => 200,
            openapi => $orders->to_api({ embed => $embed })
        );
    }
    catch {
        $c->unhandled_exception($_);
    };
}

=head3 get

Controller function that handles retrieving a single Koha::Acquisition::Order object

=cut

sub get {
    my $c = shift->openapi->valid_input or return;

    my $order = Koha::Acquisition::Orders->find( $c->validation->param('order_id') );

    unless ($order) {
        return $c->render(
            status  => 404,
            openapi => { error => "Order not found" }
        );
    }

    return try {
        my $embed = $c->stash('koha.embed');

        return $c->render(
            status  => 200,
            openapi => $order->to_api({ embed => $embed })
        );
    }
    catch {
        $c->unhandled_exception($_);
    };
}

=head3 add

Controller function that handles adding a new Koha::Acquisition::Order object

=cut

sub add {
    my $c = shift->openapi->valid_input or return;

    return try {
        my $order = Koha::Acquisition::Order->new_from_api( $c->validation->param('body') );
        $order->store->discard_changes;

        $c->res->headers->location(
            $c->req->url->to_string . '/' . $order->ordernumber
        );

        return $c->render(
            status  => 201,
            openapi => $order->to_api
        );
    }
    catch {
        if ( blessed $_ and $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
            return $c->render(
                status  => 409,
                openapi => { error => $_->error, conflict => $_->duplicate_id }
            );
        }

        $c->unhandled_exception($_);
    };
}

=head3 update

Controller function that handles updating a Koha::Acquisition::Order object

=cut

sub update {
    my $c = shift->openapi->valid_input or return;

    my $order = Koha::Acquisition::Orders->find( $c->validation->param('order_id') );

    unless ($order) {
        return $c->render(
            status  => 404,
            openapi => { error => "Order not found" }
        );
    }

    return try {
        $order->set_from_api( $c->validation->param('body') );
        $order->store()->discard_changes;

        return $c->render(
            status  => 200,
            openapi => $order->to_api
        );
    }
    catch {
        $c->unhandled_exception($_);
    };
}

=head3 delete

Controller function that handles deleting a Koha::Patron object

=cut

sub delete {
    my $c = shift->openapi->valid_input or return;

    my $order = Koha::Acquisition::Orders->find( $c->validation->param('order_id') );

    unless ($order) {
        return $c->render(
            status  => 404,
            openapi => { error => 'Order not found' }
        );
    }

    return try {

        $order->delete;

        return $c->render(
            status  => 204,
            openapi => q{}
        );
    }
    catch {
        $c->unhandled_exception($_);
    };
}

=head2 Internal methods

=head3 fix_query

    my $query = fix_query($query);

This method takes care of recursively fixing queries that should be done
against biblioitems (instead if biblio as exposed on the API)

=cut

sub fix_query {
    my ($args) = @_;

    my $query = $args->{query};
    my $biblioitem_fields = {
        'biblio.isbn' => 'biblio.biblioitem.isbn',
        'biblio.ean'  => 'biblio.biblioitem.ean'
    };

    if ( ref($query) eq 'HASH' ) {
        foreach my $key (keys %{$query}) {
            if ( exists $biblioitem_fields->{$key}) {
                my $subq = delete $query->{$key};
                $query->{$biblioitem_fields->{$key}} = (ref($subq) eq 'HASH')
                        ? fix_query({ query => $subq })
                        : $subq;
            }
            else {
                $query->{$key} = fix_query({ query => $query->{$key} });
            }
        }
    }
    elsif ( ref($query) eq 'ARRAY' ) {
        my @accum;
        foreach my $item (@{$query}) {
            push @accum, fix_query({ query => $item });
        }
        $query = \@accum;
    }
    else { # scalar
        $query = $biblioitem_fields->{$query}
            if exists $biblioitem_fields->{$query};
    }

    return $query;
}

1;
