package BookPub::Import::Importer::Acrobatiq::v2;

use strict;

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

use Date::Calc qw( Add_Delta_YM );

sub _headerIdentifier { rIsbn13 => 'eISBN' }

sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        2 => {
            dateBegin        => 'C',
            rAuthor          => 'B',
            rTitle           => 'A',
            rIsbn13          => 'F',
            rInstitution     => 'M',
            rState           => 'N',
            rPostalCode      => 'O',
            countryCode      => 'P',
            rReturns         => 'E',
            rUnits           => 'D',
            rDiscount        => 'I',
            rRevenue         => 'J',
            currencyCode     => 'K',
            rChannel         => 'L',
        },
    );

    return $fieldMap{ $self->_version() };
}

sub _unverifiedFieldMap {
    my $self = shift;

    my %fieldMap = (
        2 => {
            _pricePerStudent => 'G',
        },
    );

    return $fieldMap{ $self->_version() };
}

sub _useIsSaleRec              { 1 }
sub _autoParseTitleAndSubtitle { 1 }
sub currencyCode               { return shift->_getByFieldName("currencyCode") || "USD" }
sub countryCode                { return shift->_getByFieldName("countryCode")  || "US"  }
sub productType                { BookPub::DB::Item::Sale::kProductTypeEBook }
sub rProductType               { BookPub::DB::Item::Sale::kProductTypeEBook }
sub purchaseType               { BookPub::DB::Item::Sale::kPurchaseTypeLoan }
sub priceType                  { 'rrp' }
sub rServiceName               { 'Acrobatiq' }

sub rRevenue {
    my $self = shift;

    my $_pricePerStudent = $self->_getByFieldName('_pricePerStudent');

    return $self->rUnits * $_pricePerStudent * ($self->rDiscount // 1);
}

sub isFree {
    my $self = shift;

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

sub _isSaleRec {
    my $self = shift;

    my $date   = $self->_getByFieldName('dateBegin');
    my $author = $self->_getByFieldName('rAuthor');
    my $isbn   = $self->_getByFieldName('rIsbn13');
    my $title  = $self->_getByFieldName('rTitle');

    if ( lc($title) eq 'grand total' && !$date && !$author && !$isbn ) {
        return 0;
    }

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


1;