#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package Metadata::DB::Item::ContentImport;
use strict;
use warnings;
use lib '/app/tools/common/lib';
use Common::DB::Item;
use Common::DB::ItemCollection;
use Common::Assert;

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

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

sub getStagedImport {
    my $class  = shift;
    my %params = @_;

    my $self = {};
    bless $self, $class;

    my $sql =
        "SELECT *, "
      . " IF( validate_date, UNIX_TIMESTAMP(validate_date) - UNIX_TIMESTAMP(date_created), NULL ) as elapse "
      . " FROM "
      . kTable
      . " WHERE store_date IS NULL and reject_date IS NULL AND "
      . "    content_import_id IN ( SELECT MAX(content_import_id) FROM "
      . kTable . ") ";

    Common::Log::Debug("Query: $sql");

    my $dbo = Common::RSApp::GetClientDB();

    my $sth = $dbo->DoCmd($sql);
    my $row = $sth->fetchrow_hashref();

    return undef unless ($row);

    my $success = $self->_loadFromHash($row);

    return $success ? $self : undef;
}

sub getLastImport {
    my $class  = shift;
    my %params = @_;

    my $self = {};
    bless $self, $class;

    my $sql = "SELECT * FROM " . kTable . " WHERE store_date IS NOT NULL " . " ORDER BY store_date DESC LIMIT 1";

    my $dbo = Common::RSApp::GetClientDB();

    my $sth = $dbo->DoCmd($sql);
    my $row = $sth->fetchrow_hashref();

    return undef unless ($row);

    my $success = $self->_loadFromHash($row);

    return $success ? $self : undef;
}

sub GetValidationProgress {
    my $class = shift;
    assert($class);

    my $dbo    = Common::RSApp::GetClientDB();
    my $import = $class->getStagedImport();

    # Return undef if there are no staged imports;
    return unless ($import);

    # Return 100% complete if there are no staged imports;
    return ( 1, $import->{elapse} ) if ( $import->validate_date );

    # If we don't know how many rows are in the file just return 100%
    return (1) unless ( $import->file_row_count );

    my $total_rows = $import->file_row_count() * 3;

    my $sql =
        "SELECT sum(validated) as count, "
      . "FORMAT(AVG(UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(date_created)),0) as elapse "
      . "FROM content_import_data "
      . "LEFT JOIN content_import USING(content_import_id) "
      . "WHERE "
      . "content_import.content_import_id = "
      . $import->content_import_id();

    Common::Log::Debug("QUERY: $sql");

    my $sth         = $dbo->DoCmd($sql);
    my $row         = $sth->fetchrow_hashref();
    my $validated   = $row->{count} || 0;
    my $elapse_time = $row->{elapse};

    my $percentage = $validated / $total_rows;
    Common::Log::Debug("Validated: $validated, Rows: $total_rows, Percentage: $percentage");

    if ( $percentage >= 1 ) {
        $percentage = .99;
    }

    return ( sprintf( "%.4f", $percentage ), $elapse_time );
}

1;
