package BookPub::Import::Importer::Amazon::Version48;

use strict;

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

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

use Date::Calc qw( Add_Delta_YM );

sub _headerIdentifier { ( dateBegin => 'Factuurdatum (Invoice Date)' ) }

sub _fieldMap {
    {
        serviceProductID       => 'B',
        currencyCode           => 'X',
        dateBegin              => 'A',
        dateEnd                => 'A',
        rFormat                => 'I',
        isbn                   => 'E',
        rTitle                 => 'F',
        rAuthor                => 'G',
        rImprint               => 'H',
        rUnits                 => 'L',
        rSales                 => 'J',
        rReturns               => 'K',
        rAdjustments           => 'N',
        rListPrice             => 'Q',
        rListPriceCurrency     => 'R',
        rPurchasePrice         => 'O',
        rPurchasePriceCurrency => 'P',
        rDiscount              => 'S',
        rRevenue               => 'W',
    };
}

sub priceType          { 'agency' }
sub productType        { BookPub::DB::Item::Sale::kProductTypeEBook }
sub purchaseType       { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub _useIsSaleRec      { 1 }
sub _dateNormalization { 'month' }

sub countryCode {
    my $self = shift;

    my $filename = $self->filename();

    $self->fail("Country code missing from filename")
      unless ( $filename =~ /(?:_(\w{2})_|_(\w{2})\.)/ );

    my $cc = $1 || $2;
    my $obj = Common::Country->Get($cc);

    if ($obj) {
        if ( $obj->alpha2 eq 'UK' ) {
            return 'GB';
        } else {
            return $obj->alpha2;
        }
    } else {
        $self->fail("Invalid country code: $cc");
    }
}

sub _getDateBegin {
    my $self = shift;
    my $date = $self->_getByFieldName('dateBegin') || return;

    if ( $date && $date =~ /(\d{1,2})\/(\d{1,2})\/(\d{4})/ ) {

        my ( $year, $month, $day ) = Add_Delta_YM( $3, $2, $1, 0, -1 );

        return sprintf( "%04d-%02d-%02d", $year, $month, $day );
    }
}

sub _getDateEnd {
    my $self = shift;
    my $date = $self->_getByFieldName('dateEnd') || return;

    if ( $date && $date =~ /(\d{1,2})\/(\d{1,2})\/(\d{4})/ ) {

        my ( $year, $month, $day ) = Add_Delta_YM( $3, $2, $1, 0, -1 );

        return sprintf( "%04d-%02d-%02d", $year, $month, $day );
    }
}

sub _isSaleRec {
    my $self = shift;

    # We're going to check for the subtotal line, which is blank from column H on
    if (   !$self->currencyCode
        && !$self->rFormat
        && !$self->rImprint
        && !$self->rUnits
        && !$self->rSales
        && !$self->rReturns
        && !$self->rAdjustments
        && !$self->rListPrice
        && !$self->rListPriceCurrency
        && !$self->rPurchasePrice
        && !$self->rPurchasePriceCurrency
        && !$self->rDiscount
        && !$self->rRevenue ) {
        return 0;
    }

    return $self->SUPER::_isSaleRec();
}

1;
