#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
#------------------------------------------------------------
package Distribution::Job::Delivery;

use strict;
use warnings;

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

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

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

use base 'Job::Job';

sub generateLogBasePath {
    return Common::RSApp::GetConfig( job => 'windows_log_dir' );
}

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

    assert( $self->getCommandLineArg('distProcessID') );
    assert( $self->getCommandLineArg('serviceID') );

    my $clientID = $self->clientID();

    my $perl    = Common::RSApp::GetConfig( distribution => 'windows_perl' );
    my $command = Common::RSApp::GetConfig( distribution => 'job_delivery' );

    # Fetch the service ID associated with this distribution job.
    # !!! This assumes that there will be only _1_ 'product_distribution' record
    # !!! containing that distribution_job_id.

    my $commandLine =
        "$perl $command " . " -d "
      . " -c $clientID" . " -s "
      . $self->getCommandLineArg('serviceID') . " -j "
      . $self->getCommandLineArg('distProcessID')
      . " -f \""
      . $self->getCommandLineArg('sourceDir') . "\""
      . " -p \""
      . $self->getCommandLineArg('packageDir') . "\"";

    return $commandLine;
}

# !!! I'm not sure we really want this.
# !!! We don't _need_, technically, to hard-code the hostname.
# !!!
#sub _defaultHostname{ Common::RSApp::GetConfig( 'distribution', 'cm_host' ); }

sub _defaultClass    { return 'Delivery' }
sub _defaultPriority { return Job::Job::kPriorityNormal; }

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

    my $serviceID = $self->getCommandLineArg('serviceID');
    assert( $serviceID, "No serviceID specified" );

    my $service = Distribution::DB::Item::DistributionService->Lookup( service_id => $serviceID );
    assert( $service, "Unknown Service" );

    return $service->service_name_clean();
}

sub enqueue {
    my ( $self, %args ) = @_;

    my $distProcessID = $self->getCommandLineArg('distProcessID') || $args{distProcessID};
    assert($distProcessID);

    delete $args{distProcessID};

    $args{command_line} = $self->{CommandLine};
    $self->SUPER::enqueue(%args);

    # The inherited enqueue method will re-initialize this object with database state.
    # This means that we should now magically have a jobID.

    my $dist_job = Distribution::DB::Item::DistributionJob->Create();

    $dist_job->distribution_process_id($distProcessID);
    $dist_job->job_id( $self->id() );

    $dist_job->save();
}

###
#   Notification
#
#   On failure we want to send email
###

sub _notifyFailureRecipients { 'bill@royaltyshare.com' }

sub _notifyFailureSubject {
    my $self = shift;
    return "Delivery Failure (" . Common::RSApp::GetHostname . ")";
}

sub _notifyFailureBody {
    my $self      = shift;
    my $logDir    = Common::RSApp::GetConfig( job => 'windows_log_dir' );
    my $log       = $logDir . "/" . $self->logFile;
    my $clientID  = Common::RSApp::GetClientID;
    my $distJobID = 1;

    my $supportURL = $self->_getSupportURL();
    my $jobURL     = "$supportURL/job_queue?c=show_job&JobID=" . $self->id();
    my $distURL    = "$supportURL/job_queue?c=show_process_detail&DistributionJobID=$distJobID";
    my $requeueURL = "$supportURL/job_queue?c=requeue&DistributionJobID=$distJobID&ClientID=$clientID";

    my $message = "Command:\t" . $self->getCommandLine() . "\n";

    $message .= "Exit Code:\t" . $self->exitCode() . "\n";
    $message .= "Log File:\t$log\n\n";
    $message .= "Job Info:\t$supportURL\n";
    $message .= "Dist Info:\t$distURL\n";
    $message .= "Requeue:\t$requeueURL\n";

    return $message;
}
###
1;    #
###
