package RPS::Import::Beatport::v7;

use strict;

use lib '/app/tools/common/lib';
use lib '/app/tools/rps/lib';

use Date::Calc qw(Days_in_Month);

use base 'RPS::Import::Importer';

sub _fieldMap {
    {
        labelName        => 'A',
        countryCode      => 'N',
        productType      => 'D',
        clientProductID  => 'C',
        artistName       => 'G',
        upc              => 'B',
        isrc             => 'D',
        trackName        => 'E',
        albumName        => 'I',
        price            => 'M',
        units            => 'J',
        serviceProductID => 'D',
    };
}

sub _headerIdentifier { ( upc => 'UPC' ) }

sub formatType   { RPS::File::Sale::FORMAT_STREAM; }
sub mediaType    { RPS::DB::Item::MediaType::kMediaTypeAudio; }
sub currencyCode { 'USD' }

sub saleDates {
    my $self = shift;
    my ($y, $m) = $self->filename() =~ /-(20\d{2})(\d{2})/;
    $self->fail("Can't parse date from filename: " . $self->filename()) unless ($y && $m);

    return (sprintf("%04d-%02d-01", $y, $m, 1), sprintf("%04d-%02d-%02d", $y, $m, Days_in_Month($y, $m)));
}

sub productType {
    my $self = shift;

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

    return RPS::File::Sale::TYPE_TRACK if ($type || $trackName);

    $self->fail("Unable to find product type from: '$type' or '$trackName'");
}

2;
