use clap::{Parser, ValueEnum};

#[derive(Clone, Debug, ValueEnum)]
#[value(rename_all = "PascalCase")]
enum Command {
    CheckMigration,
    SanityCheck,
    Snapshot,
}

#[derive(Clone, Derive, ValueEnum)]
#[value(rename_all = "PascalCase")]
enum Runtime {
    Casper,
}

#[derive(Debug)]
struct Cli {
    #[arg(long, short, default_value = "wss://127.0.0.1::443")]
    uri: String,

    #[arg(long, short, ignore_case = true, value_enum, default_value_t = Runtime::Casper)]
    runtime: String,

    #[arg(long, short, ignore_case = true, value_enum, default_value_t = Command::SanityCheck)]
    command: Command,

    #[arg(long, short)]
    snapshot_limit: Option<usize>,
}

#[tokio::main]
async fn main() {
    let options = Cli::parse();
    sp_tracing::try_init_simple();

    log::info!(
        target: "remote-ext-tests",
        "using runtime {:?} / command: {:?}",
        options.runtime,
        options.command,
    );

    use pallet_bags_list_remote_tests::*;
    match options.runtime {
        Runtime::Casper => sp_core::crypto::set_default_ss58_version(
            <casper_runtime::Runtime as frame_system::Config>::SS58Prefix::get()
                .try_into()
                .unwrap(),
        ),
    };

    match options.runtime {
        Runtime::Casper => {
            use casper_runtime::{Block, Runtime};
            use casper_runtime_constants::currency::CSPR;

            match options.command {
                (Command::CheckMigration) => {
                    migration::execute::<Runtime, Block>(CSPR as u64, "CSPR", options.uri.clone())
                        .await;
                }
                (Command::SanityCheck) => {
                    try_state::execute::<Runtime, Block>(CSPR as u64, "CSPR", options.uri.clone())
                        .await;
                }
                (Command::Snapshot) => {
                    snapshot::execute::<Runtime, Block>(
                        options.snapshot_limit,
                        CSPR.try_into().unwrap(),
                        options.uri.clone(),
                    )
                    .await;
                }
            }
        }
        _ => Err("Wrong runtime was used"),
    }
}
