package BookPub::Import::Importer::DeMarque::v1;

use strict;
use warnings;

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


sub _fieldMap {
    {
        rServiceName       => 'BO',
        dateBegin          => 'E',
        dateEnd            => 'F',
        rFormat            => 'AD',
        rPriceType         => 'AK',
        rAuthor            => 'Y',
        rTitle             => 'X',
        rIsbn13            => 'U',
        rImprint           => 'AC',
        countryCode        => 'AL',
        rClientProductID   => 'W',
        rReturns           => 'AG',
        rSales             => 'AF',
        rUnits             => 'AH',
        rUnitPrice         => 'AM',
        rListPrice         => 'BQ',
        rListPriceCurrency => 'AO',
        rDiscount          => 'AP',
        rRevenue           => 'BC',
        currencyCode       => 'I',
    };
}

sub _unverifiedFieldMap {
    {
        saleDate => 'O',
    };
}

sub _headerIdentifier { rTitle => 'Product title' }

sub productType                { BookPub::DB::Item::Sale::kProductTypeEBook }
sub purchaseType               { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub priceType                  { shift->rPriceType }
sub priceTypeQualifierID       { BookPub::DB::Item::PriceTypeQualifier::kCorporate }
sub _autoParseTitleAndSubtitle { 1 }
sub _useIsSaleRec              { 1 }

sub _getDateBegin {
    my $self = shift;

    my $reportBegin = $self->_getByFieldName('dateBegin') || '';
    if ( $reportBegin =~ /^(\d{4})(\d{2})(\d{2})$/ ) {
        return sprintf( "%04d-%02d-%02d", $1, $2, $3 );
    }

    return $self->_getByFieldName('saleDate');
}

sub _getDateEnd {
    my $self = shift;

    my $reportEnd = $self->_getByFieldName('dateEnd') || '';
    if ( $reportEnd =~ /^(\d{4})(\d{2})(\d{2})$/ ) {
        return sprintf( "%04d-%02d-%02d", $1, $2, $3 );
    }

    return $self->_getByFieldName('saleDate');
}

sub priceType {
    my $self = shift;

    my $type = lc( $self->_getByFieldName('rPriceType') || '' );
    return 'agency' if $type =~ /agency/;
    return 'rrp'    if $type =~ /wholesale/;

    $self->fail("Unexpected priceType found: $type ");
}


1;