package RPS::Import::Caroline::v36;

use strict;
use warnings;

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

use Date::Calc qw(Days_in_Month Decode_Month);

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

sub physical { 2 }

sub _fieldMapExtended {
    {
        physical => {
            option1 => {
                labelName      => 'C',
                dateBegin      => 'A',
                productType    => 'I',
                artistName     => 'D',
                upc            => 'E',
                albumName      => 'F',
                units          => 'S',
                totalRevenue   => 'P',
                mediaType      => 'I',
            },
            option2 => {
                labelName      => 'C',
                dateBegin      => 'A',
                productType    => 'I',
                artistName     => 'D',
                upc            => 'E',
                albumName      => 'F',
                units          => 'S',
                totalRevenue   => 'P',
                mediaType      => 'I',
            },
        },
    };
}

sub _unverifiedFieldMapExtended {
    {
        physical => {
            option1 => {
                _netUnits           => 'S',
                _discountFee        => 'K',
            },
            option2 => {
                _netUnits           => 'S',
                _discountFee        => 'M',
            },
        }
    };
}

sub _headerIdentifierExtended {
    {
        physical => {
            option1 => {
                units => 'TOTAL NET QUANTITY',
            },
            option2 => {
                units => 'Net Quantity - Third',
            },
        },
    };
}

sub channel    { RPS::File::Sale::CHANNEL_RETAIL }
sub priceLevel { RPS::File::Sale::PLEVEL_UNKNOWN }

my %months = (
    jan => 1,
    feb => 2,
    mar => 3,
    apr => 4,
    may => 5,
    jun => 6,
    jul => 7,
    aug => 8,
    sep => 9,
    oct => 10,
    nov => 11,
    dec => 12,
);

sub saleDates {
    my $self = shift;

    my $date = lc( $self->_getByFieldName('dateBegin') || '' );

    # FEB 2021
    if ( $date =~ /^(\w{3}).(\d{4})/ ) {
        my $year = $2;
        my $month = $months{$1} if exists $months{$1};
        my $dateBegin = sprintf "%04d-%02d-%02d", $year, $month, 1;
        my $dateEnd   = sprintf "%04d-%02d-%02d", $year, $month, Days_in_Month( $year, $month );
        return ( $dateBegin, $dateEnd );
    }

    $self->fail("Unable to determine date begin from '$date'");
}

sub productType {
    my $self = shift;

    my $type = $self->_getByFieldName('productType') || '';
    return RPS::File::Sale::TYPE_LP if $type =~ /^vinyl (?:single|album) 12\" 33 rpm$/i;
    return RPS::File::Sale::TYPE_CD if $type =~ /^cd album$/i;
    return RPS::File::Sale::TYPE_DVD_CD_SET if $type =~ /^CD Album plus DVD Video$/i;

    $self->fail("Unable to determine product type from '$type'");
}

sub countryCode {
    my $self = shift;

    return 'US'  if ( $self->filename =~ /[_ ]US/i );
    return 'CA'  if ( $self->filename =~ /[_ ]C(?:A|N|CANADA)/i );
}

sub currencyCode {
    my $self = shift;

    return 'USD'  if ( $self->filename =~ /[_ ]US/i );
    return 'CAD'  if ( $self->filename =~ /[_ ]C(?:A|N|ANADA)/i );
}

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

sub _isValidSaleRecord {
    my $self = shift;

    my $artistName = $self->_getByFieldName('artistName') || '';
    my $dateBegin = $self->_getByFieldName('dateBegin') || '';
    return if $artistName =~ /Result/i;
    return if $dateBegin =~ /Result/i;

    $self->fail("Found revenue without units. ") if ($dateBegin !~ /fiscal year/i && $self->totalRevenue && !$self->units);
    return ($self->totalRevenue && $self->units);
}

sub mediaType {
    my $self = shift;

    my $type = $self->_getByFieldName('mediaType') || '';

    return RPS::DB::Item::MediaType::kMediaTypeVideo if ($type =~ /dvd|video/i);
    return RPS::DB::Item::MediaType::kMediaTypeAudio if ($type =~ /vinyl|cd/i);

    $self->fail("Unable to determine product type from '$type'");
}

1;
