#!/usr/bin/env perl
use v5.34.1;

use strict;
use warnings;

use Rsaves
  qw/read_save check_correct_size get_saves find_current_save_index check_correct_size find_pokemon_substruct change_gender read_pc_storage save_pc_changes enable_eon_ticket save_changes pokemon_set_shiny read_pkm_file_box parse_version_name/;

my $input = $ARGV[0] or die "No input save";
my $output = $ARGV[1] or die "No output save";
my $version = parse_version_name($ARGV[2]) // parse_version_name('ruby');

sub start {
    my ( @saves_raw, $extra );
    ( @saves_raw[ 0, 1 ], $extra ) = read_save($input);
    check_correct_size( @saves_raw, $extra );
    my @saves              = get_saves(@saves_raw, $version);
    my $current_save_index = find_current_save_index(@saves);
    my $save               = $saves[$current_save_index];
    my $pc = read_pc_storage($save);

    for my $i ( 0 .. 13 ) {
        for my $j ( 0 .. 29 ) {
            my $pokemon      = $pc->{boxes}[$i][$j];
            my $substructures = $pokemon->{substructures};
            my $substruct0   = find_pokemon_substruct( $substructures, 0 );
            if ( $substruct0->{species} == 0 ) {
                # Celebi because it is almost unobtainable.
                $pc->{boxes}[$i][$j] = read_pkm_file_box(
                    'firered event mons/251 - CELEBI - B434BCEB935C.pk3');
                last;
            }
        }
    }
    save_pc_changes( $save, $pc );
    save_changes( @saves, $extra, $output );
}

start;
