use crate::prompt::select_item;
use crate::services::inventory::{Host, get_hosts, get_playbooks};
use clap::Subcommand;
use eyre::Result;
use std::path::PathBuf;

#[derive(Subcommand)]
pub enum SelectCommands {
    #[command(alias = "h")]
    Host {
        #[arg(short, long, help = "Filter hosts by group")]
        group: Option<String>,
    },
    #[command(alias = "p")]
    Playbook,
}

pub fn run_select_host(group: Option<String>) -> Result<()> {
    let hosts = get_hosts(group.as_deref(), None)?;

    if hosts.is_empty() {
        eyre::bail!("No hosts found");
    }

    let selected = select_item(
        &hosts,
        |h: &Host| {
            format!(
                "{} ({}:{})",
                h.name, h.vars.ansible_host, h.vars.ansible_port
            )
        },
        "Select host",
    )?;

    match selected {
        Some(host) => {
            println!("{}", host.name);
            Ok(())
        }
        None => eyre::bail!("No host selected"),
    }
}

pub fn run_select_playbook() -> Result<()> {
    let playbooks = get_playbooks(None)?;

    let selected = select_item(
        &playbooks,
        |p: &PathBuf| {
            let name = p.file_stem().and_then(|s| s.to_str()).unwrap_or("unknown");
            let file = p.file_name().unwrap_or_default().to_string_lossy();
            format!("{} ({})", name, file)
        },
        "Select playbook",
    )?;

    match selected {
        Some(path) => {
            println!("{}", path.display());
            Ok(())
        }
        None => eyre::bail!("No playbook selected"),
    }
}
