//! Compute the root to the repository.
use clap::Args;

use crate::cli::get_cwd;
use crate::config::Config;
use crate::repository::Repositories;
use crate::version_control_system::VersionControlSystem;

/// Get the root and type of the repository the working directory or its
/// parent is into.
#[derive(Args)]
pub struct RootArgs {
    /// Get the root of the repository the parent directory of the current
    /// working directory is in.
    #[arg(long, short)]
    parent: bool,

    /// Also display repository types. The output will then have 4 words:
    /// <Root of the repository> <is_git> <is_jj>.
    #[arg(long)]
    print_type: bool,
    /// Force recreating the cache.
    #[arg(short = 'R', long, global = true)]
    refresh_cache: bool,
}

/// Execute the `rt repo root` command.
pub fn run(config: &Config, args: RootArgs) -> i32 {
    if args.refresh_cache {
        Repositories::load(config, true);
    }

    let mut cwd = get_cwd();

    if args.parent
        && let Some(parent) = cwd.parent()
    {
        cwd = parent.to_path_buf()
    }

    if let Some((root, vcs, _)) =
        VersionControlSystem::discover_root(cwd.clone())
    {
        print!("{}", root.display());
        if args.print_type {
            println!("\n{}\n{}", vcs.is_git(), vcs.is_jujutsu(),);
        } else {
            println!();
        }
        0
    } else {
        1
    }
}
