package RPS::Import::AWAL::v6;

use strict;
use warnings;

use parent qw/RPS::Import::AWAL::v5/;

sub _fieldMapExtended {
    {
        mixed => {
            option1 => {
                labelName       => 'I',
                serviceID       => 'G',
                countryCode     => 'F',
                dateBegin       => 'E',
                dateEnd         => 'E',
                productType     => 'U',
                formatType      => 'U',
                clientProductID => 'M',
                upc             => 'N',
                albumName       => 'K',
                isrc            => 'S',
                trackName       => 'Q',
                units           => 'X',
                currencyCode    => 'AC',
                wholesalePrice  => 'AF',
                mediaType       => 'U',
                grossRevenue    => 'AB',
            },
        },
    };
}

sub _unverifiedFieldMapExtended {
    {
        mixed => {
            option1 => {
                _netUnits                    => 'X',
                _netShareAccountCurrency     => 'AE',
                _mechanicalDeduction         => 'AG',
                _mechanicalDeductionAdminFee => 'AH',
                _trackArtist                 => 'P',
                _productArtist               => 'J',
            },
        }
    };
}

sub _headerIdentifierExtended {
    {
        mixed => {
            option1 => { upc => 'DISPLAY UPC' }
        },
    };
}

sub artistName {
    my $self = shift;

    return $self->productType eq RPS::File::Sale::TYPE_TRACK
        ? $self->_getByFieldName('_trackArtist')
        : $self->_getByFieldName('_productArtist');
}

sub saleDates {
    my $self = shift;

    return $self->dateBegin, $self->dateEnd;
}

sub productType {
    my $self = shift;

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

    my $hTypeMapping = $self->_getMapping();
    my $productType = $hTypeMapping->{$type}{product_type} if exists $hTypeMapping->{$type};

    # edge case
    my $trackArtist = $self->_getByFieldName('_trackArtist') || '';
    my $isrc        = $self->_getByFieldName('isrc')         || '';
    my $trackName   = $self->_getByFieldName('trackName')    || '';
    if ( $type =~ /^streaming bonus$/i && !$trackArtist && !$isrc && !$trackName ) {
        $productType = RPS::File::Sale::TYPE_ALBUM;
    }

    return $productType if $productType;

    my $product_code = $self->_getByFieldName('clientProductID') || '';
    return RPS::File::Sale::TYPE_CD if ($type =~ /^physical/i && $product_code =~ /CD/i);
    return RPS::File::Sale::TYPE_LP if ($type =~ /^physical/i && $product_code =~ /LP/i);
    return RPS::File::Sale::TYPE_CD if ($type =~ /^physical/i && $product_code !~ /(?:CD|LP)/i);

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

sub grossRevenue {
    my $self = shift;

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

    return sprintf("%.8f", $grossRevenue);
}


1;
