use std::env::consts::EXE_EXTENSION;
use std::fmt::Display;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::sync::LazyLock;

use anyhow::{Context, Result, bail};
use itertools::Itertools;
use prek_consts::env_vars::{EnvVars, EnvVarsRead};
use tracing::{debug, trace, warn};

use super::version::DotnetVersion;
use crate::fs::LockedFile;
use crate::http::REQWEST_CLIENT;
use crate::languages::dotnet::DotnetRequest;
use crate::process::Cmd;

static DOTNET_BINARY_NAME: LazyLock<String> = LazyLock::new(|| {
    if let Ok(name) = EnvVars.var(EnvVars::PREK_INTERNAL__DOTNET_BINARY_NAME) {
        name
    } else {
        "dotnet".to_string()
    }
});

/// Result of a dotnet installation or discovery.
#[derive(Debug, Clone)]
pub(crate) struct DotnetResult {
    dotnet: PathBuf,
    version: DotnetVersion,
}

impl Display for DotnetResult {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}@{}", self.dotnet.display(), self.version)?;
        Ok(())
    }
}

impl DotnetResult {
    /// Creates a result from a managed installation directory.
    pub(crate) fn from_dir(dir: &Path, version: DotnetVersion) -> Self {
        let dotnet = dir.join("dotnet").with_extension(EXE_EXTENSION);
        Self { dotnet, version }
    }

    /// Returns the resolved `dotnet` executable path.
    pub(crate) fn dotnet(&self) -> &Path {
        &self.dotnet
    }

    /// Returns the probed SDK version.
    pub(crate) fn version(&self) -> &DotnetVersion {
        &self.version
    }

    /// Builds a command that runs this `dotnet` executable.
    pub(crate) fn cmd(&self) -> Cmd {
        Cmd::new(&self.dotnet)
    }

    /// Creates a result by querying the SDK version from a `dotnet` executable.
    pub(crate) async fn from_executable(dotnet: PathBuf) -> Result<Self> {
        let mut cmd = Cmd::new(&dotnet);
        if let Some(parent) = dotnet.parent() {
            cmd.current_dir(parent);
        }

        let stdout = cmd.arg("--version").check(true).output().await?.stdout;
        let version_str = str::from_utf8(&stdout)?.trim();
        let version = version_str
            .parse()
            .with_context(|| format!("Failed to parse version from: {version_str}"))?;

        Ok(Self { dotnet, version })
    }
}

/// Finds, downloads, and manages SDK installations for the `dotnet` backend.
pub(crate) struct DotnetInstaller {
    /// The base directory for all managed dotnet installations (e.g., .../tools/dotnet)
    root: PathBuf,
}

impl DotnetInstaller {
    /// Creates an installer rooted at the managed `dotnet` tool directory.
    pub(crate) fn new(root: PathBuf) -> Self {
        Self { root }
    }

    /// Install or find dotnet SDK based on the language request.
    pub(crate) async fn install(
        &self,
        request: &DotnetRequest,
        allows_download: bool,
    ) -> Result<DotnetResult> {
        fs_err::tokio::create_dir_all(&self.root).await?;
        let _lock = LockedFile::acquire(self.root.join(".lock"), "dotnet").await?;

        if let Ok(result) = self.find_installed(request) {
            debug!(%result, "Using existing managed dotnet");
            return Ok(result);
        }

        if let Some(result) = self.find_system_dotnet(request).await? {
            debug!(%result, "Using system dotnet");
            return Ok(result);
        }

        if !allows_download {
            bail!("No suitable dotnet version found and downloads are disabled");
        }

        self.install_managed(request).await
    }

    /// Finds the newest managed SDK installation that satisfies the request.
    fn find_installed(&self, request: &DotnetRequest) -> Result<DotnetResult> {
        let mut installed = fs_err::read_dir(&self.root)
            .ok()
            .into_iter()
            .flatten()
            .filter_map(|entry| match entry {
                Ok(entry) => Some(entry),
                Err(err) => {
                    warn!(?err, "Failed to read entry");
                    None
                }
            })
            .filter(|entry| entry.file_type().is_ok_and(|file_type| file_type.is_dir()))
            .filter_map(|entry| {
                let dir_name = entry.file_name();
                let version = DotnetVersion::from_str(&dir_name.to_string_lossy()).ok()?;
                Some((version, entry.path()))
            })
            .sorted_unstable_by(|(a, _), (b, _)| a.cmp(b))
            .rev();

        installed
            .find_map(|(version, path)| {
                if request.matches(&version) {
                    Some(DotnetResult::from_dir(&path, version))
                } else {
                    None
                }
            })
            .context("No installed dotnet version matches the request")
    }

