package BookPub::Import::Importer::Kno::Version1;

use strict;

use lib '/app/tools/sale_import/lib/';
use Import::ValidationError;

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

sub _headerIdentifier { ( rTitle => 'Title' ) }

sub _fieldMap {
    {
        currencyCode => 'J',
        countryCode  => 'K',
        rState       => 'L',
        rPostalCode  => 'M',
        dateBegin    => 'C',
        isbn         => 'D',
        rTitle       => 'N',
        rAuthor      => 'O',
        rListPrice   => 'G',
        rDiscount    => 'I',
        rRevenue     => 'H',
    };
}

sub _unverifiedFieldMap {
    {
        transactionType => 'E',
        licenseType     => 'F',
    };
}

sub priceType    { 'rrp' }
sub purchaseType { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType  { BookPub::DB::Item::Sale::kProductTypeEBook }
sub rUnits       { 1 }

sub rTitle {
    my $self = shift;

    my $title = $self->SUPER::rTitle();
    if ($title) {

        # Just going to validate some other fields, to make sure we know what we're getting
        my $transactionType = $self->_getByFieldName('transactionType');
        if ( lc($transactionType) ne 'cash sale' ) {
            $self->fail("Transaction Type ($transactionType) not supported");
            return undef;
        }
        my $licenseType = $self->_getByFieldName('licenseType');
        if ( lc($licenseType) ne 'buy' ) {
            $self->fail("License Type ($licenseType) not supported");
            return undef;
        }

        return $title;
    }
}

1;
