package BookPub::Import::Importer::Heni::v1;

use strict;
use warnings;

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

sub _fieldMap {
    {
        rServiceName       => 'F2',
        dateBegin          => 'A24',
        rTitle             => 'B',
        rIsbn13            => 'A',
        rUnitPrice         => 'F',
        rRevenue           => 'F',
        currencyCode       => 'E',
    };
}

sub _unverifiedFieldMap {{}}

sub _headerIdentifier { rTitle => 'Title' }

sub _useIsSaleRec      { 1 }
sub purchaseType       { BookPub::DB::Item::Sale::kPurchaseTypeSubscriptionDL }
sub productType        { BookPub::DB::Item::Sale::kProductTypeEBook }
sub rPurchaseType      { $_[0]->purchaseType() }
sub rProductType       { $_[0]->productType() }
sub rPriceType         { $_[0]->priceType() }
sub rListPriceCurrency { $_[0]->currencyCode() }
sub rUnits             { 1 }
sub priceType          { 'rrp' }
sub currencyCode       { 'GBP' }
sub countryCode        { 'US' }

my %months = (
    january   => 1,
    february  => 2,
    march     => 3,
    april     => 4,
    may       => 5,
    june      => 6,
    july      => 7,
    august    => 8,
    september => 9,
    october   => 10,
    november  => 11,
    december  => 12,
);

sub dateBegin {
    my $self = shift;

    my $date = $self->_getByFieldName("dateBegin") || "";
    if ( $date =~ /Self-billing invoice for the period \d+[A-z]{2} to \d+[A-z]{2} (\w+?) (\d{4})/i ) {
        return sprintf( "%04d-%02d-%02d", $2, $months{lc($1)}, 1);
    }

    $self->fail("Cannot determine date from '$date'");
}

sub dateEnd { shift->dateBegin() }

sub isFree {
    my $self = shift;

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

sub _isSaleRec {
    my $self = shift;
    return $self->rTitle && $self->rIsbn13;
}

1;
