package RPS::Import::TheOrchardUK::v1;

use strict;
use warnings;

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

use parent 'RPS::Import::ImporterMixed';

sub physical { 2 }

sub _fieldMapExtended {
    {
        physical => {
            option1 => {
                labelName       => 'E',
                countryCode     => 'H',
                dateBegin       => 'N',
                productType     => 'F',
                clientProductID => 'A',
                artistName      => 'D',
                albumName       => 'C',
                sales           => 'O',
                returns         => 'P',
                mediaType       => 'F',
            },
        },
    };
}

sub _unverifiedFieldMapExtended {
    {
        physical => {
            option1 => {
                _physicalCodePrice => 'K'
            },
        },
    };
}

sub _headerIdentifierExtended {
    {
        physical => {
            option1 => {
                productType => 'Distribution Format Media Format',
            },
        },
    };
}

sub currencyCode { 'GBP' }
sub channel      { RPS::File::Sale::CHANNEL_RETAIL }
sub priceLevel   { RPS::File::Sale::PLEVEL_UNKNOWN }

sub countryCode {
    my $self = shift;

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

    my $country = Common::Country->Get($name);
    return $country->alpha2() if $country;

    $self->fail( "Unable to determine country from '$name'." );
}

sub saleDates {
    my $self = shift;

    # 2023-07
    my $period = $self->_getByFieldName('dateBegin') || '';
    my @yearMonth = $period =~ /^(\d{4}).(\d{1,2})$/g;
    $self->fail( "Unable to determine date from '$period'." ) unless @yearMonth == 2;

    return $self->_getDates(
        date_begin => sprintf( "%04d-%02d-%02d", @yearMonth, 1 ),
        date_end   => sprintf( "%04d-%02d-%02d", @yearMonth, Days_in_Month( @yearMonth ) ),
    );
}

sub productType {
    my $self = shift;

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

    return RPS::File::Sale::TYPE_LP   if $type =~ /^(?:1[02]" Vinyl)$/i;
    return RPS::File::Sale::TYPE_LP5  if $type =~ /^(?:7" Vinyl)$/i;
    return RPS::File::Sale::TYPE_CD   if $type =~ /^(?:CD)$/i;
    return RPS::File::Sale::TYPE_CASS if $type =~ /^(?:Cassette)$/i;

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

sub mediaType {
    my $self = shift;

    return RPS::DB::Item::MediaType::kMediaTypeAudio;
}

# physical
sub sales {
    my $self = shift;

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

    my $salesOut = 0;
    $salesOut += $sales   if $sales >= 0;
    $salesOut += $returns if $returns >= 0;

    return abs($salesOut);
}

sub salesRevenue {
    my $self = shift;

    my $salesRevenue = $self->sales * $self->_physicalCodePrice;

    return $salesRevenue;
}

sub returns {
    my $self = shift;

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

    my $returnsOut = 0;
    $returnsOut += $sales   if $sales < 0;
    $returnsOut += $returns if $returns < 0;

    return abs($returnsOut);
}

sub returnsRevenue {
    my $self = shift;

    my $returnsRevenue = $self->returns * $self->_physicalCodePrice;

    return $returnsRevenue;
}

sub netUnits {
    my $self = shift;

    my $netUnits = $self->sales + $self->returns;
    return $netUnits;
}

sub totalRevenue {
    my $self = shift;

    my $revenue = ($self->sales + $self->returns * -1) * $self->_physicalCodePrice;

    return $revenue;
}

sub _physicalCodePrice {
    my $self = shift;

    my $codePrice = $self->_getByFieldName('_physicalCodePrice') || 0;
    $codePrice =~ s/^\x{00A3}//u;  # remove the £ sign

    if ( $codePrice !~ /^[+-]?\d+(?:\.\d+)?$/ ) {
        $self->fail("Non-digital physical code price: '$codePrice'");
    }

    return $codePrice;
}

sub _isValidSaleRecord {
    my $self = shift;

    return 1 if $self->sales || $self->returns || $self->totalRevenue;

    return $self->SUPER::_isValidSaleRecord();
}


1;
