#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package RPS::Statement::Label::Report;

use strict;
use warnings;

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

use lib '/app/tools/rps/lib';
use RPS::Config;
use RPS::RoyaltyRun::Status;
use RPS::DB::Item::LabelRoyaltyStatement;
use RPS::DB::Item::LabelPayee;
use RPS::DB::Item::LabelRoyaltyRun;
use Common::DB::Item::Client;

# !!! For now, this package will probably just contain constants and maybe static methods.
# !!! Later on down the road, I'll look at moving the actual code
# !!! that generates the statements into here.
#
#use constant kBasePath => '/app/data/royalty_statements/label';

sub _BasePath {
    my $config = RPS::Config->new();
    my $path   = $config->get('label_royalty_statement_dir');
    return $path;

    #    return '/app/data/royalty_statements/mechanical';
}

sub ReportFullFilePathFromStatementID {
    my ($id) = @_;
    my $statement = RPS::DB::Item::LabelRoyaltyStatement->Lookup( label_royalty_statement_id => $id );
    my $runID     = $statement->label_royalty_run_id;
    my $path      = ReportPathFromRunID($runID);
    my $filename  = ReportFileNameFromDBItem($statement);

    return $path . $filename;
}

sub PrettyReportFileNameFromID {
    my ($id) = @_;
    assert($id);

    my $statement = RPS::DB::Item::LabelRoyaltyStatement->Lookup( label_royalty_statement_id => $id );
    return PrettyReportFileNameFromDBItem($statement);
}

sub PrettyReportFileNameFromDBItem {
    my ($statementDBItem) = @_;
    assert($statementDBItem);

    my $runID         = $statementDBItem->label_royalty_run_id;
    my $cleanRunLabel = _runLabelClean($runID);

    my $labelID   = $statementDBItem->payee_id;
    my $nameClean = _nameClean($labelID);

    # If the state of the run is not 'COMMITTED' or 'CLOSED', then we want to add
    # "DRAFT" into the filename.
    #
    my $draft;
    my $run = RPS::DB::Item::LabelRoyaltyRun->Lookup( label_royalty_run_id => $runID );
    if (   RPS::RoyaltyRun::Status::kCommitted != $run->status
        && RPS::RoyaltyRun::Status::kClosed != $run->status ) {
        $draft = "_DRAFT";
    }

    my $fileName = $nameClean . '_' . $labelID . '_' . $cleanRunLabel . $draft . ".txt";

    return $fileName;
}

sub ReportFileNameFromID {
    my ($id) = @_;
    assert($id);

    my $statement = RPS::DB::Item::LabelRoyaltyStatement->Lookup( label_royalty_statement_id => $id );
    return ReportFileNameFromDBItem($statement);
}

sub ReportFileNameFromDBItem {
    my ($statementDBItem) = @_;
    assert($statementDBItem);

    my $labelID = $statementDBItem->payee_id;

    my $fileName = "$labelID.txt";

    return $fileName;
}

# This returns the directory in which report files can be found.
# client_id is optional - if ommitted, we'll use the current client id.
#
sub ReportPathFromRunID {
    my ( $runID, $clientID ) = @_;
    assert($runID);

    $clientID = Common::RSApp::GetClientID() unless $clientID;
    my $clientData = Common::DB::Item::Client->Lookup( client_id => $clientID );
    my $clientName = $clientData->client_name_clean;

    return _BasePath() . "/$clientName/$runID/";
}

# Going to create a little ram cache, since this code will often get called in a loop.
#
my %gRunLabel;

sub _runLabelClean {
    my ($runID) = @_;

    my $clientID = Common::RSApp::GetClientID();
    unless ( exists $gRunLabel{$clientID} && exists $gRunLabel{$clientID}{$runID} && defined $gRunLabel{$clientID}{$runID} ) {
        my $run = RPS::DB::Item::LabelRoyaltyRun->Lookup( label_royalty_run_id => $runID );
        assert( $run, "ERROR - runID $runID is invalid" );

        $gRunLabel{$clientID}{$runID} = Common::Util::clean_name( $run->label );
    }

    return $gRunLabel{$clientID}{$runID};
}

my %gLabelName;

sub _nameClean {
    my ($labelID) = @_;

    my $clientID = Common::RSApp::GetClientID();
    unless ( exists $gLabelName{$clientID} && exists $gLabelName{$clientID}{$labelID} && defined $gLabelName{$clientID}{$labelID} ) {
        my $dist = RPS::DB::Item::LabelPayee->Lookup( label_payee_id => $labelID );
        assert( $dist, "ERROR - label payee id $labelID is invalid" );

        my $distName = $dist->name;

        # Replace '&' with 'and' - the normal clean function doesn't do that.
        #
        $distName =~ s/ \& / and /g;
        $gLabelName{$clientID}{$labelID} = Common::Util::clean_name($distName);
    }

    return $gLabelName{$clientID}{$labelID};
}

1;
