package BookPub::Import::Importer::FranklinUniversity::v1;

use strict;
use warnings;

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

use constant RE_STATE_ZIP => qr/,\s?(\w+)\s(\d+)$/;

sub _fieldMap {
    {
        rServiceName => 'D1',
        dateBegin    => 'H',
        dateEnd      => 'I',
        rAuthor      => 'B',
        rTitle       => 'C',
        rIsbn13      => 'D',
        rInstitution => 'D1',
        rState       => 'D4',
        rPostalCode  => 'D4',
        rUnits       => 'A',
        rUnitPrice   => 'E',
        rListPrice   => 'G',
        rRevenue     => 'F',
        rChannel     => 'D2',
    };
}

sub _headerIdentifier  { rIsbn13 => 'FIRST DAY CUSTOM ISBN' }
sub priceType          { 'rrp' }
sub countryCode        { 'US' }
sub currencyCode       { 'USD' }
sub rListPriceCurrency { 'USD' }
sub rServiceName       { 'Franklin University' }
sub rInstitution       { 'Franklin University' }
sub productType        { BookPub::DB::Item::Sale::kProductTypeEBook }
sub rProductType       { BookPub::DB::Item::Sale::kProductTypeEBook }
sub purchaseType       { BookPub::DB::Item::Sale::kPurchaseTypeLoan }

sub rState {
    my $self = shift;

    unless ( exists $self->{_r_state} ) {
        my $d4Cell = $self->_getByFieldName('rState') || '';
        ( $self->{_r_state}, $self->{_postal_code} ) = $d4Cell =~ RE_STATE_ZIP;
    }

    return $self->{_r_state};
}
sub rPostalCode {
    my $self = shift;

    unless ( exists $self->{_postal_code} ) {
        my $d4Cell = $self->_getByFieldName('rPostalCode') || '';
        ( $self->{_r_state}, $self->{_postal_code} ) = $d4Cell =~ RE_STATE_ZIP;
    }

    return $self->{_postal_code};
}

sub rDiscount {
    my $self = shift;

    my $unitPrice  = $self->_getByFieldName('rUnitPrice') || 0;
    my $rListPrice = $self->_getByFieldName('rListPrice') || 0;
    return 1 - ( $unitPrice / $rListPrice );
}

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


1;