use strict;

use Data::Dumper;

use lib '/app/tools/common/lib';
use Common::RSApp;
use Common::RSDB;
use Common::Log;
use Common::DB::Item::TableChangeLog;
use Common::DB::Item::Client;
use Common::DB::Item::ClientType;
use Common::DB::Item::ClientCMIDMap;

use lib '/app/tools/rps/lib';
use RPS::DB::Item::Track;
use RPS::DB::Item::Album;
use RPS::DB::Item::Product;
use RPS::DB::Item::Master;
use RPS::DB::Item::Song;
use RPS::DB::Item::Artist;
use RPS::DB::Item::Composer;
use RPS::DB::Item::Label;
use RPS::DB::Item::ProductTrack;

use constant kCMClientID => 112;

my %gReport;

my $singleton = Common::RSApp->new( clientID => kCMClientID );

# Strategy:
# Loop through all the CM products.
# Get the corresponding album or track title, and artist name.
# Find the id mapping
# -- Complain if there isn't one!
# Look up the album, track, and artist title in the original database
# -- If it's a rps02 client, anyway...

my $allCMProducts = RPS::DB::Item::Product->GetAll();
while ( my $cmProduct = $allCMProducts->next() ) {
    my $productMap = Common::DB::Item::ClientCMIDMap->Lookup(
        id_type    => 'product_id',
        cm_site_id => $cmProduct->product_id,
    );

    if ( !$productMap ) {
        push @{ $gReport{missingProductMap} }, $cmProduct;
        next;
    }

    my $dbData = $Common::RSDB::CLIENT_DB{ $productMap->client_id };
    next unless ( 'rps02' eq $dbData->{host_id} );

    my $cmProductData = getProductData($cmProduct);
    my $clientProductData = getClientProductData( $productMap->client_site_id, $productMap->client_id );
    if ( !$clientProductData ) {
        push @{ $gReport{noClientProduct} }, { cm => $cmProductData, product => $cmProduct, map => $productMap };
        next;
    }

    if (   $cmProductData->{productType} != $clientProductData->{productType}
        || $cmProductData->{title} ne $clientProductData->{title}
        || $cmProductData->{artistName} ne $cmProductData->{artistName} ) {
        push @{ $gReport{noMatch} }, { cm => $cmProductData, client => $clientProductData, product => $cmProduct, map => $productMap };
        next;
    }

    #    push @{$gReport{good}}, $cmProduct;
}

my $summary;
foreach my $key ( keys %gReport ) {
    my $array = $gReport{$key};
    $summary .= "$key: " . scalar @$array . "  ";
}

print "$summary\n";
print Dumper( \%gReport ) . "\n";

sub getClientProductData {
    my ( $productID, $clientID ) = @_;

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

    my $product = RPS::DB::Item::Product->Lookup( product_id => $productID );
    return undef unless $product;
    return getProductData($product);
}

sub getProductData {
    my ($product) = @_;

    my %data;

    $data{productID}   = $product->product_id;
    $data{productType} = $product->product_type_id;
    $data{assetID}     = $product->asset_id;

    my $artistID;
    if ( RPS::DB::Item::Product::kProductTypeDigitalTrack eq $product->product_type_id ) {
        my $track = RPS::DB::Item::Track->Lookup( track_id => $product->asset_id );
        $data{title} = $track->title;
        $artistID = $track->artist_id;
    } else {
        my $album = RPS::DB::Item::Album->Lookup( album_id => $product->asset_id );
        $data{title} = $album->title;
        $artistID = $album->artist_id;
    }

    my $artist = RPS::DB::Item::Artist->Lookup( artist_id => $artistID );
    $data{artistName} = $artist->name;

    return \%data;
}
