package BookPub::Import::Importer::Amazon::Version66;

use strict;
use warnings;

use lib '/app/tools/bookpub/lib';
use parent 'BookPub::Import::Importer::Amazon::Version64';

sub _fieldMap {
    {
        dateBegin                => 'A',
        dateEnd                  => 'A',
        rPurchaseType            => 'L',
        purchaseType             => 'L',
        rProductType             => 'I',
        rAuthor                  => 'C',
        rTitle                   => 'B',
        rIsbn13                  => 'E',
        rImprint                 => 'D',
        countryCode              => 'J',
        rClientProductID         => 'H',
        serviceProductID         => 'G',
        rReturns                 => 'W',
        rSales                   => 'V',
        rUnits                   => 'X',
        rUnitPrice               => 'P',
        rListPrice               => 'M',
        rListPriceCurrency       => 'O',
        rNativeListPrice         => 'M',
        rNativeListPriceCurrency => 'O',
        rCOGS                    => 'K',
        rDiscount                => 'U',
        rRevenue                 => 'Q',
        currencyCode             => 'AA',
        rFee                     => 'S',
        rChannel                 => 'J',
        rComments                => 'U',
    }
}

sub _unverifiedFieldMap {
    {
        _unit_fees_currency => 'T',
    }
}

sub isFree {
    my $self = shift;

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

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 rProductType { shift->_getByFieldName('rProductType') }

sub productType {
    my $self = shift;

    my $type = $self->_getByFieldName('rProductType') || '';
    if ( $type =~ /^Paperback|Hardcover$/i ) {
        return BookPub::DB::Item::Sale::kProductTypePhysicalBook;
    }

    $self->fail("Unable to determine productType from '$type'.");
}

sub dateBegin {
    my $self = shift;

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

    # Match format: Jan 5, 2023 or January 5, 2023
    if ( my ($m, $d, $y) = $dateBegin =~ /^(\w{3,9})\s+(\d{1,2}),(\d{4})$/ ) {
        my $m3 = lc(substr($m, 0, 3));  # Use the first 3 letters, lowercase
        if (exists $months{$m3}) {
            return sprintf "%04d-%02d-%02d", $y, $months{$m3}, $d;
        }
    }

    return $dateBegin;
}

sub dateEnd { shift->dateBegin }

sub rFee {
    my $self= shift;

    my $rFee = $self->_getByFieldName('rFee') || 0;

    my $unitFeesCurrency      = $self->_getByFieldName('_unit_fees_currency') || '';
    my $royaltyAmountCurrency = $self->_getByFieldName('currencyCode')        || '';
    if ( $unitFeesCurrency ne $royaltyAmountCurrency ) {
        $self->fail("Unit fees currency differs from royalty amount currency");
    }

    return $rFee;
}

sub rComments {
    my $self = shift;

    my $rComments = $self->_getByFieldName('rComments') || 0;
    $rComments =~ s/[^0-9.]+//g;

    return $rComments;
}

sub rDiscount {
    my $self = shift;

    my $rDiscount = $self->_getByFieldName('rDiscount');
    # if not BBUS or BBUK
    return $rDiscount unless Common::RSApp::GetClientID() =~ /^(?:314|361|624)$/;

    $rDiscount = 1 - $self->rCOGS;
    return $rDiscount;
}


1;