use std::path::Path;

use eyre::Result;
use inquire::Confirm;
use tracing::instrument;

/// Checks if rust artifacts are present in the given directory.
/// If `dry_run` is enabled, this method will not error if rust
/// artifacts are found.
#[instrument(name = "utils", skip(dir, ci, dry_run))]
pub(crate) fn check_artifacts(dir: &Path, ci: bool, dry_run: bool) -> Result<()> {
    if dry_run {
        return Ok(());
    }
    let mut prompted = false;
    if dir.join("Cargo.toml").exists() {
        tracing::warn!("Rust artifacts detected in the project directory");
        if !Confirm::new("[WARNING] Found conflicting files. Are you sure you wish to proceed?")
            .prompt()?
        {
            println!("Phew, close call... aborting");
            eyre::bail!("User aborted after detecting rust artifacts in the project directory");
        }
        prompted = true;
    }
    if !prompted && dir.join("LICENSE").exists() {
        tracing::warn!("Rust artifacts detected in the project directory");
        if !Confirm::new("[WARNING] Found conflicting files. Are you sure you wish to proceed?")
            .prompt()?
        {
            println!("Phew, close call... aborting");
            eyre::bail!("User aborted after detecting rust artifacts in the project directory");
        }
        prompted = true;
    }
    if !prompted
        && ci
        && dir
            .join(".github")
            .join("workflows")
            .join("ci.yml")
            .exists()
    {
        tracing::warn!("Rust artifacts detected in the project directory");
        if !Confirm::new("[WARNING] Found conflicting files. Are you sure you wish to proceed?")
            .prompt()?
        {
            println!("Phew, close call... aborting");
            eyre::bail!("User aborted after detecting rust artifacts in the project directory");
        }
    }
    Ok(())
}
