use clap::Args;
use inquire::{Confirm, Select};
use itertools::Itertools;
use lux_lib::{
    config::Config,
    lockfile::LocalPackage,
    lua_rockspec::RemoteLuaRockspec,
    lua_version::LuaVersion,
    package::PackageReq,
    rockspec::Rockspec,
    tree::{InstallTree, RockMatches, Tree},
};
use miette::{miette, Context, IntoDiagnostic, Result};
use url::Url;
use walkdir::WalkDir;

#[derive(Args)]
pub struct Doc {
    package: PackageReq,

    /// Ignore local docs and open the package's homepage in a browser.
    #[arg(long)]
    online: bool,
}

pub async fn doc(args: Doc, config: Config) -> Result<()> {
    let tree = config.user_tree(LuaVersion::from(&config)?.clone())?;
    let package_id = match tree.match_rocks(&args.package)? {
        RockMatches::NotFound(package_req) => {
            Err(miette!("no package matching {package_req} found"))
        }
        RockMatches::Many(_package_ids) => Err(miette!(
            help = "specify an exact package (<name>@<version>) or narrow the version requirement",
            "found multiple packages matching {}",
            &args.package
        )),
        RockMatches::Single(package_id) => Ok(package_id),
    }?;
    let lockfile = tree.lockfile()?;
    let pkg = lockfile
        .get(&package_id)
        .ok_or_else(|| miette!("package is installed, but not found in the lockfile"))?
        .clone();
    if args.online {
        open_homepage(pkg, &tree).await
    } else {
        open_local_docs(pkg, &tree, &config).await
    }
}

async fn open_homepage(pkg: LocalPackage, tree: &Tree) -> Result<()> {
    let homepage = match get_homepage(&pkg, tree)? {
        Some(homepage) => Ok(homepage),
        None => Err(miette!(
            "package {} does not have a homepage in its RockSpec",
            pkg.into_package_spec()
        )),
    }?;
    open::that(homepage.to_string()).into_diagnostic()?;
    Ok(())
}

fn get_homepage(pkg: &LocalPackage, tree: &Tree) -> Result<Option<Url>> {
    let layout = tree.installed_rock_layout(pkg)?;
    let rockspec_content = std::fs::read_to_string(layout.rockspec_path()).into_diagnostic()?;
    let rockspec = RemoteLuaRockspec::new(&rockspec_content)?;
    Ok(rockspec.description().homepage.clone())
}

async fn open_local_docs(pkg: LocalPackage, tree: &Tree, config: &Config) -> Result<()> {
    let layout = tree.installed_rock_layout(&pkg)?;
    let files: Vec<String> = WalkDir::new(&layout.doc)
        .into_iter()
        .filter_map_ok(|file| {
            let path = file.into_path();
            if path.is_file() {
                path.file_name()
                    .map(|file_name| file_name.to_string_lossy().to_string())
            } else {
                None
            }
        })
        .try_collect()
        .into_diagnostic()?;
    match files.first() {
        Some(file) if files.len() == 1 => {
            edit::edit_file(layout.doc.join(file)).into_diagnostic()?;
            Ok(())
        }
        Some(_) => {
            let file = Select::new(
                "Multiple documentation files found. Please select one to open.",
                files,
            )
            .prompt()
            .into_diagnostic()
            .wrap_err("error selecting from multiple files")?;
            edit::edit_file(layout.doc.join(file)).into_diagnostic()?;
            Ok(())
        }
        None => match get_homepage(&pkg, tree)? {
            None => Err(miette!(
                "no documentation found for package '{}'",
                pkg.into_package_spec()
            )),
            Some(homepage) => {
                if config.no_prompt() {
                    return Err(miette!(
                        "no local documentation found for package '{}'",
                        pkg.into_package_spec()
                    ));
                } else if Confirm::new("No local documentation found. Open homepage?")
                    .with_default(false)
                    .prompt()
                    .into_diagnostic()
                    .wrap_err("error prompting to open homepage")?
                {
                    open::that(homepage.to_string()).into_diagnostic()?;
                }
                Ok(())
            }
        },
    }
}
