package BookPub::RDAS::Response::BestSellerList::NewYorkTimes;

use Data::Dumper;

use lib '/app/tools/bookpub/lib';
use base 'BookPub::RDAS::Response::BestSellerList';

sub _validOptions {
    return ('parser');
}

sub run {
    my $self = shift;
    my %args = @_;

    my $localResults  = $self->parser->BookResults();
    my $listID        = @$localResults[0]->{best_seller_list_id};
    my $maxDatePosted = BookPub::DB::Item::BestSellerMonitor->GetMaxDatePostedByListID($listID);
    if ( !$maxDatePosted ) { $maxDatePosted = "0000-00-00" }
    my $datePosted = @$localResults[0]->{date_posted};

    # compare only 1st 10 chars, just the date, not time too
    if ( substr( $maxDatePosted, 0, 10 ) eq $datePosted ) {

        # Best Sellers list hasn't been updated since our last
        # insert, abort
        Log->info("Skipping list ID: $listID, already have the latest");
        return;
    }

    # Add an entry to the monitor table
    my $monitorItem = BookPub::DB::Item::BestSellerMonitor->Create();
    $monitorItem->date_posted($datePosted);
    $monitorItem->best_seller_list_id($listID);
    $monitorItem->save();

    # insert each record into the table
    foreach $item (@$localResults) {
        my $dbitem = BookPub::DB::Item::BestSellerBook->Create();
        $dbitem->best_seller_list_id( $item->{best_seller_list_id} );
        $dbitem->best_seller_monitor_id( $monitorItem->best_seller_monitor_id );
        $dbitem->title( $item->{title} );
        $dbitem->author( $item->{author} );
        $dbitem->imprint( $item->{imprint} );
        $dbitem->rank( $item->{rank} );
        $dbitem->isbn10( $item->{isbn10} );
        $dbitem->isbn13( $item->{isbn13} );
        $dbitem->related_isbn10( $item->{related_isbn10} );
        $dbitem->related_isbn13( $item->{related_isbn13} );
        $dbitem->save();
    }

}

sub fail {
    my $self = shift;
    my %args = @_;
}

1;
