#---------------------------------------------------------------
# ____                   _ _         ____  _                    
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___ 
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/                            
#
# Copyright (C) 2012 RoyaltyShare, Inc.   All Rights Reserved
#---------------------------------------------------------------

package RPS::ArtistRoyalty::Fast::Script::AssembleMissedSaleLog;

use strict;
use Data::Dumper;
use File::Path;

use lib '/app/tools/common/lib';
use lib '/app/tools/rps/lib';
use lib '/app/tools/raptor/lib';

use DB_File;

use Common::RSApp;
use Common::Util;
use Common::Log;
use Common::Parser;
use Common::WriteXML;


use RPS::DB::Item::ArtistRoyaltyRun;
use RPS::ArtistRoyalty::Fast::Mapped::MissedSales;
use RPS::ArtistRoyalty::Fast::Final::ConsolidatedArtistRoyaltyRunMissedSaleLog;
use RPS::ArtistRoyalty::Fast::Static::ContractTitleArtists;
use RPS::ArtistRoyalty::Fast::Static::MissedSaleProducts;


use base 'Common::Script';


sub _options {{
    client_id => 
    {
        short       => 'c',
        required    => 1,
        description => 'Limit to this client id',
        parameter    => 'i'
    },
    data_path => 
    {
        short       => 'd',
        required    => 1,
        description => 'path to directory containing static data',
        parameter    => 's'
    },
    output_path => 
    {
        short       => 'o',
        required    => 1,
        description => 'path to directory to write output files',
        parameter    => 's'
    },
    run_id =>
    {
        short       => 'r',
        required    => 1,
        description => 'The artist_royalty_run_id we are creating statements for.',
        parameter   => 'i',
    },
    index_range =>
    {
        short       => 'i',
        required    => 0,
        description => 'First,Last data indexes to process.  Ex: -s0,10000',
        parameter   => 's',
    },
}}



sub _process
{
    my ($self) = @_;

    my $dataPath = $self->param('data_path');
    my $outputDirectory = $self->param('output_path');
    my $runID = $self->param('run_id');

    # Read in the run record, so we can get the correct payor_id.
    #
    my $runData = RPS::DB::Item::ArtistRoyaltyRun->Lookup(artist_royalty_run_id => $runID);
    my $payorID = $runData->payor_id();

    my $inData = RPS::ArtistRoyalty::Fast::Mapped::MissedSales->GetMissedSales($outputDirectory);

    # Load my static data.
    # I'm going to need a lot of it.
    #
    my $artistContractData = RPS::ArtistRoyalty::Fast::Static::ContractTitleArtists->GetContractTitleArtistData();
    my $productData = RPS::ArtistRoyalty::Fast::Static::MissedSaleProducts->GetMissedSaleProductData();

    RPS::ArtistRoyalty::Fast::Final::ConsolidatedArtistRoyaltyRunMissedSaleLog->Init($outputDirectory, $runID);


    my $logLine;

    while (my $data = $inData->nextLine())
    {
        Log->info("\n\nDATA:", $data);

        if (! $logLine || ! $logLine->process($data))
        {
            $logLine = undef;
            $logLine = RPS::ArtistRoyalty::Fast::Final::ConsolidatedArtistRoyaltyRunMissedSaleLog->new($data, $runID, $artistContractData, $productData);
        }
    }


    # Explicitly release these references, so they write out immediately.
    #
    $logLine = undef;


    # Now that we've got all the data assembles, use mysqlimport to slurp it all in.
    #
#    RPS::ArtistRoyalty::Fast::Final::ConsolidatedArtistRoyaltyRunMissedSaleLog->Commit($outputDirectory);
}

1;
