// SPDX-License-Identifier: MIT
// Copyright 2026 John Harwell, All rights reserved.
/*!
 * Implementation of the clean command.
 */

// Imports
use anyhow;
use clap;
use log::debug;

use crate::cmake;
use crate::preset;
use crate::runner;

// Types
#[derive(clap::Parser, Debug)]
pub struct CleanArgs {
    /// Removes the preset's binaryDir entirely (rm -rf).  Requires the build
    /// directory to exist; exits with an error otherwise.
    #[arg(long)]
    pub all: bool,
}

// Traits

// Implementation

// Public API

pub fn run(ctx: &runner::Context, args: CleanArgs) -> anyhow::Result<()> {
    preset::ensure_project_root(ctx)?;

    debug!("Begin");

    let preset = preset::resolve(ctx, None)?;

    if args.all {
        let bdir = cmake::binary_dir(&preset).ok_or_else(|| {
            anyhow::anyhow!(
                "Build directory does not exist for preset '{}'.\n\
         Run 'clibra build' first to configure the project.",
                ctx.preset.as_deref().unwrap_or("unknown")
            )
        })?;
        std::fs::remove_dir_all(bdir)?;
    } else {
        let mut cmd = cmake::base_build(&preset);
        ctx.run(cmd.args(["--target", "clean"]))?;
    }
    Ok(())
}
