package Common::DB::Export::Redshift;

use strict;
use warnings;

use JSON::XS;
use FileHandle;
use File::Basename;
use File::Find::Rule;

use lib '/app/tools/common/lib';
use Common::Session;
use Common::DB::Export::Parallel;

use base 'Common::DB::Export';

sub _init {
    my $self = shift;
    $self->SUPER::_init();
    $self->directory->parent('schema-exports');
    $self->exporter( Common::DB::Export::Parallel->new( $self->database ) );
    $self;
}

sub run_redshift_sql {
    my $self = shift;
    my $sql  = shift;
    my $rsdbh;
    eval {
        local $SIG{__WARN__} = sub {
            my $warn = shift;
            $warn =~ m/.*?:\s+(.*)\n/;
            logMessage( 'info2', $1 );
        };
        $rsdbh = Common::Session::getdbh('redshift');
        $rsdbh->do($sql);
    };
    if ( $@ or $rsdbh->err ) {
        logMessage( 'error2', "$@ - " . $rsdbh->errstr );
        return 1;
    }
    $rsdbh->disconnect;
    return 0;
}

sub load_redshift {
    my $self  = shift;
    my $table = shift || 'sale_stage';

    my $client = $self->database;
    logMessage( 'info', 'COPY [' . $client->client_id . ']: ' . $client->db_name . '.' . $table . ' to Redshift' );
    my $copysql = $self->redshift_copy_sql($table);
    $self->run_redshift_sql($copysql);
}

sub create_manifest {
    my $self = shift;

    my $dir = $self->directory;
    my $find;
    if ( $self->exporter->compress ) {
        $find = "*.gz";
    } else {
        $find = "*.txt";
    }

    my $manifest;
    foreach my $file ( @{ $dir->sysfiles($find) } ) {
        my $s3path = $dir->remote . "/" . $file->name;
        push @{ $manifest->{'entries'} }, { 'url' => $s3path, 'mandatory' => $Types::Serialiser::true };
    }
    my $json = JSON::XS::encode_json($manifest);

    my $mfile = $dir->local . "/manifest.json";
    logMessage( 'info2', "Creating manifest file $mfile" );
    my $fh = FileHandle->new( $mfile, "w" ) or die $!;
    $fh->print($json);
    $fh->close;

    $manifest;

}

sub redshift_copy_sql {
    my $self  = shift;
    my $table = shift;

    my $schema   = $self->redshift_schema;
    my $manifest = $self->directory->remote . '/manifest.json';
    my $sql      = (
        qq{
COPY $schema.$table FROM '$manifest'
IAM_ROLE 'arn:aws:iam::290365479392:role/service-role/AmazonRedshift-CommandsAccessRole-20220212T094750' 
FORMAT AS DELIMITER AS '\t' 
ENCODING AS UTF8
REMOVEQUOTES ESCAPE
ACCEPTINVCHARS
ACCEPTANYDATE
REGION AS 'us-west-2' 
TRUNCATECOLUMNS 
MANIFEST
}
    );
    if ( $self->exporter->compress ) {
        $sql .= "GZIP";
    }
    $sql;
}

sub send_to_s3 {
    my $self = shift;

    my $local  = $self->directory->local;
    my $remote = $self->directory->remote;

    my $out = runlogcmd("aws s3 cp --recursive $local/ $remote/");
    if ( $out->[2] ) {
        logMessage( 'error', $out->[1] );
        exit 1;
    } else {

        #my $lines = [ split "\n", $out->[0] ];
        #map { $_ =~ s/.*\r//g } @{$lines};
        #logMessage( 'info', $lines, 'S3OUT' );
    }

}

1;
