#!/usr/bin/perl

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

use JSON::XS;
use FileHandle;
use Text::CSV::Easy(qw/csv_build csv_parse/);

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

my $dbx     = getdbx('admindb');
my $clients = Common::RSDB::StaticList::allClientDB;
foreach my $cid ( sort { $a <=> $b } keys %{$clients} ) {
    my $href     = $clients->{$cid};
    my $inactive = defined $href->{'inactive'} ? 1 : 0;
    my $row      = $dbx->resultset('Report::DDb')->search( { 'client_id' => $cid } )->first;
    if ( defined $row ) {
        ## check status
        if ( $row->db_status eq 'Active' and $inactive ) {
            logMessage( 'error', "$cid " . $row->db_name . " is wrong!" );
        } elsif ($inactive) {
            logMessage( 'info', "$cid " . $row->db_name . " status [" . $row->db_status . "] is [inactive=1] in RSDB" );
        }

        ## check host
        my $dbServer = $row->host->aws_host->hostname;
        if ( $dbServer ne $href->{'server'} ) {
            logMessage( 'error', "$cid " . $row->db_name ." $dbServer database host is not RSDB host " . $href->{'server'} );
        }
    } else {
        logMessage( 'warn', "$cid $href->{'db_name'} is not in the admin DB and is [inactive=1] in RSDB" );
    }
}
