package RPS::Import::Universal::v78;

use strict;
use warnings;

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

use lib '/app/tools/rps/lib';
use RPS::Import::ServiceMap;
use Date::Calc qw(Days_in_Month Add_Delta_Days Decode_Month);

use parent qw/RPS::Import::ImporterMixed/;

sub physical { 1 }

sub _fieldMapExtended {
    {
        physical => {
            option1 => {
                labelName       => 'F',
                countryCode     => 'N',
                dateBegin       => 'A',
                productType     => 'Q',
                clientProductID => 'J',
                artistName      => 'G',
                upc             => 'I',
                albumName       => 'H',
                channel         => 'O',
                priceLevel      => 'O',
                sales           => 'R',
                salesRevenue    => 'W',
                returns         => 'S',
                returnsRevenue  => 'X',
                totalRevenue    => 'Y',
            }
        }
    }
}


sub _unverifiedFieldMapExtended {
    {
        physical => {
            option1 => {
                _netUnits => 'T',
            }
        }
    }
}

sub _headerIdentifierExtended {
    {
        physical => {
            option1 => { upc => 'UPC/EAN' }
        }
    }
}

sub serviceID    { Client::Service::DSP_VIRGIN_MUSIC_GROUP }
sub currencyCode { 'USD' }

sub countryCode {
    my $self = shift;

    my $code = $self->_getByFieldName('countryCode') || '';

    my $oCountry = Common::Country->Get($code) if $code;
    return $oCountry->alpha2() if $oCountry;

    $self->fail("Unknown country code '$code'");
}

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 $dateBegin = $self->_getByFieldName('dateBegin') || '';
    # P10 25(Oct 25)
    if ( my ($m, $y) = $dateBegin =~ /^P\d+ \d+\((\w{3}) (\d{2})\)$/ ) {
        $m = $months{ lc $m } if $m;
        $y = "20$y";
        return $self->_getDates( date_begin => sprintf("%04d-%02d-%02d", $y, $m, 1) );
    }

    $self->fail("Unable to determine sale period from: '$dateBegin'");
}

sub productType {
    my $self = shift;

    my $type = $self->_getByFieldName('productType') || '';
    return RPS::File::Sale::TYPE_LP   if $type =~ /^LP$/i;
    return RPS::File::Sale::TYPE_CD   if $type =~ /^CD$/i;
    return RPS::File::Sale::TYPE_LP5  if $type =~ /^(LP5|7["']+)$/i;
    return RPS::File::Sale::TYPE_DVD  if $type =~ /^DVD$/i;
    return RPS::File::Sale::TYPE_CASS if $type =~ /^(?:Cass(ette)?|CS|MC)$/i;

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

sub channel {
    my $self = shift;

    my $channel = $self->_getByFieldName('channel') || '';
    return RPS::File::Sale::CHANNEL_RETAIL if !$channel || $channel =~ /^(?:11|89)$/;

    $self->fail("Unable to determine channel from: '$channel'");
}

sub priceLevel {
    my $self = shift;

    my $priceLevel = $self->_getByFieldName('priceLevel') || '';
    return RPS::File::Sale::PLEVEL_UNKNOWN if !$priceLevel || $priceLevel =~ /^(?:11|89)$/;

    $self->fail("Unable to determine proce level from: '$priceLevel'");
}

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

sub _isValidSaleRecord {
    my $self = shift;

    return ($self->_getByFieldName('sales') || $self->_getByFieldName('returns') ) && $self->albumName;
}


1;
