#!/usr/bin/perl

# 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 CGI qw ( -utf8 );
use Try::Tiny;
use C4::Members;
use C4::Auth qw( get_template_and_user );
use C4::Output qw( output_html_with_http_headers );

use Koha::DateUtils qw( dt_from_string );
use Koha::CurbsidePickups;
use Koha::CurbsidePickupPolicies;
use Koha::Libraries;
use Koha::Patrons;

my $input = CGI->new;
my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
    {
        template_name   => "opac-curbside-pickups.tt",
        query           => $input,
        type            => "opac",
    }
);

unless( C4::Context->preference('CurbsidePickup') ) {
    print $input->redirect("/cgi-bin/koha/errors/404.pl");
    exit;
}

my $op = $input->param('op') || 'list';

my $logged_in_patron = Koha::Patrons->find($borrowernumber);
my $branchcode = $input->param('pickup_branch');
my @messages;

if ( $op eq 'create-pickup' ) {
    my $scheduled_pickup_datetime = $input->param('pickup_time');
    my $notes                     = $input->param('notes');

    try {
        Koha::CurbsidePickup->new(
            {
                branchcode                => $branchcode,
                borrowernumber            => $borrowernumber,
                scheduled_pickup_datetime => dt_from_string($scheduled_pickup_datetime),
                notes                     => $notes,
            }
        )->store();
    } catch {
        if ( $_->isa('Koha::Exceptions::CurbsidePickup::TooManyPickups') ) {
            push @messages, {
                type   => 'error',
                code   => 'too_many_pickups',
                patron => Koha::Patrons->find($borrowernumber)
            };
        } else {
            warn $_;
            push @messages, {
                type   => 'error',
                code   => 'something_wrong_happened',
            };
        }
    }
        # $self->_notify_new_pickup($curbside_pickup); TODO
}
elsif ( $op eq 'cancel-pickup' ) {
    my $id              = $input->param('pickup_id');
    my $curbside_pickup = Koha::CurbsidePickups->search(
        { borrowernumber => $borrowernumber } )->find($id);
    $curbside_pickup->delete
      if $curbside_pickup
      && !$curbside_pickup->delivered_datetime;
}
elsif ( $op eq 'arrival-alert' ) {
    my $id              = $input->param('pickup_id');
    my $curbside_pickup = Koha::CurbsidePickups->search(
        { borrowernumber => $borrowernumber } )->find($id);
    $curbside_pickup->mark_patron_has_arrived if $curbside_pickup;
    push @messages, {
        type => 'message',
        code => 'library_notified',
    }
}

$template->param(
    policies => Koha::CurbsidePickupPolicies->search(
        {
            enabled                 => 1,
            patron_scheduled_pickup => 1,
        }
    ),
    patron_curbside_pickups => Koha::CurbsidePickups->search(
        {
            borrowernumber            => $logged_in_patron->borrowernumber,
            scheduled_pickup_datetime => { '>' => \'DATE(NOW())' },
        },
        {
            order_by => { -asc => 'scheduled_pickup_datetime' }
        }
    ),
    curbside_pickups => Koha::CurbsidePickups->search(
        {
            scheduled_pickup_datetime => { '>' => \'DATE(NOW())' },
        },
        {
            order_by => { -asc => 'scheduled_pickup_datetime' }
        }
    ),
);

output_html_with_http_headers $input, $cookie, $template->output, undef,
  { force_no_caching => 1 };
