package BookPub::Import::Importer::Chegg::Version6;

use strict;
use Data::Dumper;

use lib '/app/tools/common/lib';
use Common::Country;

use lib '/app/tools/bookpub/lib';
use BookPub::DB::Item::Sale;
use base 'BookPub::Import::Importer::Chegg::Base';

sub _fieldMap {
    {
        rPurchaseType      => 'E',
        currencyCode       => 'Q',
        countryCode        => 'V',
        rState             => 'T',
        rPostalCode        => 'U',
        dateBegin          => 'A',
        rIsbn13            => 'J',
        rTitle             => 'F',
        rAuthor            => 'I',
        rReturns           => 'P',
        rListPrice         => 'O',
        rListPriceCurrency => 'O',
        rRevenue           => 'Q',
    };
}

sub currencyCode { 'USD' }
sub countryCode  { 'US' }

sub _validTabNames { ['Detail'] }
sub _useIsSaleRec  { 1 }

sub _processHeader {
    my $self = shift;

    # Only process sales from the transaction sheets
    my $validTab;
    my $validTabNames = _validTabNames();
    foreach my $validTabName (@$validTabNames) {
        if ( $self->sheetname() =~ /$validTabName/i ) {
            $validTab = 1;
        }
    }
    return undef unless $validTab;

    if ( !$self->{_listPriceCurrencyFound} ) {

        my $field = $self->_getByFieldName('rListPriceCurrency');

        if ( $field =~ m/^list price \(([A-Z]{3})\)$/i ) {
            $self->{_listPriceCurrency}      = $1;
            $self->{_listPriceCurrencyFound} = 1;
        } else {
            $self->fail("no list price currency in field: $field");
        }

    }

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

sub rReturns {
    my $self  = shift;
    my $value = $self->_getByFieldName('rReturns');

    return lc($value) eq 'cancellation' ? 1 : 0;
}

sub rListPriceCurrency {
    my $self = shift;

    return $self->{_listPriceCurrency};
}

###
1;    # Play nicely.
###
