package RPS::Import::Shopify::v1;

use strict;
use warnings;

use Date::Calc qw(Days_in_Month);

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

sub physical { 2 }

sub _fieldMapExtended {
    {
        mixed => {
            option1 => {
                countryCode     => 'A',
                productType     => 'D',
                clientProductID => 'H',
                artistName      => 'E',
                upc             => 'G',
                albumName       => 'F',
                sales           => 'I',
                totalRevenue    => 'J',
                units           => 'I',
                price           => 'J',
            },
            option2 => {
                countryCode     => 'A',
                productType     => 'D',
                clientProductID => 'H',
                artistName      => 'E',
                upc             => 'G',
                albumName       => 'F',
                sales           => 'I',
                totalRevenue    => 'J',
                units           => 'I',
                price           => 'J',
            },
        },
    };
}

sub _unverifiedFieldMapExtended {
    {
        mixed => {
            option1 => {
                saleYear  => 'B',
                saleMonth => 'C',
            },
            option2 => {
                saleYear  => 'B',
                saleMonth => 'C',
            },
        },
    };
}

sub _headerIdentifierExtended {
    {
        mixed => {
            option1 => { productType => 'Product Type' },
            option2 => { productType => 'Product Type' },
        },
    };
}

sub mediaType { RPS::DB::Item::MediaType::kMediaTypeAudio }
sub serviceID { Client::Service::DSP_SHOPIFY }

sub currencyCode { 'USD' }

sub saleDates {
    my $self = shift;

    my $year = $self->_getByFieldName('saleYear') || '';
    my $month = $self->_getByFieldName('saleMonth') || '';

    $self->fail("Unable to determine sale dates from year: '$year', month: '$month'") unless ($year && $month);

    my $dateBegin = sprintf("%04d-%02d-%02d", $year, $month, 1);
    my $dateEnd = sprintf("%04d-%02d-%02d", $year, $month, Days_in_Month($year, $month));

    return ($dateBegin, $dateEnd);
}


sub productType {
    my $self = shift;

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

    # Physical
    return RPS::File::Sale::TYPE_LP if $type =~ /Vinyl/i;
    return RPS::File::Sale::TYPE_CASS if $type =~ /Cassette/i;
    return RPS::File::Sale::TYPE_CD if $type =~ /Compact Disc/i;
    return RPS::File::Sale::TYPE_DVD if $type =~ /DVD/i;
    return RPS::File::Sale::TYPE_LP if $type =~ /Limited Clear 7"/i;
    # Digital
    return RPS::File::Sale::TYPE_ALBUM;

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

sub formatType {
    my $self = shift;

    return unless $self->isDigitalProduct( $self->productType );

    my $type = $self->_getByFieldName('productType') || '';
    return RPS::File::Sale::FORMAT_DOWNLOAD if $type =~ /Download/i;

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

# physical
sub channel            { RPS::File::Sale::CHANNEL_RETAIL }
sub priceLevel         { RPS::File::Sale::PLEVEL_UNKNOWN }
sub totalRevenue       { $_[0]->_getByFieldName('totalRevenue') || 0 }
sub sales              { abs( $_[0]->_getByFieldName('sales') || 0 ) }
sub salesRevenue       { $_[0]->totalRevenue > 0 ? $_[0]->totalRevenue : 0 }

sub _isValidSaleRecord {
    return $_[0]->_getByFieldName('albumName') || $_[0]->_getByFieldName('trackName');
}

1;
