package BookPub::DB::Item::Country;

use strict;
use warnings;

use Data::Dumper;

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

use constant kTable => 'country';
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.
# !!! At the moment, I don't see any reason to re-invent the Common::Country interface.

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

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

        $localCopy = $class->Create(
            country_id => $commonCopy->country_id(),
            alpha2     => $commonCopy->alpha2(),
            alpha3     => $commonCopy->alpha3(),
            numeric3   => $commonCopy->numeric3(),
            name       => $commonCopy->name(),
        );
        $localCopy->save();
    }

    return $localCopy;
}

1;
