package RPS::Import::Secretly::v6;

use strict;
use warnings;

use lib '/app/tools/rps/lib';
use lib '/app/tools/data_classes/lib';
use base 'RPS::Import::Secretly';
use RPS::Import::ServiceMap;

sub _fieldMap {
    {
        labelName       => 'W',
        serviceID       => 'B',
        dateBegin       => 'S',
        productType     => 'K',
        artistName      => 'G',
        clientProductID => 'C',
        upc             => 'E',
        isrc            => 'F',
        trackName       => 'H',
        albumName       => 'D',
        price           => 'Q',
        units           => 'L',
        countryCode     => 'J',
    };
}

sub _unverifiedFieldMap {
    {
        dsp => 'F',    # digpay cosmetic dsp
    };
}

sub _headerIdentifier { ( dateBegin => 'dateProcessedlast' ) }

sub currencyCode { 'USD' }
sub unitPrice    { abs shift->SUPER::unitPrice }

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('productType');
    my $dsp  = $self->_getByFieldName('dsp');

    if ( $type =~ /^(album|track)$/i ) {
        return RPS::File::Sale::FORMAT_DOWNLOAD;
    }
    elsif ( $type =~ /^(cloud|stream|not a sale|radio \(statutory\))$/i ) {
        return RPS::File::Sale::FORMAT_STREAM;
    }
    elsif ( $type =~ /^Radio \((Broadcasting Statutory|Direct)\)$/i ) {
        return RPS::File::Sale::FORMAT_PERFORMANCE;
    }
    else {
        $self->fail("Could not parse format type from '$type'");
    }
}


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;
}

1;
