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

package RPS::LabelRoyalty::Fast::Script::StageData;

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 RPS::DB::Item::LabelRoyaltyRun;

use Common::Log;

# Packages for the data files.
# !!! These will probably end up in a different namespace soon.
#
use RPS::LabelRoyalty::Fast::Static::Sales;
use RPS::LabelRoyalty::Fast::Static::LabelTerms;
use RPS::LabelRoyalty::Fast::Static::Exceptions;
use RPS::LabelRoyalty::Fast::Static::Products;
use RPS::LabelRoyalty::Fast::Static::Services;
use RPS::LabelRoyalty::Fast::Static::LabelPayeeAccounts;
use RPS::LabelRoyalty::Fast::Static::LabelPayeePendingTransactions;

use base 'Common::Script';

sub _filePackages {
    (
        'RPS::LabelRoyalty::Fast::Static::Sales',      'RPS::LabelRoyalty::Fast::Static::LabelTerms',
        'RPS::LabelRoyalty::Fast::Static::Exceptions', 'RPS::LabelRoyalty::Fast::Static::Products',
        'RPS::LabelRoyalty::Fast::Static::Services',   'RPS::LabelRoyalty::Fast::Static::LabelPayeeAccounts',
        'RPS::LabelRoyalty::Fast::Static::LabelPayeePendingTransactions',
    );
}

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 to store the static data files',
            parameter   => 's'
        },
        run_id => {
            short       => 'r',
            required    => 1,
            description => 'label_royalty_run_id to process',
            parameter   => 'i'
        },
        files => {
            short       => 'f',
            required    => 0,
            description => 'a comma-separated list of files to extract.',
            parameter   => 's',
        },
    };
}

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

    my $dataPath = $self->param('data_path');

    # JPK - Should it be the responsibility of this process to create the directory if
    # it doesn't already exist?   It's a bit of a stretch, but then again it's convenient...
    #
    if ( !-d $dataPath ) {

        # !!! Might want to make this behavior optional...
        #
        mkpath($dataPath);
    }

    my $runID = $self->param('run_id');
    my $runData = RPS::DB::Item::LabelRoyaltyRun->Lookup( label_royalty_run_id => $runID );

    my $fileHash;
    my @files = split( ',', $self->param('files') );
    if ( scalar @files ) {
        $fileHash = {};
        foreach my $f (@files) {
            $fileHash->{$f} = 1;
        }
    }

    # !!! Fetch payor_id from the run data.
    #
    my $payorID = $runData->payor_id();

    foreach my $pkg ( $self->_filePackages() ) {
        if ( !defined $fileHash || $fileHash->{ $pkg->FileName() } ) {
            Log->warn( "fetching '" . $pkg->FileName() . "'" );
            $pkg->Create( $dataPath, $payorID, $runID );
        }
    }

    Log->info('done');
}

1;
