package BookPub::Import::Importer::Cepiec::v1;

use strict;
use warnings;

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

sub _fieldMap {
    {
        rAuthor            => 'C',
        rTitle             => 'B',
        rIsbn13            => 'A',
        rInstitution       => 'I',
        countryCode        => 'J',
        rUnits             => 'G',
        rListPrice         => 'F',
        rListPriceCurrency => 'E',
        rRevenue           => 'H',
        currencyCode       => 'E',
    };
}

sub _unverifiedFieldMap {{}}

sub _headerIdentifier { rTitle => 'Title' }

sub _useIsSaleRec      { 1 }
sub purchaseType       { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType        { BookPub::DB::Item::Sale::kProductTypeEBook }
sub rPurchaseType      { $_[0]->purchaseType() }
sub rProductType       { $_[0]->productType() }
sub rPriceType         { $_[0]->priceType() }
sub rListPriceCurrency { $_[0]->currencyCode() }
sub priceType          { 'rrp' }
sub rServiceName       { 'CEPIEC' }

sub dateBegin {
    my $self = shift;

    if ( $self->filename =~ /Bloomsbury.Iresearchbook.Sales.Report.(\d{4}).(\d{2}).(?:usa|uk)/i ) {
        return sprintf( "%04d-%02d-%02d", $1, $2, 1);
    }

    $self->fail("Couldn't find date from filename: '".$self->filename."'");
}

sub dateEnd { shift->dateBegin() }

sub isFree {
    my $self = shift;

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

sub _isSaleRec {
    my $self = shift;

    return 0 if !$self->rIsbn13 && !$self->rTitle;

    return 1;
}


1;
