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

package RPS::ArtistRoyalty::Fast::Static::CrossedAlbumContracts;

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 Common::RSApp;
use RPS::DB::Item::AlbumContract;
use RPS::DB::Item::TrackContract;
use RPS::DB::Item::Track;

use Data::Dumper;
use DB_File;

use Common::Log;

use constant kMinPayment      => 0;
use constant kPreviousBalance => 1;

use base 'RPS::ArtistRoyalty::Fast::Static';

sub FileName            { 'crossed_album_contracts' }
sub CacheQueryTableList { "'album_contract','track_contract'" }

sub _Create {
    my ( $class, $filename, $payorID ) = @_;

    # We're going to need to know which of these album/contracts are crossed.
    # This data is found in the track_contract and album_contract tables, which we don't query here.
    # I think I'm going to do a couple of queries and build a big ole' hash table.
    # !!! Building this with the somewhat slow interface, but I don't imagine this is going to take that long to run
    #
    my %crossedMap;
    my $albumContracts = RPS::DB::Item::AlbumContract->GetCrossCollateralized();
    while ( my $albumContract = $albumContracts->next() ) {
        $crossedMap{ $albumContract->album_id() }{ $albumContract->artist_contract_id() } = 1;
    }

    my $trackContracts = RPS::DB::Item::TrackContract->GetCrossCollateralized();
    while ( my $trackContract = $trackContracts->next() ) {
        my $track = RPS::DB::Item::Track->Lookup( track_id => $trackContract->track_id() );
        $crossedMap{ $track->album_id() }{ $trackContract->artist_contract_id() } = 1;
    }

    $Data::Dumper::Terse = 1;
    open OUTFH, "> $filename" or die "ERROR: $!";
    print OUTFH Dumper( \%crossedMap );
    close OUTFH;
}

sub GetCrossedAlbumContracts {
    my ( $class, $dataPath ) = @_;

    my $filename = "$dataPath/" . $class->FileName();

    my $data = do $filename or die "ERROR: $!";

    return $data;
}

1;

