package BookPub::Import::Importer::Amazon::Version64;

use strict;
use warnings;

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

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           => 'V',
        rSales             => 'U',
        rUnits             => 'W',
        rUnitPrice         => 'P',
        rListPrice         => 'M',
        rListPriceCurrency => 'O',
        rCOGS              => 'K',
        rRevenue           => 'X',
        currencyCode       => 'Z',
        rFee               => 'R',
        rChannel           => 'J',
    }
}

sub _headerIdentifier { rIsbn13 => 'ISBN' }

sub _useIsSaleRec { 1 }
sub priceType     { 'rrp' }
sub productType   { shift->rProductType }

sub rServiceName {
    return BookPub::Tracker::Service::GetDefaultServiceName( service_id => shift->serviceID );
}

sub purchaseType {
    my $self = shift;

    my $type = $self->_getByFieldName('purchaseType') || '';
    if ( $type =~ /^POD \(I(?:F|SP)\)$/i ) {
        return BookPub::DB::Item::Sale::kPurchaseTypePrintOnDemand;
    }

    return BookPub::DB::Item::Sale::kPurchaseTypePrintOnDemand if !$type;

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

sub rProductType {
    my $self = shift;

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

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

sub countryCode {
    my $self = shift;

    my $cc = $self->_getByFieldName('countryCode') || '';
    my ($countryCode) = $cc =~ /.*\((\w{2})\).*/;

    return $countryCode;
}

sub _isSaleRec {
    my $self = shift;

    return unless $self->rTitle && $self->rIsbn13;

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


1;