//! User interaction utilities (prompts, confirmations, selections)

use console::Style;
use dialoguer::{theme::ColorfulTheme, Confirm, Input, MultiSelect, Select};

/// Create a custom theme with enhanced cursor visibility
fn create_enhanced_theme() -> ColorfulTheme {
    ColorfulTheme {
        active_item_style: Style::new().cyan().bold(),
        active_item_prefix: console::style("❯".to_string()).cyan().bold(),
        inactive_item_prefix: console::style(" ".to_string()),
        ..Default::default()
    }
}

/// Prompt user for confirmation
#[allow(dead_code)] // UI utility for future use
pub fn confirm(prompt: &str, default: bool) -> anyhow::Result<bool> {
    let theme = create_enhanced_theme();
    Confirm::with_theme(&theme)
        .with_prompt(prompt)
        .default(default)
        .interact()
        .map_err(|e| anyhow::anyhow!("Failed to get user confirmation: {}", e))
}

/// Prompt user to select from a list of options
#[allow(dead_code)]
pub fn select<T: ToString>(prompt: &str, items: &[T]) -> anyhow::Result<usize> {
    let theme = create_enhanced_theme();
    Select::with_theme(&theme)
        .with_prompt(prompt)
        .items(items)
        .interact()
        .map_err(|e| anyhow::anyhow!("Failed to get user selection: {}", e))
}

/// Prompt user to select from a list with a default selection
#[allow(dead_code)] // UI utility for future use
pub fn select_with_default<T: ToString>(prompt: &str, items: &[T], default: usize) -> anyhow::Result<usize> {
    let theme = create_enhanced_theme();
    Select::with_theme(&theme)
        .with_prompt(prompt)
        .items(items)
        .default(default)
        .interact()
        .map_err(|e| anyhow::anyhow!("Failed to get user selection: {}", e))
}

/// Prompt user to select multiple items from a list
#[allow(dead_code)] // UI utility for future use
pub fn multiselect<T: ToString>(prompt: &str, items: &[T]) -> anyhow::Result<Vec<usize>> {
    let theme = create_enhanced_theme();
    MultiSelect::with_theme(&theme)
        .with_prompt(prompt)
        .items(items)
        .interact()
        .map_err(|e| anyhow::anyhow!("Failed to get user selections: {}", e))
}

/// Prompt user to select multiple items from a list with defaults pre-selected
#[allow(dead_code)] // UI utility for future use
pub fn multiselect_with_defaults<T: ToString>(
    prompt: &str,
    items: &[T],
    defaults: &[bool],
) -> anyhow::Result<Vec<usize>> {
    let theme = create_enhanced_theme();
    MultiSelect::with_theme(&theme)
        .with_prompt(prompt)
        .items(items)
        .defaults(defaults)
        .interact()
        .map_err(|e| anyhow::anyhow!("Failed to get user selections: {}", e))
}

/// Prompt user for text input
#[allow(dead_code)] // UI utility for future use
pub fn input(prompt: &str) -> anyhow::Result<String> {
    let theme = create_enhanced_theme();
    Input::<String>::with_theme(&theme)
        .with_prompt(prompt)
        .interact_text()
        .map_err(|e| anyhow::anyhow!("Failed to get user input: {}", e))
}

/// Prompt user for text input with a default value
#[allow(dead_code)]
pub fn input_with_default(prompt: &str, default: &str) -> anyhow::Result<String> {
    let theme = create_enhanced_theme();
    Input::with_theme(&theme)
        .with_prompt(prompt)
        .default(default.to_string())
        .interact_text()
        .map_err(|e| anyhow::anyhow!("Failed to get user input: {}", e))
}

/// Prompt user for text input with validation
#[allow(dead_code)]
pub fn input_with_validation<F>(prompt: &str, validator: F) -> anyhow::Result<String>
where
    F: Fn(&String) -> Result<(), String> + 'static,
{
    let theme = create_enhanced_theme();
    Input::with_theme(&theme)
        .with_prompt(prompt)
        .validate_with(validator)
        .interact_text()
        .map_err(|e| anyhow::anyhow!("Failed to get user input: {}", e))
}
