#!/usr/bin/perl -Ilibs

use strict;
use warnings;

use API::Controller;

use ECS::Product::Inventory::StockStatus;
use ECS::Site;
use ECS::SiteSetting;

package API::StockStatusAdmin;

use Moose;

extends 'API::Controller';

__PACKAGE__->meta->make_immutable;

my @SPECIAL_ORDER_FIELDS =
    qw(specialOrderCartWarning 
    specialOrderDescription 
    specialOrderShortDescription 
    specialOrderLongDescription 
    specialOrderMinimumLeadTime 
    specialOrderMaximumUnitsSold);


###############################################################################
# get the data required for the main page of the stock status admin

sub stockStatusIndex {
    my $self = (shift)->new({'input' => shift || {}});

    $self->data->{'templates'} = [ "index.html" ];

    $self->data->{'stockStatuses'} =
        ECS::Product::Inventory::StockStatus::getStockStatuses({
            'dbh'   => $self->dbhr,
            });

    my $site = ECS::Site->fromName( "ECS" );
    $self->data->{ 'siteSettings' } = ECS::SiteSetting::fetchForUpdate( $site );

    if(not defined $self->data->{'stockStatuses'}
        or ref $self->data->{'stockStatuses'} ne "HASH"
        or keys %{$self->data->{'stockStatuses'}} == 0
        ) {
        $self->error("Unable to get stock statuses");
        return $self->result;
    }

    return $self->result;
}

###############################################################################
# Update stock statuses from the form in the portal

sub updateStockStatus {
    my $self = (shift)->new({'input' => shift || {}});

    # parse the form data from
    #   description_1 = foo
    #   icon_1 = bar
    # into a hash
    #   { '1' => {
    #       'description'  => "foo",
    #       'displayClass' => "bar", 
    #       }
    my %stockStatuses = ();
    for my $field (keys %{$self->input}) {
        if($field =~ m/^description_(\d+)$/) {
            $stockStatuses{$1}->{'description'} = $self->input->{$field};
        }
        elsif($field =~ m/^shortDescription_(\d+)$/) {
            $stockStatuses{$1}->{'shortDescription'} = $self->input->{$field};
        }
        elsif($field =~ m/^longDescription_(\d+)$/) {
            $stockStatuses{$1}->{'longDescription'} = $self->input->{$field};
        }
        elsif($field =~ m/^icon_(\d+)$/) {
            $stockStatuses{$1}->{'displayClass'} = $self->input->{$field};
        }
        elsif($field =~ m/^fulfillmentType_(\d+)$/) {
            $stockStatuses{$1}->{'fulfillmentType'} = $self->input->{$field};
        }
        elsif($field =~ m/^estimatedShipDate_(\d+)$/) {
            $stockStatuses{$1}->{'estimatedShipDate'} = $self->input->{$field};
        }
    }

    my $err = ECS::Product::Inventory::StockStatus::updateStockStatuses({
        'dbh'           => $self->dbh,
        'stockStatuses' => \%stockStatuses,
        });

    if($err) {
        $self->error("Problem updating stock statuses: $err");
        return $self->result;
    }

    $self->data->{'redirect'} = ".";
    return $self->result;
}

###############################################################################

sub updateSpecialOrderWarnings {
    my $self = (shift)->new({'input' => shift || {}});

    for my $field ( @SPECIAL_ORDER_FIELDS ) {
        ECS::Site->forAll( sub {
            my ( $site ) = @_;
            ECS::SiteSetting::updateSiteSetting(
                $site,
                $field,
                $self->input->{ $field },
            );
        } );
    }

    $self->data->{ 'redirect' } = ".";
    return $self->result;
}

###############################################################################

return 1;

