package BookPub::Import::Importer::Chirp::v1;

use strict;
use warnings;

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

sub _fieldMap {
    {
        dateBegin          => 'E1',
        rAuthor            => 'D',
        rTitle             => 'C',
        rIsbn13            => 'B',
        countryCode        => 'E',
        serviceProductID   => 'A',
        rReturns           => 'M',
        rSales             => 'J',
        rUnitPrice         => 'I',
        rListPrice         => 'H',
        rListPriceCurrency => 'F',
        currencyCode       => 'F',
        rPromoCode         => 'G',
    };
}

sub _unverifiedFieldMap {
    {
        _rUnitPriceAlt => 'N',
        _invoice       => 'K',
        _returns       => 'O',
        _rRevenueCheck => 'E5',
    };
}
sub _headerIdentifier { rTitle => 'Title' }


sub rServiceName       { 'Chirp ' }
sub _useIsSaleRec      { 1 }
sub purchaseType       { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType        { BookPub::DB::Item::Sale::kProductTypeAudioBook }
sub rPurchaseType      { $_[0]->purchaseType() }
sub rProductType       { $_[0]->productType() }
sub rListPriceCurrency { $_[0]->currencyCode() }
sub rPriceType         { 'rrp' }

sub dateBegin {
    my $self = shift;
    #9/1/2022 - 9/30/2022 M/D/Y
    my $date = $self->_getByFieldName('dateBegin') || "";
    if ( my ($month, $day, $year) = $date =~ /^(\d{1,2})\/(\d{1,2})\/(\d{4}) -/) {
        return sprintf("%04d-%02d-%02d", $year, $month, $day);
    }

    $self->fail("Unable to determine date begin from '$date'");
}

sub dateEnd {
    my $self = shift;
    #9/1/2022 - 9/30/2022 M/D/Y
    my $date = $self->_getByFieldName('dateBegin') || "";
    if ( my ($month, $day, $year) = $date =~ /- (\d{1,2})\/(\d{1,2})\/(\d{4})$/) {
        return sprintf("%04d-%02d-%02d", $year, $month, $day);
    }

    $self->fail("Unable to determine date end from '$date'");
}

sub currencyCode {
    my $self = shift;

    my $code = $self->_getByFieldName('currencyCode') || '';
    return $code if $code =~ /^\w{3}$/;

    $self->fail("Unable to determine currency code from '$code'");
}

sub rUnits {
    my $self = shift;

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

    return ($sales-$returns);
}

sub rUnitPrice {
    my $self = shift;

    my $price = $self->_getByFieldName('rUnitPrice') || 0;
    my $priceAlt = $self->_getByFieldName('_rUnitPriceAlt') || 0;

    $price =~ s/[,\$]//g;
    $priceAlt =~ s/[,\$]//g;

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

    return ($returns > 0 ? $priceAlt : $price);
}

sub rListPrice {
    my $self = shift;

    my $price = $self->_getByFieldName('rListPrice') || 0;
    $price =~ s/[,\$]//g;
    return $price;
}

sub rRevenue {
    my $self = shift;

    my $invoice = $self->_getByFieldName('_invoice') || 0;
    my $returns = $self->_getByFieldName('_returns') || 0;

    $invoice =~ s/[,\$]//g;
    $returns =~ s/[,\$]//g;

    my $revenue = sprintf "%.8f", ($invoice - $returns);
    $self->{_total_revenue}{$self->linenum} = $revenue;
    return $revenue;
}

sub isFree {
    my $self = shift;

    return !$self->rRevenue && $self->rUnits ? 'Y' : 'N';
}

sub _isSaleRec {
    my $self = shift;

    if ( $self->rSales =~ /Total/i ) {
        my $revenueCheck = $self->_getByFieldName('_rRevenueCheck') || 0;
        $revenueCheck =~ s/[,\$]//g;
        $revenueCheck = sprintf "%.2f", $revenueCheck;

        my $totalRevenue = 0;
        $totalRevenue += $_ for values %{ $self->{_total_revenue} };
        $totalRevenue = sprintf "%.2f", $totalRevenue;

        if ( $totalRevenue ne $revenueCheck ) {
            $self->fail("Revenue '$totalRevenue' does not match amount '$revenueCheck' in the header section");
        }

        return;
    }

    return if !$self->rRevenue && !$self->rTitle;

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


1;
