package BookPub::Import::Importer::Mackin::Version3;

use strict;
use warnings;

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

sub _fieldMap {
    {
        dateBegin     => 'E1',
        dateEnd       => 'E1',
        rPurchaseType => 'B',
        rProductType  => 'A3',
        rTitle        => 'A',
        rIsbn13       => 'C',
        rSales        => 'D',
        rUnits        => 'D',
        rUnitPrice    => 'F',
        rListPrice    => 'E',
        rDiscount     => 'H',
        rRevenue      => 'G',
    }
}

sub _headerIdentifier { rIsbn13 => 'ISBN' }

sub _useIsSaleRec { 1 }

sub priceType            { 'rrp' }
sub countryCode          { 'US' }
sub currencyCode         { 'USD' }
sub rListPriceCurrency   { 'USD' }
sub productType          { shift->rProductType }
sub purchaseType         { shift->rPurchaseType }
sub priceTypeQualifierID { BookPub::DB::Item::PriceTypeQualifier::kCorporate }


sub rServiceName {
    return BookPub::Tracker::Service::GetDefaultServiceName( service_id => shift->serviceID );
}

sub dateBegin {
    my $self = shift;

    # Sales for 11/01/2025 to 11/30/2025
    my $date = $self->{dateBegin} || '';

    if ( $date =~ /(\d{2})\/(\d{2})\/(\d{4}) to \d{2}\/\d{2}\/\d{4}/i ) {
        return sprintf "%04d-%02d-%02d", $3, $1, $2;
    }
    # Extract from file name *_2025_11_01_to_2025_11_30
    if ( $self->filename =~ /(\d{4})[ _](\d{2})[ _](\d{2})[ _]to[ _]\d{4}[ _]\d{2}[ _]\d{2}/ ) {
        return sprintf "%04d-%02d-%02d", $1, $2, $3;
    }

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

sub dateEnd {
    my $self = shift;

    # Sales for 11/01/2025 to 11/30/2025
    my $date = $self->{dateEnd} || '';

    if ( $date =~ /\d{2}\/\d{2}\/\d{4} to (\d{2})\/(\d{2})\/(\d{4})/i ) {
        return sprintf "%04d-%02d-%02d", $3, $1, $2;
    }
    # Extract from file name *_2025_11_01_to_2025_11_30
    if ( $self->filename =~ /\d{4}[ _]\d{2}[ _]\d{2}[ _]to[ _](\d{4})[ _](\d{2})[ _](\d{2})/ ) {
        return sprintf "%04d-%02d-%02d", $1, $2, $3;
    }

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

sub rPurchaseType {
    my $self = shift;

    my $rPurchaseType = lc( $self->_getByFieldName('rPurchaseType') || '' );

    if ( $rPurchaseType eq 'm' || $rPurchaseType eq 's' ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeSubscriptionDL;
    } elsif ( $rPurchaseType eq 'm/s' || $rPurchaseType eq 's/s' || $rPurchaseType eq 'c/9' ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeSubscriptionDL;
    } elsif ( $rPurchaseType eq 'm/p' || $rPurchaseType eq 's/p' ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeDownload;
    }

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

sub rProductType  {
    my $self = shift;

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

    return BookPub::DB::Item::Sale::kProductTypeEBook     if $rProductType =~ /EBOOKS$/i;
    return BookPub::DB::Item::Sale::kProductTypeAudioBook if $rProductType =~ /DIGITAL AUDIO$/i;

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

sub rUnits {
    my $self = shift;

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

    return $rUnits + 0;
}

sub rRevenue {
    my $self = shift;

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

    return $rRevenue + 0;
}

sub _isSaleRec {
    my $self = shift;

    return $self->rTitle && $self->rIsbn13 && $self->rIsbn13 ne 'ISBN';
}

sub isFree {
    my $self = shift;

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


1;