use strict;

FillInProductTitleClean->start();

package FillInProductTitleClean;
use lib '/app/tools/common/lib';
use lib '/app/tools/rps/lib';

use Common::Log;
use Common::Util;
use Common::Client;
use Common::RSApp;
use RPS::DB::Item::Product;

use Common::Util qw(clean_name_catalog);

use base 'Common::Script';

sub _process {
    my $self = shift;

    my $clientName = Common::Client::Current()->ClientName();
    print "$clientName\n";

    my $dbo = Common::RSApp::GetClientDB();

    # Loop over all the products.
    my $products = RPS::DB::Item::Product->GetAll();

    my $updatedCount = 0;

    while ( my $product = $products->next() ) {

        # Digital track products don't have titles, so skip them.
        if ( $product->product_type_id == RPS::DB::Item::Product::kProductTypeDigitalTrack ) {
            next();
        }

        # In theory, it should be safe to do this for all products.
        # But, we're only going to set the title clean if it's NULL.
        if ( $product->title && !$product->title_clean ) {
            $product->title_clean( clean_name_catalog( $product->title ) );
            $product->save();
            $updatedCount++;
        }
    }

    print STDERR "Updated $updatedCount products\n";

}

sub _getProductionClientIDs {
    return Common::RSDB::GetAllRPSClientIDs();
}

sub _getLocalClientIDs {
    return 202;    # RS Test
}

1;
