use crate::config::config_file::mise_toml::MiseToml;
use crate::config::settings::{SETTINGS_META, SettingsType};
use crate::config::{ConfigPathOptions, resolve_target_config_path, top_toml_config};
use crate::file::display_path;
use crate::toml::dedup_toml_array;
use clap::ValueEnum;
use eyre::bail;
use std::path::PathBuf;

/// Set the value of a setting in a mise.toml file
#[derive(Debug, clap::Args)]
#[clap(after_long_help = AFTER_LONG_HELP, verbatim_doc_comment)]
pub struct ConfigSet {
    /// The path of the config to display
    pub key: String,

    /// The value to set the key to (optional if provided as KEY=VALUE)
    pub value: Option<String>,

    /// The path to the mise.toml file to edit
    ///
    /// Can be a file path or directory. If a directory is provided, the config file in that directory is used.
    ///
    /// If not provided, the nearest mise.toml file will be used
    #[clap(short, long, visible_alias = "path", value_hint = clap::ValueHint::AnyPath)]
    pub file: Option<PathBuf>,

    #[clap(value_enum, short, long, default_value_t)]
    pub type_: TomlValueTypes,
}

#[derive(ValueEnum, Default, Clone, Debug)]
pub enum TomlValueTypes {
    #[default]
    Infer,
    #[value()]
    String,
    #[value()]
    Integer,
    #[value()]
    Float,
    #[value()]
    Bool,
    #[value()]
    List,
    #[value()]
    Set,
}

impl ConfigSet {
    pub fn run(self) -> eyre::Result<()> {
        let (full_key, value) = match self.value {
            Some(v) => (self.key, v),
            None => {
                let (k, v) = self.key.split_once('=').ok_or_else(|| {
                    eyre::eyre!(
                        "Usage: mise config set <KEY>=<VALUE> or mise config set <KEY> <VALUE>"
                    )
                })?;
                (k.to_string(), v.to_string())
            }
        };
        // Only an explicitly named target goes through the shared resolver — the default is a
        // different rule (the top TOML config of the loaded set, not the nearest writable one).
        let file = match self.file {
            Some(path) => Some(resolve_target_config_path(ConfigPathOptions {
                path: Some(path),
                prefer_toml: true,
                ..Default::default()
            })?),
            None => top_toml_config(),
        };
        let Some(file) = file else {
            bail!("No mise.toml file found");
        };
        if !file.exists() {
            bail!("config file not found: {}", display_path(&file));
        }
        let mut config: toml_edit::DocumentMut = std::fs::read_to_string(&file)?.parse()?;
        let mut container = config.as_item_mut();
        let parts = full_key.split('.').collect::<Vec<&str>>();
        let last_key = parts.last().unwrap();
        for (idx, part) in parts.iter().take(parts.len() - 1).enumerate() {
            container = container
                .as_table_like_mut()
                .ok_or_else(|| {
                    eyre::eyre!(
                        "cannot set '{full_key}': '{}' is already set to a non-table value",
                        parts[..idx].join(".")
                    )
                })?
                .entry(part)
                .or_insert({
                    let mut t = toml_edit::Table::new();
                    t.set_implicit(true);
                    toml_edit::Item::Table(t)
                });
            // if the key is a tool with a simple value, we want to convert it to an inline table preserving the version
            let is_simple_tool_version =
                full_key.starts_with("tools.") && idx == 1 && !container.is_table_like();
            if is_simple_tool_version {
                let mut inline_table = toml_edit::InlineTable::new();
                inline_table.insert("version", container.as_value().unwrap().clone());
                *container = toml_edit::Item::Value(toml_edit::Value::InlineTable(inline_table));
            }
        }

        let infer_bool_or_string = |value: &str| match value {
            "true" | "yes" | "1" => TomlValueTypes::Bool,
            "false" | "no" | "0" => TomlValueTypes::Bool,
            _ => TomlValueTypes::String,
        };
        let type_to_use = match self.type_ {
            TomlValueTypes::Infer => {
                let expected_type = full_key
                    .strip_prefix("settings.")
                    .and_then(|key| SETTINGS_META.get(key));
                match expected_type {
                    Some(meta) => match meta.type_ {
                        SettingsType::Bool => TomlValueTypes::Bool,
                        SettingsType::BoolOrString => infer_bool_or_string(&value),
                        SettingsType::String => TomlValueTypes::String,
                        SettingsType::Integer => TomlValueTypes::Integer,
                        SettingsType::Duration => TomlValueTypes::String,
                        SettingsType::Path => TomlValueTypes::String,
                        SettingsType::Url => TomlValueTypes::String,
                        SettingsType::ListString => TomlValueTypes::List,
                        SettingsType::ListPath => TomlValueTypes::List,
                        SettingsType::SetString => TomlValueTypes::Set,
                        SettingsType::IndexMap => TomlValueTypes::String,
                    },
                    None => infer_bool_or_string(&value),
                }
            }
            _ => self.type_,
        };

        let value = match type_to_use {
            TomlValueTypes::String => toml_edit::value(value),
            TomlValueTypes::Integer => toml_edit::value(value.parse::<i64>()?),
            TomlValueTypes::Float => toml_edit::value(value.parse::<f64>()?),
            TomlValueTypes::Bool => toml_edit::value(value.parse::<bool>()?),
            TomlValueTypes::List => {
                let mut list = toml_edit::Array::new();
                for item in value.split(',').map(|s| s.trim()) {
                    list.push(item);
                }
                toml_edit::Item::Value(toml_edit::Value::Array(list))
            }
            TomlValueTypes::Set => {
                let mut set = toml_edit::Array::new();
                let value = value.trim();
                if value != "[]" {
                    for item in value.split(',').map(|s| s.trim()).filter(|s| !s.is_empty()) {
                        set.push(item);
                    }
                }
                toml_edit::Item::Value(toml_edit::Value::Array(dedup_toml_array(&set)))
            }
            TomlValueTypes::Infer => bail!("Type not found"),
        };

        container
            .as_table_like_mut()
            .ok_or_else(|| {
                eyre::eyre!(
                    "cannot set '{full_key}': '{}' is already set to a non-table value",
                    parts[..parts.len() - 1].join(".")
                )
            })?
            .insert(last_key, value);

        let raw = config.to_string();
        MiseToml::from_str(&raw, &file)?;
        std::fs::write(&file, raw)?;
        Ok(())
    }
}

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

    $ <bold>mise config set tools.python 3.12</bold>
    $ <bold>mise config set settings.always_keep_download true</bold>
    $ <bold>mise config set env.TEST_ENV_VAR ABC</bold>
    $ <bold>mise config set settings.disable_tools node,rust</bold>

    # Type for `settings` is inferred
    $ <bold>mise config set settings.jobs 4</bold>
"#
);
