package BookPub::Import::Importer::Follett_Higher_Education::Version17;

use strict;

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

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

# map version num to the field order
sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        17 => {
            dateBegin            => 'R',
            rAuthor              => 'C',
            rTitle               => 'D',
            rIsbn13              => 'U',
            rIsbn10              => 'V',
            rInstitution         => 'W',
            rState               => 'Y',
            rPostalCode          => 'Z',
            serviceProductID     => 'K',
            rListPrice           => 'F',
            rReturns             => 'I',
            rSales               => 'I',
            rRevenue             => 'G',
            rDeliveryMethod      => 'O',
            rChannel             => 'AA',
            rTransactionCategory => 'E',
            rComments            => 'N'
        }
    );

    return $fieldMap{ $self->_version() };
}

sub _unverifiedFieldMap {
    my $self = shift;

    my %fieldMap = (
        17 => {
            transactionType => 'I',
        },
    );

    return $fieldMap{ $self->_version() };
}

sub _useIsSaleRec              { 1 }
sub _autoParseTitleAndSubtitle { 1 }
sub _dateFormat                { [ 'us', 'iso', 'numerical' ] }

sub purchaseType           { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType            { BookPub::DB::Item::Sale::kProductTypeEBook }
sub rListPriceCurrency     { 'USD' }
sub rPurchasePriceCurrency { 'USD' }
sub currencyCode           { 'USD' }

sub priceType {
    my $self = shift;
    my $type;
    my $clientID = Common::RSApp::GetClientID;

    if ( $clientID == 318 || $clientID == 310 ) {    # Macmillan and DTMVTEST
        $type = 'agency';
    } elsif ( $clientID == 346 || $clientID == 335 || $clientID == 365 ) {    # RLPG, BKP, and SAGE
        $type = 'rrp';
    }

    return $type;
}

# For this importer, we'll determine the country by the value of the state column
sub countryCode {
    my $self        = shift;
    my $rState      = $self->rState();
    my $postalObjUS = Common::Postal->new('US');
    my $postalObjCA = Common::Postal->new('CA');

    # US state abbreviation
    if ( $postalObjUS->getName($rState) ) {
        return "US";

        # CA state abbreviation
    } elsif ( $postalObjCA->getName($rState) ) {
        return "CA";
    } else {
        $self->fail("State is neither a valid US state or Canadian province: $rState");
    }
}

# There are some Canadian sales with province in the state field.  We'll
# accept those and verify they're correct
sub state {
    my $self        = shift;
    my $state       = $self->_getByFieldName('rState');
    my $postalObjUS = Common::Postal->new('US');
    my $postalObjCA = Common::Postal->new('CA');

    if ( $postalObjUS->getName($state) ) {

        # acceptable state abbreviation
        return $state;
    } elsif ( $postalObjCA->getName($state) ) {

        # acceptable province abbreviation
        return $state;
    } else {
        $self->fail("State is neither a valid US state or Canadian province: $state");
    }
    return $state;
}

sub rUnits {
    my $self    = shift;
    my $revenue = $self->_getByFieldName('rRevenue');
    my $return  = $self->rReturns();

    if ( $revenue < 0 ) {
        return -1;
    }
    if ( $revenue == 0 && $return == 1 ) {
        return -1;
    }

    return 1;
}

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

    if ( $type eq 'Refund' || $type eq 'Refund Correction' ) {
        return 1;
    }
    return;
}

sub rSales {
    my $self = shift;
    my $type = $self->_getByFieldName('rSales');

    if ( $type eq 'Sale' || $type eq 'Sale Correction' || $type eq 'Refund Reversal' ) {
        return 1;
    }
    return;
}

sub isFree {
    my $self     = shift;
    my $category = $self->_getByFieldName('rTransactionCategory');
    my $revenue  = $self->_getByFieldName('rRevenue');

    if ( $category eq 'includED instructor' && $revenue == 0 ) {
        return 'Y';
    }
    return 'N';
}

1;
