package Common::RSDB::Client;

use strict;
use warnings;
use open ':std', ':encoding(UTF-8)';

use base 'Class::Accessor';

use Class::Load(qw/:all/);

use lib '/app/tools/common/lib';
use Common::RSDB;

__PACKAGE__->mk_accessors(qw/clientId client_cache dbx dbh dbname nocache server table_cache url username password/);

sub new {
    my $class    = shift;
    my $clientId = shift;
    my $nocache  = shift || 0;
    die __PACKAGE__ . '::new requires a clientId' if not defined $clientId;
    my $self = { 'clientId' => $clientId };
    bless $self, $class;
    $self->_init($nocache);
    $self;
}

sub _init {
    my $self    = shift;
    my $nocache = shift;

    my $clientId = $self->clientId;

    ## set DB connection methods
    $self->dbname( $Common::RSDB::CLIENT_DB{$clientId}{db_name} );
    $self->server( $Common::RSDB::CLIENT_DB{$clientId}{server} );
    $self->username( $Common::RSDB::CLIENT_DB{$clientId}{username} );
    $self->password( $Common::RSDB::CLIENT_DB{$clientId}{password} );

    ## set DB methods
    if ( not $self->is_test and not $nocache ) {
        load_class('Common::DB::Schema');
        my $client = $self->get_client;
        $self->dbx( Common::DB::Schema->getdbx( $self->server ) );
        $self->dbh( $self->dbx->getdbh );
        $self->dbh->do( "USE " . $self->dbname );
    }
}

sub get_client {
    my $self = shift;
    if ( not defined $self->client_cache ) {
        load_class('Common::DB::Schema');
        my $dbx    = Common::DB::Schema->getdbx('rscommon');
        my $client = $dbx->resultset('RSCOMMON::Client')->find( $self->clientId );
        if ( defined $client ) {
            my $url = ".royaltyshare.com";
            if ( defined $client->web_alias ) {
                $client->url( $client->web_alias . $url );
            } else {
                $client->url( $client->client_name_clean . $url );
            }
        }
        $self->client_cache($client);
    }
    $self->client_cache;
}

sub getdbx {
    my $self = shift;
    my $dbx;
    if ( defined $self->dbx ) {
        $dbx = $self->dbx;
    } else {
        load_class('Common::DB::Schema');
        $dbx = Common::DB::Schema->getdbx( $self->server );
        my $dbh = $dbx->getdbh;
        $dbh->do( "USE " . $self->dbname );
        $self->dbh($dbh);
    }
    $dbx;
}

sub get_tables {
    my $self = shift;
    if ( defined $self->table_cache ) {
        $self->table_cache;
    } else {
        my $dbx = defined $self->dbx ? $self->dbx : $self->getdbx;
        my $tables = $dbx->getdbh->selectcol_arrayref('show tables');
        $self->table_cache($tables);
    }
    $self->table_cache;
}

sub is_test {
    my $self = shift;
    my $testIds;
    map { $testIds->{$_}++ } ( 60, 202, 287, 288, 302, 310, 331, 374, 777, 999, 2000000001, 2000000002, 2000000003 );
    exists $testIds->{ $self->clientId } ? 1 : 0;
}

sub client_type {
    my $self = shift;
    my $type;
    if ( $self->dbname =~ m/^c_/i ) {
        $type = 'RPS';
    } elsif ( $self->dbname =~ m/^bp_/i ) {
        $type = 'DTMV';
    } else {
        $type = 'Other';
    }
    $type;
}

1;