    /// Finds the first system `dotnet` executable in `PATH` that satisfies the request.
    async fn find_system_dotnet(&self, request: &DotnetRequest) -> Result<Option<DotnetResult>> {
        let dotnet_paths = match which::which_all(&*DOTNET_BINARY_NAME) {
            Ok(paths) => paths,
            Err(err) => {
                debug!("No dotnet executables found in PATH: {err}");
                return Ok(None);
            }
        };

        for dotnet in dotnet_paths {
            match DotnetResult::from_executable(dotnet).await {
                Ok(result) => {
                    if request.matches(result.version()) {
                        trace!(%result, "Found system dotnet that matches request");
                        return Ok(Some(result));
                    }
                    trace!(%result, "System dotnet does not match request");
                }
                Err(e) => {
                    warn!(?e, "Failed to query version for system dotnet");
                }
            }
        }

        Ok(None)
    }

    /// Downloads an SDK into a temporary directory, then promotes it to its final location.
    async fn install_managed(&self, request: &DotnetRequest) -> Result<DotnetResult> {
        let install_dir = tempfile::Builder::new()
            .prefix(".install-")
            .tempdir_in(&self.root)?;

        debug!(
            request = ?request,
            path = %install_dir.path().display(),
            "Installing dotnet SDK"
        );

        self.download(install_dir.path(), request).await?;

        let dotnet = install_dir
            .path()
            .join("dotnet")
            .with_extension(EXE_EXTENSION);
        let installed = DotnetResult::from_executable(dotnet)
            .await
            .context("Failed to query installed dotnet version")?;

        self.promote_installation(install_dir, installed).await
    }

    /// Renames a successful temporary installation into its versioned final directory.
    async fn promote_installation(
        &self,
        install_dir: tempfile::TempDir,
        installed: DotnetResult,
    ) -> Result<DotnetResult> {
        let DotnetResult { version, .. } = installed;

        let final_dir = self.root.join(version.to_string());
        if final_dir.exists() {
            warn!(
                path = %final_dir.display(),
                "Final installation directory already exists, removing"
            );
            fs_err::tokio::remove_dir_all(&final_dir).await?;
        }

        let install_path = install_dir.keep();
        fs_err::tokio::rename(&install_path, &final_dir).await?;

        Ok(DotnetResult::from_dir(&final_dir, version))
    }

    /// Downloads the platform-specific install script and runs it for the request.
    async fn download(&self, install_dir: &Path, request: &DotnetRequest) -> Result<()> {
        // https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-install-script
        let (script_url, script_name) = if cfg!(windows) {
            (
                "https://dot.net/v1/dotnet-install.ps1",
                "dotnet-install.ps1",
            )
        } else {
            ("https://dot.net/v1/dotnet-install.sh", "dotnet-install.sh")
        };
        let script_dir = tempfile::tempdir()?;
        let script_path = script_dir.path().join(script_name);

        let response = REQWEST_CLIENT
            .get(script_url)
            .send()
            .await?
            .error_for_status()
            .with_context(|| {
                format!("Failed to download dotnet install script from `{script_url}`")
            })?;
        let script_content = response.bytes().await?;
        fs_err::tokio::write(&script_path, &script_content).await?;

        Self::install_dotnet(&script_path, install_dir, request).await
    }

    #[cfg(unix)]
    /// Executes `dotnet-install.sh` for Unix-like platforms.
    async fn install_dotnet(
        script_path: &Path,
        install_dir: &Path,
        request: &DotnetRequest,
    ) -> Result<()> {
        let mut cmd = Cmd::new("bash");
        cmd.arg(script_path)
            .arg("--no-path")
            .arg("--install-dir")
            .arg(install_dir);
        match request {
            DotnetRequest::Any => {
                cmd.arg("--channel").arg("LTS");
            }
            DotnetRequest::Channel(channel) => {
                cmd.arg("--channel").arg(channel.to_string());
            }
            DotnetRequest::Exact(major, minor, patch) => {
                cmd.arg("--version").arg(format!("{major}.{minor}.{patch}"));
            }
        }

        cmd.check(true).output().await?;
        Ok(())
    }

    #[cfg(windows)]
    /// Executes `dotnet-install.ps1` for Windows.
    async fn install_dotnet(
        script_path: &Path,
        install_dir: &Path,
        request: &DotnetRequest,
    ) -> Result<()> {
        let mut cmd = Cmd::new("powershell.exe");
        cmd.arg("-NoProfile")
            .arg("-ExecutionPolicy")
            .arg("Bypass")
            .arg("-NonInteractive")
            .arg("-File")
            .arg(script_path)
            .arg("-NoPath")
            .arg("-InstallDir")
            .arg(install_dir);
        match request {
            DotnetRequest::Any => {
                cmd.arg("-Channel").arg("LTS");
            }
            DotnetRequest::Channel(channel) => {
                cmd.arg("-Channel").arg(channel.to_string());
            }
            DotnetRequest::Exact(major, minor, patch) => {
                cmd.arg("-Version").arg(format!("{major}.{minor}.{patch}"));
            }
        }

        cmd.check(true).output().await?;
        Ok(())
    }
}
