package BookPub::Import::Importer::1000Cookbooks::v1;

use strict;
use warnings;

use lib '/app/tools/common/lib';
use Common::Country;

use lib '/app/tools/bookpub/lib';
use base 'BookPub::Import::Importer';


sub _fieldMap {
    {
        rServiceName       => 'O1',
        dateBegin          => 'A1',
        dateEnd            => 'A1',
        rAuthor            => 'D',
        rTitle             => 'C',
        rIsbn13            => 'B',
        rReturns           => 'K',
        rSales             => 'J',
        rListPrice         => 'F',
        rListPriceCurrency => 'H',
        rDiscount          => 'M',
        currencyCode       => 'H',
        rTaxAmount         => 'I',
    };
}

sub _unverifiedFieldMap {
    {
        territoryCode       => 'A',
        salePriceWithoutTax => 'G',
    }
}

sub _headerIdentifier { rTitle => 'Title' }

sub _useIsSaleRec { 1 }
sub priceType     { 'rrp' }
sub purchaseType  { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub rProductType  { BookPub::DB::Item::Sale::kProductTypeEBook }
sub productType   { BookPub::DB::Item::Sale::kProductTypeEBook }

my %months = (
    jan => 1, feb => 2, mar => 3, apr => 4, may => 5, jun => 6,
    jul => 7, aug => 8, sep => 9, oct => 10, nov => 11, dec => 12
);

sub dateBegin {
    my $self = shift;

    my $date = $self->_getByFieldName('dateBegin') || '';

    # Monthly Sales Report for 01 Feb 2023 to 28 Feb 2023
    my ($d, $m, $y) = $date =~ /(\d{1,2}) (\w{3}) (\d{4}) to/i;
    if ( exists $months{lc $m} ) {
        $m = $months{lc $m};
        return sprintf "%04d-%02d-%02d", $y, $m, $d;
    }

    $self->fail("Unable to determine date begin from: $date");
}

sub dateEnd {
    my $self = shift;

    my $date = $self->_getByFieldName('dateEnd') || '';

    # Monthly Sales Report for 01 Feb 2023 to 28 Feb 2023
    my ($d, $m, $y) = $date =~ /to (\d{1,2}) (\w{3}) (\d{4})/i;
    if ( exists $months{lc $m} ) {
        $m = $months{lc $m};
        return sprintf "%04d-%02d-%02d", $y, $m, $d;
    }

    $self->fail("Unable to determine date end from: $date");
}

sub isFree {
    my $self = shift;

    return !$self->rRevenue && $self->rUnits ? 'Y' : 'N';
}

sub rUnits {
    my $self = shift;

    # since column L contains a formula, we will calculate it as J - K
    return $self->rSales - $self->rReturns;
}

sub countryCode {
    my $self = shift;

    # since column N contains a formula, we will take it from column A
    return $self->_getByFieldName('territoryCode') || '';
}

sub rDiscount {
    my $self = shift;

    my $rDiscount = $self->_getByFieldName('rDiscount') || 0;
    $rDiscount =~ s/[%\s]//g;

    return $rDiscount;
}

sub rRevenue {
    my $self = shift;

    # since column P contains a formula, we will calculate it as ROUND( G * ( 1 - M ), 2) * L
    my $salePriceWithoutTax = $self->_getByFieldName('salePriceWithoutTax') || 0;
    my $rDiscount = $self->rDiscount();
    my $rUnits = $self->rUnits();

    return sprintf("%.2f", $salePriceWithoutTax * ( 1 - $rDiscount ) ) * $rUnits;
}

sub _isSaleRec {
    my $self = shift;

    return 0 if !$self->_getByFieldName('salePriceWithoutTax') && !$self->rTitle();

    return 1;
}


1;
