package BookPub::Import::Importer::Spotify::v2;

use strict;
use warnings;

use lib '/app/tools/bookpub/lib';
use BookPub::Tracker::Service;
use base 'BookPub::Import::Importer';

sub _fieldMap {
    {
        dateBegin          => 'E',
        dateEnd            => 'F',
        rFormat            => 'AD',
        priceType          => 'AN',
        rTitle             => 'X',
        rSubtitle          => 'Y',
        rIsbn13            => 'U',
        rImprint           => 'AC',
        countryCode        => 'AL',
        rClientProductID   => 'AA',
        serviceProductID   => 'W',
        rReturns           => 'AG',
        rSales             => 'AF',
        rUnits             => 'AH',
        rListPrice         => 'AM',
        rListPriceCurrency => 'AO',
        rDiscount          => 'AP',
        rRevenue           => 'BM',
        currencyCode       => 'I',
        rFee               => 'BL',
        isFree             => 'AI',
    };
}

sub _unverifiedFieldMap {
    {
        _transactionDate => 'O',
    }
}

sub _headerIdentifier { rTitle => 'Product title' }

sub _useIsSaleRec { 1 }
sub purchaseType  { BookPub::DB::Item::Sale::kPurchaseTypeStream }
sub rPurchaseType { shift->purchaseType }
sub productType   { BookPub::DB::Item::Sale::kProductTypeAudioBook }
sub rProductType  { shift->productType }
sub rServiceName { 'Spotify' }


sub dateBegin {
    my $self = shift;

    my $transactionDate = $self->_getByFieldName('_transactionDate') || '';
    my $dateBegin = $self->_getByFieldName('dateBegin') || '';

    if ( $transactionDate =~ /^(\d{4})(\d{2})(\d{2})$/ ) {
        return sprintf "%04d-%02d-%02d", $1, $2, $3;
    } elsif ( $dateBegin =~ /^(\d{4})(\d{2})(\d{2})$/ ) {
        return sprintf "%04d-%02d-%02d", $1, $2, $3;
    }

    $self->fail("Unable to determine dateBegin from '$transactionDate' or '$dateBegin'");
}

sub dateEnd {
    my $self = shift;

    my $transactionDate = $self->_getByFieldName('_transactionDate') || '';
    my $dateEnd = $self->_getByFieldName('dateEnd') || '';

    if ( $transactionDate =~ /^(\d{4})(\d{2})(\d{2})$/ ) {
        return sprintf "%04d-%02d-%02d", $1, $2, $3;
    } elsif ( $dateEnd =~ /^(\d{4})(\d{2})(\d{2})$/ ) {
        return sprintf "%04d-%02d-%02d", $1, $2, $3;
    }

    $self->fail("Unable to determine dateEnd from '$transactionDate' or '$dateEnd'");
}

sub priceType {
    my $self = shift;

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

    return 'rrp' if $type =~ /^(?:01|41)$/;
}

sub units {
    my $self = shift;

    my $units   = $self->_getByFieldName('rUnits') || 0;
    my $revenue = $self->_getByFieldName('rRevenue') || 0;

    if ( !$units && $revenue ) {
        $self->fail("No units, but there is revenue");
    }

    if ( $units =~ /\./ ) {
        $self->fail("Fractional units detected '$units'");
    }

    return $units;
}

sub isFree {
    my $self = shift;

    my $revenue = $self->_getByFieldName('rRevenue') || 0;
    my $units = $self->_getByFieldName('isFree') || 0;

    return $units && !($revenue * 1) ? 'Y' : 'N';
}


1;