
#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2011 RoyaltyShare, Inc.   All Rights Reserved
#---------------------------------------------------------------
package BookPub::DB::Item::ProductMonitorItem;

use strict;
use warnings;
use Data::Dumper;

use lib '/app/tools/common/lib';
use lib '/app/tools/bookpub/lib';
use Common::Assert;

use base 'Common::DB::Item';

use constant kTable => 'product_monitor_item';
use constant kDB    => Common::DB::Item::kClientDB;

sub ResolveItems {
    my $class = shift;
    my %args  = @_;

    my $bookID         = $args{book_id};
    my $excludeProduct = $args{exclude_product_id};
    my $productID      = $args{product_id};
    my $serviceID      = $args{service_id};

    assert($serviceID);
    assert( $productID || $bookID );

    my $sql =
        "UPDATE "
      . kTable
      . " INNER JOIN book_product USING (product_id) SET status = 'old' "
      . "WHERE service_id = "
      . $class->quote($serviceID);

    if ($productID)      { $sql .= " AND product_id = " . $class->quote($productID); }
    if ($bookID)         { $sql .= " AND book_id    = " . $class->quote($bookID); }
    if ($excludeProduct) { $sql .= " AND product_id <> " . $class->quote($excludeProduct); }

    my $dbo = Common::RSApp::GetClientDB;
    Log->debug("QUERY: $sql");
    $dbo->DoCmd($sql);
}

sub GetLastMonitoredDate {
    my $class = shift;
    my %args  = @_;

    assert( %args,             "No args passed" );
    assert( $args{product_id}, "Product ID Required" );

    # product_id can be either a single id, or a reference to an array of ids.
    # We'll convert the single ID to an array of one
    #
    my @quotedProductIDs;
    if ( 'ARRAY' ne ref( $args{product_id} ) ) {
        push @quotedProductIDs, $class->quote( $args{product_id} );
    } else {
        foreach my $id ( @{ $args{product_id} } ) {
            push @quotedProductIDs, $class->quote($id);
        }
    }
    my $productIDString = join( ',', @quotedProductIDs );

    my $type = $args{type};

    my $dbo = Common::RSApp::GetClientDB();
    my $sql = "SELECT MAX(last_verified) As max FROM " . kTable . " WHERE product_id IN ( $productIDString )";

    if ($type) {
        $sql .= " AND type = '$type'";
    }

    my $sth     = $dbo->DoCmd($sql);
    my $hr      = $sth->fetchrow_hashref();
    my $maxDate = $hr->{'max'};
    return undef unless defined $maxDate;
    return $maxDate;
}

sub LastUpdated {
    my $class     = shift;
    my %args      = @_;
    my $serviceID = $args{service_id};

    my $sql = "SELECT MAX(last_verified) as max FROM " . kTable;

    if ($serviceID) {
        $sql .= " WHERE service_id = " . $class->quote($serviceID);
    }

    Log->debug("QUERY: $sql");

    my $collection = $class->SUPER::GetAll($sql);

    return $collection->hasNext ? $collection->next->{max} : undef;
}

sub InvalidateOtherEbookProducts {
    my $class = shift;
    my %args  = @_;

    assert( $args{product_id}, "Product ID Required" );
    assert( $args{book_id},    "Book ID Required" );
    assert( $args{service_id}, "Service ID Required" );

    my $sql =
        "UPDATE "
      . kTable
      . " INNER JOIN book_product USING (product_id) "
      . "SET status = 'old' "
      . "WHERE "
      . " book_id = "
      . $class->quote( $args{book_id} ) . " AND "
      . " product_id <> "
      . $class->quote( $args{product_id} ) . " AND "
      . " service_id = "
      . $class->quote( $args{service_id} ) . " AND "
      . " status = 'current'";

    Log->debug("QUERY: $sql");
    my $dbo = Common::RSApp::GetClientDB();
    $dbo->DoCmd($sql);
}

sub GetDistinctServiceIDsAlphabetical {
    my ($class) = @_;

    # !!! This may be too broad, but let's go with it for now.
    #
    # !!! Excluding Borders from this list for now.
    my $sql =
      "SELECT DISTINCT(service_id) FROM " . kTable . " INNER JOIN service USING (service_id) WHERE service_id != 49 ORDER BY service_name";

    return $class->SUPER::GetAll($sql);
}

1;
