use lux_lib::{config::Config, lua_installation::LuaInstallation, lua_version::LuaVersion};

use miette::{miette, Result};

pub async fn install_lua(config: Config) -> Result<()> {
    let version_stringified = &LuaVersion::from(&config)?;

    // TODO: Detect when path already exists by checking `Lua::path()` and prompt the user
    // whether they'd like to forcefully reinstall.
    let lua = LuaInstallation::install(version_stringified, &config).await?;
    let lua_root =
        lua.includes()
            .first()
            .and_then(|dir| dir.parent())
            .ok_or_else(|| {
                miette!(
            help = "ensure that pkg-config is installed and configured, and that you have Lua installed",
            "error getting lua include parent directory"
    )
            })?;

    tracing::info!(
        "Installed Lua ({}) to {}",
        version_stringified,
        lua_root.display(),
    );

    Ok(())
}
