package Common::DB::Deploy::ClientManager;

use strict;
use warnings;
use Carp;

sub new {
    my ($class, %args) = @_;

    my $self = bless {
        oLogger     => $args{oLogger},
        db_dsn_list => [],
    }, $class;

    $self->_init(\%args);
    return $self;
}
sub logger {
    shift->{oLogger};
}

sub getListOfDSN {
    shift->{db_dsn_list};
}

sub _init {
    my ($self, $hOption) = @_;

    $self->logger->debug("Preparing the list of client databases.");
    unless ( exists $hOption->{client_db_list} && ref $hOption->{client_db_list} eq 'HASH') {
        $self->logger->error("The 'client_db_list' list is corrupted.");
        exit 1;
    }

    my $hClientsDB = $hOption->{client_db_list};
    my $cSort = sub { $a->{server} cmp $b->{server} || $a->{db_name} cmp $b->{db_name} };
    foreach my $hData ( sort $cSort values %$hClientsDB ) {
        next unless $hData->{password};

        # filling the DSN list with available clients
        push @{ $self->{db_dsn_list} }, {
            host     => $hData->{server},
            database => $hData->{db_name},
            user     => $hData->{username},
            pass     => $hData->{password},
        };
    }

    return 1;
}


1;