use strict;
use Data::Dumper;

use lib '/app/tools/common/lib';
use Common::RSApp;
use Common::RSDB;
use Common::Client;
use Common::DB::Item::Client;

use lib '/app/tools/rps/lib';
use RPS::DB::Item::PublisherAccount;
use RPS::DB::Item::TrackLicense;
use RPS::DB::Item::PublisherTrackCrossedLicenseAccount;
use RPS::DB::Item::FinanceAccount;
use RPS::DB::Item::FinanceTransaction;

my $app = Common::RSApp->new( clientID => 0 );

# We want to run this for all 'foreign' clients present on this host.
#
my @localClientIDs = _getLocalNonUSClientIDs();

foreach my $clientID (@localClientIDs) {
    _convertClient($clientID);
}

# ---------------------------------------------------------------

sub _convertClient {
    my ($clientID) = @_;

    print "Converting client $clientID : " . Common::RSDB::ClientIDToDBName($clientID) . "\n";

    my $tempApp = Common::RSApp->new_temporary( clientID => $clientID );

    # We'll make a list of accounts to _exclude_ from the conversion.
    # (!!!) Might want to sanity check these to make sure they are already 'USD'.
    #
    my %excludeIDs;
    my $publisherAccounts = RPS::DB::Item::PublisherAccount->GetAll();
    while ( my $pubAccount = $publisherAccounts->next() ) {
        $excludeIDs{ $pubAccount->finance_account_id } = 1;
    }

    my $trackLicenses = RPS::DB::Item::TrackLicense->GetAll();
    while ( my $tl = $trackLicenses->next() ) {
        $excludeIDs{ $tl->finance_account_id } = 1;
    }

    my $crossedAccounts = RPS::DB::Item::PublisherTrackCrossedLicenseAccount->GetAll();
    while ( my $ca = $crossedAccounts->next() ) {
        $excludeIDs{ $ca->finance_account_id } = 1;
    }

    # What is our 'base' currency, anyway?
    #
    my $currencyCode = Common::Client::Current()->Locale()->currencyFormat()->currencyCode();

    # Now we iterate over all the accounts, skipping the mechanical-related ones.
    #
    my $allAccounts = RPS::DB::Item::FinanceAccount->GetAll();
    while ( my $account = $allAccounts->next() ) {
        my $accountID = $account->finance_account_id;
        if ( $excludeIDs{$accountID} ) {
            die "ERROR - expected USD currency code for account $accountID!" unless $account->currency_code() eq 'USD';
            next;
        }

        $account->currency_code($currencyCode);
        $account->save();

        # Update all the transactions in a batch... We'll just slime this in there.
        #
        my $dbo = Common::RSApp::GetClientDB();
        $dbo->DoCmd(
            "UPDATE finance_transaction SET currency_code=" . $dbo->DBQuote($currencyCode) . " WHERE finance_account_id=$accountID" );
    }
}

sub _getLocalClientIDs {
    my $localClients = `/app/tools/rps/bin/db/list_client_ids.pl`;
    chomp $localClients;
    my @localIDs = split( /\s/, $localClients );
    return @localIDs;
}

sub _getLocalNonUSClientIDs {
    my @localIDs = _getLocalClientIDs();

    my @foreignIDs;
    foreach my $id (@localIDs) {
        my $clientData = Common::DB::Item::Client->Lookup( client_id => $id );
        next unless $clientData;

        next if $clientData->country_code() eq 'US';
        push @foreignIDs, $id;
    }

    return @foreignIDs;
}
