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

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

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

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

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

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

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

    my $sql =
        "SELECT * FROM "
      . kTable
      . " INNER JOIN product_monitor_item USING (product_monitor_item_id) WHERE "
      . " product_id = "
      . $class->quote( $args{product_id} ) . " AND "
      . " service_id = "
      . $class->quote( $args{service_id} ) . " AND "
      . " status = 'current'";

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

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

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

    my $sql = "SELECT * FROM " . kTable . " INNER JOIN product_monitor_item USING (product_monitor_item_id) WHERE ";

    my $where .= " status = 'current'";
    foreach my $field ( keys(%args) ) {
        $where .= " AND " if ($where);
        if ( defined( $args{$field} ) ) {
            $where .= " $field = " . $class->quote( $args{$field} );
        } else {
            $where .= " $field IS NULL ";
        }
    }

    $sql .= $where;

    Log->debug("QUERY: $sql");
    my $collection = $class->SUPER::GetAll($sql);

    return !$collection->hasNext();
}

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

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

    my $sql =
        "UPDATE product_monitor_item INNER JOIN "
      . kTable
      . " USING (product_monitor_item_id) "
      . "SET last_verified = NOW(), verified_count = verified_count + 1 "
      . "WHERE "
      . " 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 InvalidateRecords {
    my $class = shift;
    my %args  = @_;

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

    my $sql =
        "UPDATE product_monitor_item INNER JOIN "
      . kTable
      . " USING (product_monitor_item_id) "
      . "SET status = 'old' "
      . "WHERE "
      . " 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);
}

1;
