#!/usr/bin/perl

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

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

use lib '/var/app/orchard/collab/jgetter/perllib';
use Orchard::Session;

my $csv = FileHandle->new( 'dyn2csv.csv', 'w' );

my $header = [ (qw/user_id_type user_params file_type period_ids status s3_path/) ];
my $line   = csv_build( @{$header} );
$csv->print("$line\n");

my $ttl  = 0;
my $next = shift @ARGV || 'first';
while ( defined $next ) {
    my $cmd = "aws dynamodb scan --region us-east-1 --table prod_accounting_statement_export --max-items 4096";
    $cmd .= " --starting-token $next" if $next ne 'first';
    my $out = runcmd($cmd);
    if ( $out->[2] ) {
        logMessage( 'error', $out->[1] );
        exit;
    }
    my $href = JSON::XS::decode_json( encode( "utf8", $out->[0] ) );
    $next = $href->{'NextToken'};
    my $items = $href->{'Items'};
    foreach my $item ( @{$items} ) {
        my $data;
        foreach my $k ( @{$header} ) {
            push @{$data}, $item->{$k}->{'S'};
        }
        my $line = csv_build( @{$data} );
        $csv->print("$line\n");
    }
    $ttl += scalar @{$items};
    logMessage( 'info', "total: $ttl token: $next" );
    last if not defined $next;
}
