package RPS::Import::Revolver::v3;

use strict;
use warnings;

use lib '/app/tools/rps/lib';
use parent 'RPS::Import::ImporterMixed';

sub physical { 2 }

sub _fieldMapExtended {
    {
        physical => {
            option1 => {
                dateBegin       => 'O',
                dateEnd         => 'P',
                productType     => 'G',
                clientProductID => 'D',
                artistName      => 'E',
                upc             => 'C',
                albumName       => 'F',
                sales           => 'H',
                totalRevenue    => 'N',
            },
         },
    };
}

sub _unverifiedFieldMapExtended {
    {
        physical => {
            option1 => {
                _returnsDefective => 'I',
                _returnsOverStock => 'J',
                _netUnits         => 'K',
            }
        }
    }
};

sub _headerIdentifierExtended {
    {
        physical => {
            option1 => {
                upc => 'Barcode',
            },
        },
    };
}

sub countryCode  { 'US' }
sub currencyCode { 'USD' }
sub channel      { RPS::File::Sale::CHANNEL_RETAIL }
sub priceLevel   { RPS::File::Sale::PLEVEL_UNKNOWN }

sub productType {
    my $self = shift;

    my $type = $self->_getByFieldName('productType') || '';
    if ( $type =~ /^(?:CD|7xCD|3xCD)$/i ) {
        return RPS::File::Sale::TYPE_CD;
    } elsif ( $type =~ /^(?:LP|10"|12"|2x10"|2xLP|3xLP|LP ?\+ ?7"|7")$/i ) {
        return RPS::File::Sale::TYPE_LP;
    } elsif ( $type =~ /^(?:2xDVD|DVD-Audio)$/i ) {
        return RPS::File::Sale::TYPE_DVD;
    } elsif ( $type =~ /^(?:MC|2xMC)$/i ) {
        return RPS::File::Sale::TYPE_CASS;
    } elsif ( $type =~ /^(?:2xCD)$/i ) {
        return RPS::File::Sale::TYPE_DBL_CD;
    } elsif ( $type =~ /^(?:CD\+DVD)$/i ) {
        return RPS::File::Sale::TYPE_DVD_CD_SET;
    }

    $self->fail("Unknown product type '$type'");
}

sub netUnits           { $_[0]->_getByFieldName('_netUnits')    || 0 }
sub totalRevenue       { $_[0]->_getByFieldName('totalRevenue') || 0 }
sub sales              { $_[0]->_getByFieldName('sales') > 0 ? $_[0]->_getByFieldName('sales') : 0 }
sub salesRevenue       { $_[0]->totalRevenue > 0 ? $_[0]->totalRevenue     : 0 }
sub returnsRevenue     { $_[0]->totalRevenue < 0 ? abs $_[0]->totalRevenue : 0 }

sub returns {
    my $self = shift;

    my $defective = abs( $self->_getByFieldName('_returnsDefective') || 0 );
    my $overstock = abs( $self->_getByFieldName('_returnsOverStock') || 0 );
    my $returns = $defective + $overstock;

    return ($returns == 0 && $self->sales == 0 && $self->netUnits < 0) ? abs($self->netUnits) : $returns;
}

sub _isValidSaleRecord {
    return ( $_[0]->netUnits && ($_[0]->_getByFieldName('artistName') || $_[0]->_getByFieldName('albumName')) )
}


1;
