use anyhow::Context as _;
use reedline_repl_rs::clap::ArgMatches;
use s_macro::s;
pub use wildland_cargo_lib::api::CargoLib;
use crate::{match_context, Context};
#[tracing::instrument(level = "trace", err(Debug), ret, skip(args, ctx))]
pub(crate) fn h_evs_request(args: ArgMatches, ctx: &mut Context) -> anyhow::Result<Option<String>> {
let path = args
.get_one::<String>("templatename")
.context("template name to save template not provided")?;
let email = args
.get_one::<String>("email")
.context("email for the account")?;
let userapi = match_context!(ctx, cargo_user => Some(_))?;
let handle = userapi
.request_free_tier_storage(email.to_string())
.map_err(|e| anyhow::anyhow!("failed to request storage from EVS {e}"))?;
println!("process of requesting the storage started, please check your email for the confirmation link");
println!("once you get the code, insert it here:");
let mut code = String::new();
std::io::stdin().read_line(&mut code)?;
let code = code.trim();
let template = handle
.verify_email(code.to_string())
.map_err(|e| anyhow::anyhow!("failed to verify the email {e}"))?;
let template = serde_json::to_string_pretty(&template)
.map_err(|e| anyhow::anyhow!("failed to serialize the template {e}"))?;
std::fs::write(path, template)?;
Ok(Some(s!("Ok: Storage is written to {path}")))
}