package BookPub::DB::Item::CountryTerritory;

use strict;
use warnings;

use Data::Dumper;

use lib '/app/tools/common/lib';
use Common::Assert;
use Common::DB::ItemCollection;
use Common::DB::Item;
use Common::DB::Item::CountryTerritory;
use base 'Common::DB::Item';

use constant kTable => 'country_territory';
use constant kDB    => Common::DB::Item::kClientDB;

# ...  So, at the moment, we want to _lazily_ copy records from the RSCOMMON version of this table.
# ...  This requires an overload of Lookup.

sub Lookup {
    my $class  = shift;
    my %params = @_;

    my $localCopy = $class->SUPER::Lookup(%params);
    if ( !$localCopy ) {
        my $commonCopy = Common::DB::Item::CountryTerritory->Lookup(%params);
        return undef unless $commonCopy;

        $localCopy = $class->Create(
            country_territory_id => $commonCopy->country_territory_id(),
            country_id           => $commonCopy->country_id(),
            code                 => $commonCopy->code(),
            name                 => $commonCopy->name(),
        );
        $localCopy->save();
    }

    return $localCopy;
}

1;
