package BookPub::Import::Importer::Kobo::Version23;

use strict;

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

sub _headerIdentifier { ( isbn => 'eISBN' ) }

# We have to do some odd things with the field map here because this importer has
# different column definitions of seperate tabs.  The map is defined by tab name below.
# The summary tab is ignored (returns and empty field set).
sub _fieldMap {
    my $self = shift;

    my $map = {
        23 => {
            'sales' => {
                currencyCode           => 'T',
                conversionRate         => 'R',
                countryCode            => 'C',
                rState                 => 'D',
                rPostalCode            => 'E',
                dateBegin              => 'A',
                isbn                   => 'J',
                rTitle                 => 'L',
                rAuthor                => 'K',
                rImprint               => 'I',
                rSales                 => 'F',
                rReturns               => 'G',
                rListPrice             => 'N',
                rListPriceCurrency     => 'Q',
                rPurchasePrice         => 'M',
                rPurchasePriceCurrency => 'Q',
                rCOGS                  => 'O',
                rRevenue               => 'S',
            },
        },
        33 => {
            'sales' => {
                currencyCode           => 'U',
                conversionRate         => 'S',
                countryCode            => 'C',
                rState                 => 'D',
                rPostalCode            => 'E',
                dateBegin              => 'A',
                isbn                   => 'K',
                rTitle                 => 'M',
                rAuthor                => 'L',
                rImprint               => 'J',
                rUnits                 => 'H',
                rSales                 => 'F',
                rReturns               => 'G',
                rListPrice             => 'O',
                rListPriceCurrency     => 'R',
                rPurchasePrice         => 'N',
                rPurchasePriceCurrency => 'R',
                rCOGS                  => 'P',
                rRevenue               => 'T',
            },
        },
    };

    my $sheet = lc( $self->sheetname ) || return {};
    return {} if ( $sheet =~ /(invoice|summary)/i );

    die "Unsupported tab name '" . $sheet . "' or version '" . $self->_version() . "'"
      unless ( $map->{ $self->_version() }{$sheet} );

    return $map->{ $self->_version() }{$sheet};
}

sub purchaseType { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType  { BookPub::DB::Item::Sale::kProductTypeEBook }

sub _dateFormat { [ 'iso', 'excel', 'text', 'eur' ] }

sub priceType {
    my $self     = shift;
    my $filename = $self->filename();
    if (   $filename =~ /wiley/i
        || $filename =~ /hachette uk/i ) {
        return 'agency';
    } else {
        return 'rrp';
    }
}

sub rTaxUnitPrice {
    my $self = shift;

    my $rTaxUnitPrice = $self->rPurchasePrice - $self->rListPrice;

    return $rTaxUnitPrice;
}

1;
