#!/usr/bin/perl
use strict;

# This script will take an existing, already-imported sales file and re-import it
# into the 'shadow_sale' table.
#
# At this point I'm going to try and avoid making any state changes to the 'file' table.
# We may find it necessary to create some sort of shadow file table as well, if that proves
# impractical.
#
# Going to relax any testing of the file status as well.

# -----------------------------------------
# input params:
#
# 	- client_id
# 	- file_id
#
# -----------------------------------------
# what it does:
#
# 	- get file record from db
# 	- if status is NEW or IN_QUEUE
# 		- change status to PROCESSING
# 		- set import_pid to this pid ($$)
# 		- import file
# 		- if success
# 			- change status to OPEN
#		- else
#			- fail?
#	- elsif status is PROCESSING
#		- if import_pid is not running
#			- try to re-import file
#		- else
#			- do nothing
#	- else
#		- do nothing
#
# -----------------------------------------

use lib '/app/tools/common/lib';
use Common::RSDB;
use Common::Log;

use lib '/app/tools/data_classes/lib';
use Client::Service;

use lib '/app/tools/rps/lib';
use RPS::File::File::ReadOnly;
use RPS::Sale::File;

use lib '/app/tools/common/lib';
use Common::Util;
use Common::RSApp;

use constant SANCTUARY => 31;

import_sales_file();

sub import_sales_file {
    my $client_id = $ARGV[0] or return usage("no client id passed");
    my $file_id   = $ARGV[1] or return usage("no file id passed");

    my $service_id = $ARGV[2];
    my $version    = $ARGV[3];
    my $file_path  = $ARGV[4];

    if ( $service_id || $version ) {
        return usage('both service_id AND version_num must be supplied, or neither at all') unless ( $service_id && $version );
    }

    # Instantiate the Application Singleton object.  This will initialize our global environment.
    #
    my $appSingleton = Common::RSApp->new( clientID => $client_id );

    # JPK - First change:  We'll use a 'read-only' subclass of RPS::File::File.
    # All this will do is make the 'Save' method a no-op.
    #
    my $file = RPS::File::File::ReadOnly->new( file_id => $file_id, client_id => $client_id );
    if ( !$file || $file->FileID != $file_id ) {
        return usage("File $file_id was not found.");
    }

    # !!! Don't care about status.
    #
    # - parse sales file
    #
    my $sf = RPS::Sale::File->new( client_id => $client_id );
    my $success = $sf->ParseFull(
        client_id         => $client_id,
        file              => $file,
        service_id        => $service_id,
        version           => $version,
        file_path         => $file_path,
        sale_object_class => 'RPS::File::Sale::Shadow',    # !!! This is a new argument.
    );

    # We're not going to update the file status, so just report whether ParseFull worked.
    #
    if ($success) {
        Common::Log::Print("Complete");
    } else {
        Common::Log::Print( "ERROR - ParseFull failed: errstr = " . $sf->errstr() );
    }
}

sub usage {
    my $err = shift;

    print STDERR <<EofUSAGE;
ERROR: $err
Usage: import_sales_file.pl CLIENT_ID FILE_ID [SERVICE_ID VERSION_NUM ALT_FILE_PATH]
EofUSAGE

    return 1;
}
