package BookPub::Import::Importer::Amazon::Version50;

use strict;

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

sub _fieldMap {
    my $map = shift->SUPER::_fieldMap();

    $map->{rIncentiveAmount} = 'BI';

    return $map;
}

sub _unverifiedFieldMap {
    my $map = shift->SUPER::_unverifiedFieldMap();

    $map->{rIncentiveAmountCurrency} = 'BJ';

    return $map;
}

sub countryCode {
    my $self        = shift;
    my $countryCode = $self->SUPER::countryCode();

    if ( !$countryCode ) {

        # Seems like when it's no money, there are no other details as well.
        # Default to client's country
        if ( $self->rRevenue() == 0 && $self->rListPrice() == 0 && $self->rPurchasePrice() == 0 ) {
            $countryCode = Common::Client::Current()->Locale()->countryCode();
        }
    }

    return $countryCode;
}

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

    if ( !$revenue ) {
        $revenue = 0;
    }

    # Contrary to version 29, we don't want to flip revenue here. Amazon means what they put.
    # Maybe they always did?

    return $revenue;
}

sub incentiveAmount {
    my $self = shift;

    my $incentive = $self->rIncentiveAmount();

    if ( !defined($incentive) || $incentive eq '' ) {

        # Okay, so Amazon doesn't populate incentive revenue for returns. Hopefully a bug that will go away and
        # then we can get rid of this grossness that looks for revenue that's too small to be actual revenue
        # --hopefully--
        if (   uc( $self->_getByFieldName('transactionStatus') ) eq 'PREVIOUSLY_REFUNDED_RECEIVED'
            && $self->rPurchasePrice() != 0
            && $self->rRevenue() != 0 ) {
            my $pctOfPrice = abs( $self->rRevenue() / $self->rPurchasePrice() );

            if ( $pctOfPrice > 0 && $pctOfPrice < 0.11 ) {
                Log->info( "Incentive? : " . -$self->rRevenue() . " line: " . $self->linenum );

                #$incentive = -$self->rRevenue();
            }
        }
    }

    return $incentive;
}

1;
