package BookPub::Import::Importer::Nextory::v1;

use strict;
use warnings;

use lib '/app/tools/bookpub/lib';

use parent 'BookPub::Import::Importer';

sub _fieldMap {
    {
        dateBegin          => 'A',
        rProductType       => 'C',
        rAuthor            => 'D',
        rTitle             => 'E',
        rIsbn13            => 'B',
        countryCode        => 'H',
        rUnits             => 'J',
        rUnitPrice         => 'I',
        rListPriceCurrency => 'G',
        rRevenue           => 'K',
        currencyCode       => 'G',
    };
}

sub _unverifiedFieldMap { }
sub _headerIdentifier   { rAuthor => 'Author' }
sub _useIsSaleRec       { 1 }

sub rServiceName  { 'Nextory' }
sub purchaseType  { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub rPurchaseType { shift->purchaseType() }
sub productType   { shift->rProductType() }
sub priceType     { 'rrp' }

sub _setDates {
    my $self = shift;

    my $dateBegin = $self->_getByFieldName('dateBegin') || '';
    if ( $dateBegin =~ /^Q(\d).(\d{4})$/i ) {
       return $self->_getDates( date_begin => "$1_Q$2", type => 'quarter' );
    }

    $self->fail("Unable to determine date range from: '$dateBegin'.");
}

sub rProductType {
    my $self = shift;

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

sub rListPrice {
    my $self = shift;

    my $unitPrice = $self->_getByFieldName('rUnitPrice') || 0;
    return sprintf "%.2f", $unitPrice / 0.35;
}

sub _isSaleRec {
    my $self = shift;

    my $isbn   = $self->_getByFieldName('rIsbn13');
    my $title  = $self->_getByFieldName('rTitle');
    my $author = $self->_getByFieldName('rAuthor');

    return 0 unless $isbn && $title && $author;

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

sub discount {
    my $self = shift;

    return $self->rListPrice * 0.65;
}


1;
