use std::path::PathBuf;

use eyre::Result;
use itertools::sorted;

use crate::{
    backend,
    config::{self, Config},
    file,
};

use super::reconcile;

/// Symlinks all ruby tool versions from an external tool into mise
#[derive(Debug, usage_rs::Args)]
#[usage(verbatim_doc_comment, after_long_help = AFTER_LONG_HELP)]
pub(super) struct SyncRuby {
    #[usage(flatten)]
    _type: SyncRubyType,
}

#[derive(Debug, usage_rs::Args)]
pub(super) struct SyncRubyType {
    /// Get tool versions from Homebrew
    #[usage(long, required = true)]
    brew: bool,
}

impl SyncRuby {
    pub(super) async fn run(self) -> Result<()> {
        if self._type.brew {
            self.run_brew().await?;
        }
        let config = Config::reset().await?;
        let ts = config.get_toolset().await?;
        config::rebuild_shims_and_runtime_symlinks(
            &config,
            ts,
            &[],
            crate::lockfile::LockfileUpdateMode::Normal,
        )
        .await?;
        Ok(())
    }

    async fn run_brew(&self) -> Result<()> {
        let ruby = backend::get(&"ruby".into()).unwrap();

        let brew_opt = PathBuf::from(cmd!("brew", "--prefix").read()?).join("opt");

        let subdirs = file::dir_subdirs(&brew_opt)?;
        let mut links = vec![];
        for entry in sorted(subdirs) {
            if entry.starts_with(".") {
                continue;
            }
            if !entry.starts_with("ruby@") {
                continue;
            }
            let v = entry.trim_start_matches("ruby@");
            links.push((v.to_string(), brew_opt.join(&entry)));
        }
        let ownership = reconcile::LinkOwnership::in_namespace(&brew_opt);
        for v in reconcile::reconcile(ruby.ba(), ownership, links)? {
            miseprintln!("Synced ruby@{} from Homebrew", v);
        }
        Ok(())
    }
}

static AFTER_LONG_HELP: &str = color_print::cstr!(
    r#"<bold><underline>Examples:</underline></bold>

    $ <bold>brew install ruby</bold>
    $ <bold>mise sync ruby --brew</bold>
    $ <bold>mise use -g ruby</bold> - Use the latest version of Ruby installed by Homebrew
"#
);
