package RPS::Import::Secretly::v11;

use strict;
use warnings;

use lib '/app/tools/rps/lib';
use base qw/RPS::Import::MapFaceBase RPS::Import::Secretly::v6/;

sub _fieldMap {
    {
        labelName       => 'Y',
        serviceID       => 'B',
        countryCode     => 'K',
        dateBegin       => 'U',
        productType     => 'G',
        formatType      => 'L',
        clientProductID => 'C',
        artistName      => 'H',
        upc             => 'F',
        albumName       => 'D',
        isrc            => 'G',
        trackName       => 'I',
        units           => 'M',
        price           => 'S',
    };
}

sub currencyCode { 'USD' }
sub mediaType { RPS::DB::Item::MediaType::kMediaTypeAudio }

sub productType {
    my $self = shift;
    my $track_name = $self->_getByFieldName('trackName') || "";
    my $isrc = $self->_getByFieldName('isrc');

    return RPS::File::Sale::TYPE_ALBUM if ($isrc && $track_name =~ /Full (?:Album|EP) Stream/i);
    return ( $isrc ? RPS::File::Sale::TYPE_TRACK : RPS::File::Sale::TYPE_ALBUM );
}

sub serviceID {
    my $self    = shift;
    my $service = $self->_getByFieldName('serviceID') || '';

    return Client::Service::DSP_KAMEA_MUSIC if ($service =~ /^neighboring rights income/i);  # RSD-1548 (special characters in sales file)

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

    $self->fail("Unknown service '$service'") unless ($serviceID);

    return $serviceID;
}

sub formatType {
    my $self = shift;

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

    $self->fail("Could not parse format type from '$type'") if $type =~ /^Unspecified$/i;

    # this condition should be first
    if ( $self->serviceID eq Client::Service::DSP_SOUNDEXCHANGE ) {
        return RPS::File::Sale::FORMAT_PERFORMANCE if $type =~ /^(?:Radio \(Broadcasting Statutory\))$/i;
    }

    return RPS::File::Sale::FORMAT_DOWNLOAD if $type =~ /^(?:Album|Track)$/i;
    return RPS::File::Sale::FORMAT_STREAM   if $type =~ /^(?:Cloud|Stream|not a Sale|Radio \(.+?\)|UGC Production)$/i;

    $self->fail("Could not parse format type from '$type'");
}

sub countryCode {
    my $self = shift;

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

    return $country->alpha2 if $country;

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

    return $code;
}

sub _mapFaceCountryCode { 'countryCode' }
sub _mapFaceProductType { }
sub _mapFaceServiceID   { }
sub _mapFaceFormatType  { }
sub _mapFaceMediaType   { }

1;
