package RPS::Import::Fuga::v5;

use strict;
use warnings;

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


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

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

sub physical { 0 }

sub _fieldMapExtended {
    {
        digital => {
            option1 => {
                serviceID    => 'O',
                countryCode  => 'M',
                dateBegin    => 'A',
                dateEnd      => 'B',
                upc          => 'K',
                artistName   => 'H',
                albumName    => 'D',
                isrc         => 'G',
                trackName    => 'F',
                units        => 'L',
                currencyCode => 'S',
                price        => 'R',
                mediaType    => 'N',
            },
         },
    };
}

sub _unverifiedFieldMapExtended {
    {
        digital => {
            option1 => {
                _asset_quantity   => 'V',
                _product_quantity => 'U',
                _asset_artist     => 'N',
                _product_artist   => 'L',
            },
        },
    };
}

sub _headerIdentifierExtended {
    {
        digital => {
            option1 => {
                upc => 'FUGA Asset ID',
            },
        },
    };
}

sub productType { RPS::File::Sale::TYPE_TRACK }
sub formatType { RPS::File::Sale::FORMAT_STREAM }

sub mediaType {
    my $self = shift;

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

    return RPS::DB::Item::MediaType::kMediaTypeAudio if ($type =~ /^Youtube.+/i);

    $self->fail("Media type is empty") unless $type;
}

sub serviceID {
    my $self = shift;

    my $service = lc( $self->_getByFieldName('serviceID') || '' );
    my $serviceID = $self->getServiceID($service) || $RPS::Import::ServiceMap::SERVICE_ALIASES{$service};
    return $serviceID if $serviceID;

    return $self->handleError( "Unknown service: '$service'", RPS::DB::Item::SaleImportError::kErrorService );
}


sub saleDates {
    my $self = shift;

    my $startDate = $self->_getByFieldName('dateBegin') || '';
    my $endDate   = $self->_getByFieldName('dateEnd')   || '';

    unless ( $startDate && $endDate ) {
        $self->fail("Unable to determine sale dates from: '$startDate' and '$endDate'");
    }

    my $flipMonthDay = 0;
    # MM/DD/YY[YY] - 7/1/23, 7/31/23 OR DD/MM/YY[YY]
    if ( my ($m, $d, $y) = $endDate =~ /^(\d{1,2})\/(\d{1,2})\/(\d{2,4})$/ ) {
        if ( $m > 12 ) {
            ($m, $d) = ($d, $m);
            $flipMonthDay++;
        }
        $endDate = sprintf "20%02d-%02d-%02d", substr($y, -2), $m, $d;
    }
    # MM/DD/YY[YY] - 7/1/23, 7/31/23 OR DD/MM/YY[YY]
    if ( my ($m, $d, $y) = $startDate =~ /^(\d{1,2})\/(\d{1,2})\/(\d{2,4})$/ ) {
        if ( $flipMonthDay || $m > 12 ) {
            ($m, $d) = ($d, $m);
            $flipMonthDay++;
        }

        $startDate = sprintf "20%02d-%02d-%02d", substr($y, -2), $m, $d;
    }

    return $self->_getDates( date_begin => $startDate, date_end => $endDate );
}

sub countryCode {
    my $self = shift;

    my $code = $self->_getByFieldName('countryCode') || 'blank';
    my $country = Common::Country->Get($code);
    return $country->alpha2 if $country;

    $self->handleError( "Unknown country code '$code'", RPS::DB::Item::SaleImportError::kErrorCountry );
    return $code;
}

# digital
sub price     { abs( shift->_getByFieldName('price') || 0 ) }
sub unitPrice { abs shift->SUPER::unitPrice }
sub units {
    my $self = shift;

    my $units = $self->_getByFieldName('units') || 0;
    my $price = $self->_getByFieldName('price') || 0;

    if ( $price > 0 ) {
        $units = $units ? abs($units) : 1;
    } elsif ( $price < 0 ) {
       $units = $units ? -1 * abs($units) : -1;
    }

    return $units;
}

sub _isValidSaleRecord {
    my $self = shift;

    return 1 if ($self->trackName || $self->isrc || $self->upc || $self->albumName || $self->artistName);
    return 0;
}

# mapFace
sub _mapFaceProductType { }
sub _mapFaceServiceID   { 'serviceID' }
sub _mapFaceCountryCode { 'countryCode' }
sub _mapFaceFormatType  { }
sub _mapFaceMediaType   { }

1;
