//! Implements the `manta restore vcluster` command (and the deprecated
//! `manta migrate vCluster restore` alias that forwards to it).

use anyhow::{Context, Error, bail};

use crate::cli::http_client::MantaClient;
use crate::cli::output::action_result;
use manta_shared::common::app_context::AppContext;

/// Restore cluster configuration from a backup bundle.
#[allow(clippy::too_many_arguments)]
pub async fn exec(
  ctx: &AppContext<'_>,
  token: &str,
  bos_file: Option<&str>,
  cfs_file: Option<&str>,
  hsm_file: Option<&str>,
  ims_file: Option<&str>,
  image_dir: Option<&str>,
  prehook: Option<&str>,
  posthook: Option<&str>,
  overwrite: bool,
  output_opt: Option<&str>,
) -> Result<(), Error> {
  let bos_file_value = bos_file.context("BOS file is required")?;
  let cfs_file_value = cfs_file.context("CFS file is required")?;
  let ims_file_value = ims_file.context("IMS file is required")?;
  let hsm_file_value = hsm_file.context("HSM file is required")?;

  action_result::print(
    &format!(
      "Migrate_restore\n Prehook: {}\n Posthook: {}\n BOS_file: {}\n CFS_file: {}\n IMS_file: {}\n HSM_file: {}",
      prehook.unwrap_or("none"),
      posthook.unwrap_or("none"),
      bos_file_value,
      cfs_file_value,
      ims_file_value,
      hsm_file_value
    ),
    output_opt,
  )?;

  if let Some(prehook_path) = prehook {
    match crate::cli::common::hooks::check_hook_perms(Some(prehook_path)) {
      Ok(_) => {
        tracing::debug!("Pre-hook script exists and is executable.")
      }
      Err(e) => {
        bail!("{e}. File: {prehook_path}");
      }
    }
  }
  if let Some(posthook_path) = posthook {
    match crate::cli::common::hooks::check_hook_perms(Some(posthook_path)) {
      Ok(_) => {
        tracing::debug!("Post-hook script exists and is executable.")
      }
      Err(e) => {
        bail!("{e}. File: {posthook_path}");
      }
    }
  }

  println!();
  if let Some(prehook_path) = prehook {
    println!("Running the pre-hook {prehook_path}");
    match crate::cli::common::hooks::run_hook(prehook) {
      Ok(_code) => {
        tracing::debug!("Pre-hook script completed ok. RT={}", _code)
      }
      Err(_error) => {
        bail!("Pre-hook script failed. Error: {_error}");
      }
    }
  }

  let server_url = ctx.manta_server_url;
  MantaClient::new(server_url, ctx.site_name)?
    .migrate_restore(
      token, bos_file, cfs_file, hsm_file, ims_file, image_dir, overwrite,
    )
    .await?;

  if let Some(posthook_path) = posthook {
    println!("Running the post-hook {posthook_path}");
    match crate::cli::common::hooks::run_hook(posthook) {
      Ok(_code) => {
        tracing::debug!("Post-hook script completed ok. RT={}", _code)
      }
      Err(_error) => {
        bail!("Post-hook script failed. Error: {_error}");
      }
    }
  }

  action_result::print(
    "Done, the image bundle, HSM group, CFS configuration and BOS sessiontemplate have been restored.",
    output_opt,
  )?;

  Ok(())
}
