#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package RPS::ArtistRoyalty::Job::RunArtistRoyalties;

use strict;
use warnings;

use Sys::Hostname;

use lib '/app/tools/common/lib';
use Common::Util;
use Common::Log;
use Common::Assert;
use Common::RSApp;
use Common::Config;
use Common::SlackURLs;
use Common::DB::Item::Client;

use lib '/app/tools/rps/lib';
use RPS::DB::Item::ArtistRoyaltyRun;
use RPS::Config;
use RPS::ArtistRoyalty::Job::Base;
use RPS::RoyaltyRun::Status;

use lib '/app/tools/job/lib';
use Job::Job;
use Job::DB::Item::Job;

use base 'Job::Job';

# If there is a failure then we need to update the royalty run status
sub _onFailure {
    my $self   = shift;
    my %params = $self->_getXMLParams();
    my $runID  = $params{runID};

    assert( $params{runID}, "Unknown run id, can't update status" );

    my $obj = RPS::DB::Item::ArtistRoyaltyRun->Lookup( artist_royalty_run_id => $params{runID} );
    $obj->status(RPS::RoyaltyRun::Status::kError);
    $obj->end_time(Common::DB::Item::kDateTimeNow);
    $obj->save();
}

# This is the method that gets called on the host that
# will _actually_ be running the job.
#
sub _generateCommandLine {
    my ($self) = @_;

    my $runID = $self->getCommandLineArg('runID');
    assert($runID);

    #    my $clientID = $self->getCommandLineArg('clientID');
    my $clientID = $self->clientID();
    assert($clientID);

    my $logLevel = $self->getCommandLineArg('logLevel');
    $logLevel = 1 unless $logLevel;

    # !!! Use the 'config' system to get the path to the royalty scripts.
    #
    #    my $rpsConfig = RPS::Config->new();
    #    my $scriptPath = $rpsConfig->get('royalty_script_dir');
    my $scriptPath = '/app/tools/rps/lib/RPS/ArtistRoyalty/Fast/bin';

    # !!! Note that this is a linux-only command line.
    # !!! If we end up wanting to run these on windows <shudder>, we'd have
    # !!! to create a 'Win32' subclass and override this method.
    # !!! Actually, we'd need to create a 'linux' subclass as well.
    #
    #    my $commandLine = "nice $scriptPath/run_artist_royalties.pl -c $clientID -r $runID -V $logLevel";
    my $commandLine = "nice $scriptPath/run.pl -c $clientID -r $runID -V $logLevel";

    return $commandLine;
}

# This will just be the path in which the file will live.
#
sub generateLogBasePath {
    my ($self) = @_;

    my $clientID = $self->clientID();
    assert($clientID);
    my $runID = $self->getCommandLineArg('runID');

    return ( ref($self) )->LogFilePath( $clientID, $runID );
}

sub _defaultClass {
    return 'RunController';
}

sub _defaultSubclass {
    return 'ArtistRoyalties';
}

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

    my $runID = $self->getCommandLineArg('runID');
    assert($runID);

    my $clientID = Common::RSApp::GetClientID();
    assert($clientID);

    my $clientData = Common::DB::Item::Client->Lookup( client_id => $clientID );
    return $clientData->client_name_clean . ".artist.$runID";
}

sub _defaultPriority {
    my $self     = shift;
    my $clientID = Common::RSApp::GetClientID();
    my $count    = Job::DB::Item::Job->GetActiveJobCount( $clientID, $self->_defaultSubclass() );

    #    return Job::Job::kPriorityNormal - $count * 50;
    return Job::Job::kPriorityLow;
}

sub LogFilePath {
    my ( $class, $clientID, $runID ) = @_;
    assert($clientID);
    assert($runID);

    my $rpsConfig = RPS::Config->new();
    my $basePath  = $rpsConfig->get('royalty_log_dir');

    my $clientData = Common::DB::Item::Client->Lookup( client_id => $clientID );

    $basePath .= '/' . $clientData->client_name_clean . "/artist/$runID";

    return $basePath;
}


### Slack notification config
#
# This endpoint currently supports three variables:
# ClientName, JobURL, and JobType
# The first two are added in by the base class,
# so we just need to set JobType here.

sub _notifyFailureVariables {
    my $self      = shift;
    my %variables = ( JobType => "Artist Royalty Run" );
    return %variables;
}

sub _notifyFailureURL {
    my $self = shift;
    return Common::SlackURLs::kRoyaltyRunErrors;
}

sub _notifyFailureTestURL {
    my $self = shift;
    return Common::SlackURLs::kRoyaltyRunErrorsTest;
}

###
1;    #
###
