use std::process::Command;

use cicero_core::command::RunRequiringSuccess;

/// General code quality checks.
#[derive(clap::Args)]
pub struct CheckCli;

impl CheckCli {
    #[tracing::instrument(name="check", skip_all)]
    pub fn run(&self) -> crate::Result<()> {
        test()?;
        clippy()?;
        audit()?;
        Ok(())
    }
}

#[tracing::instrument(skip_all)]
fn test() -> crate::Result<()> {
    Command::new("cargo")
        .arg("test")
        .arg("--all-features")
        .status_requiring_success()?;

    Ok(())
}

#[tracing::instrument(skip_all)]
fn clippy() -> crate::Result<()> {
    crate::internal_commands::CARGO_CLIPPY.command()
        .arg("--workspace")
        .status_requiring_success()?;

    Ok(())
}

#[tracing::instrument(skip_all)]
fn audit() -> crate::Result<()> {
    crate::internal_commands::CARGO_AUDIT.command()
        .current_dir(crate::path::workspace_dir()) //`cargo audit` needs Cargo.lock in working dir
        .status_requiring_success()?;

    Ok(())
}


#[deprecated(since="0.2.0", note="Use `CheckCli` instead.")]
pub struct Check;
