package BookPub::Import::Importer::Audible;

use strict;

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

use lib '/app/tools/sale_import/lib/';
use Import::ValidationError;

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

sub _headerIdentifier { ( serviceProductID => 'Parent Product Id' ) }

# map version num to the field order
sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        6 => {
            dateBegin        => 'T',
            serviceProductID => 'D',
            countryCode      => 'C',
            isbn             => 'G',
            rTitle           => 'E',
            rAuthor          => 'F',
            rUnits           => 'Q',
            rDiscount        => 'B',
            rRevenue         => 'S',
        },
        7 => {
            serviceProductID => 'C',
            countryCode      => 'B',
            dateBegin        => 'F2',
            isbn             => 'G',
            rTitle           => 'D',
            rAuthor          => 'E',
            rUnits           => 'S',
            rDiscount        => 'H',
            rRevenue         => 'U',
        },
        8 => {
            serviceProductID => 'C',
            countryCode      => 'B',
            dateBegin        => 'B4',
            isbn             => 'F',
            rTitle           => 'D',
            rAuthor          => 'E',
            rUnits           => 'P',
            rListPrice       => 'G',
            rRevenue         => 'Q',
        },
        9 => {
            serviceProductID => 'C',
            countryCode      => 'B',
            dateBegin        => 'F2',
            isbn             => 'G',
            rTitle           => 'D',
            rAuthor          => 'E',
            rUnits           => 'T',
            rDiscount        => 'H',
            rRevenue         => 'V',
        },
        10 => {
            dateBegin        => 'B1',
            rAuthor          => 'D',
            rTitle           => 'C',
            rIsbn13          => 'E',
            countryCode      => 'G',
            rClientProductID => 'F',
            serviceProductID => 'B',
            rUnits           => 'S',
            rCOGS            => 'I',
            rRevenue         => 'U',
        },
    );

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

sub purchaseType               { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub priceType                  { 'rrp' }
sub currencyCode               { 'USD' }
sub productType                { BookPub::DB::Item::Sale::kProductTypeAudioBook }
sub _dateNormalization         { 'month' }
sub _useIsSaleRec              { 1 }
sub _autoParseTitleAndSubtitle { 1 }

sub _isSaleRec {
    my $self = shift;

    # Ignore refunds with 0 revenue for Hachette only.
    return if ( !$self->rRevenue() && $self->rUnits() < 0 && Common::RSApp::GetClientID == 308 );

    # skip over some spurious blank lines in the file FB10152
    return if ( $self->rUnits() eq "" );

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

1;
