#!/usr/bin/perl

=encoding UTF-8
=cut

=head1 DESCRIPTION

=cut

# common modules
use strict;
use warnings FATAL => 'all';
use feature 'say';
use utf8;
use open qw(:std :utf8);

use Carp;
use LWP::UserAgent;
use JSON::PP;
use Path::Tiny;
use DDP;

use lib::abs qw(
    ../lib
);
use Utils;
use PiSecrets;

# global vars

# subs
sub get_tjson {
    my $file_name = lib::abs::path('../translations/') . '/ru.json';
    my $data = decode_json path($file_name)->slurp_raw();

    my $keys = {};

    foreach my $k (keys %{$data}) {
        $keys->{$k} = {
            translations => {
                ru => {
                    form => $k,
                    status => 'approved',
                }
            }
        }
    }

    my $tjson = {
        keysets => {
            form => {
                keys => $keys,
            }
        }
    };

    return $tjson;
}

# main
sub main {

    my $tjson = get_tjson();

    my $lwp = LWP::UserAgent->new(
        default_headers => HTTP::Headers->new(
            Content_Type => 'multipart/form-data',
            'Authorization' => 'OAuth ' . get_secret('tanker-pi2-token'),
        ),
        ssl_opts => {verify_hostname => 0,},
    );

    my $response = $lwp->post(
        'https://tanker-api.yandex-team.ru/keysets/onlyadd/',
        Content => [
            file => [{
                filename => 'file.tjson',
                content  => encode_json($tjson),
            }],
            'project-id' => 'pi2',
            'keyset-id' => 'form',
        ]
    );
    p $response;
}
main();
__END__
