#!/usr/bin/perl

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

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

use lib '/var/app/orchard/collab/jgetter/perllib';
use Orchard::Session;
use Orchard::DB::Connect;
use Orchard::ETL::Extract::CSV;
use Orchard::ETL::Extract::SpreadSheet;

my $file = shift @ARGV;
my $db   = 'abacusqa';

logMessage( 'info', "Connecting to $db" );
my $dsn = Orchard::DB::Connect->dsn($db);
my $dbh;
if ( defined $dsn->{'ssh'} ) {
    my $ssh = $dsn->{'ssh'};
    $ssh->{'net'}->spawn( $ssh->{'tunnel'} );
    sleep 1;
}
$dbh = DBI->connect( @{ $dsn->{'conn'} } ) || die "Conn: $DBI::errstr";

my $sth = $dbh->prepare(
    qq{
select
	contract_term_id,
	contract_term_name,
	attachments
from contract_term
where created_by='knr_sax_ingestion'
}
);
$sth->execute;
my $dbrows;
while ( my $row = $sth->fetchrow_arrayref() ) {
    my ($id, $name, $attachments ) = @{$row};
    my $attachment_array = JSON::XS::decode_json($attachments);
	map { print STDERR "$id,$name,$_\n" } sort @{$attachment_array};
}
