use std::io::Write;
use std::path::{Path, PathBuf};

use color_eyre::eyre::Result;
use ini::Ini;
use rust_i18n::t;
use tracing::{debug, warn};

use crate::HOME_DIR;
use crate::command::CommandExt;
use crate::config::NixHandler;
use crate::error::{SkipStep, TopgradeError};
use crate::execution_context::ExecutionContext;
use crate::step::Step;
use crate::steps::generic::IS_WSL;
use crate::steps::os::archlinux;
use crate::steps::unix::{NhSwitchArgs, can_nh_switch, nh_switch};
use crate::sudo::SudoExecuteOpts;
use crate::terminal::{print_separator, print_warning, prompt_yesno};
use crate::utils::{PathExt, require, require_flatpak, require_one, which};

static OS_RELEASE_PATH: &str = "/etc/os-release";

#[allow(clippy::upper_case_acronyms)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Distribution {
    Alpine,
    AOSC,
    Wolfi,
    Arch,
    Bedrock,
    CentOS,
    Chimera,
    ClearLinux,
    Fedora,
    FedoraImmutable,
    Debian,
    Gentoo,
    NILRT,
    OpenMandriva,
    OpenSuseTumbleweed,
    PCLinuxOS,
    Suse,
    SuseMicro,
    Vanilla,
    Void,
    Solus,
    Exherbo,
    NixOS,
    KDELinux,
    KDENeon,
    Nobara,
}

impl Distribution {
    fn parse_os_release(os_release: &Ini) -> Result<Self> {
        let section = os_release.general_section();
        let id = section.get("ID");
        let name = section.get("NAME");
        let variant = section.get("VARIANT");
        let id_like: Option<Vec<&str>> = section.get("ID_LIKE").map(|s| s.split_whitespace().collect());

        Ok(match id {
            Some("alpine") => Distribution::Alpine,
            Some("aosc") => Distribution::AOSC,
            Some("chimera") => Distribution::Chimera,
            Some("wolfi") => Distribution::Wolfi,
            Some("centos") | Some("rhel") | Some("ol") => Distribution::CentOS,
            Some("clear-linux-os") => Distribution::ClearLinux,
            Some("fedora") => Distribution::match_fedora_variant(&variant),
            Some("nilrt") => Distribution::NILRT,
            Some("nobara") => Distribution::Nobara,
            Some("void") => Distribution::Void,
            Some("debian") | Some("pureos") | Some("Deepin") | Some("linuxmint") => Distribution::Debian,
            Some("arch") | Some("manjaro-arm") | Some("garuda") | Some("artix") | Some("cachyos") => Distribution::Arch,
            Some("solus") => Distribution::Solus,
            Some("gentoo") | Some("funtoo") => Distribution::Gentoo,
            Some("exherbo") => Distribution::Exherbo,
            Some("nixos") => Distribution::NixOS,
            Some("opensuse-microos") => Distribution::SuseMicro,
            Some("kde-linux") => Distribution::KDELinux,
            Some("neon") => Distribution::KDENeon,
            Some("openmandriva") => Distribution::OpenMandriva,
            Some("pclinuxos") => Distribution::PCLinuxOS,
            Some(id) if id.starts_with("origami") => Distribution::FedoraImmutable,

            _ => {
                if let Some(name) = name
                    && name.contains("Vanilla")
                {
                    return Ok(Distribution::Vanilla);
                }
                if let Some(id_like) = id_like {
                    if id_like.contains(&"debian") || id_like.contains(&"ubuntu") {
                        return Ok(Distribution::Debian);
                    } else if id_like.contains(&"centos") {
                        return Ok(Distribution::CentOS);
                    } else if id_like.contains(&"suse") {
                        let id_variant = id.unwrap_or_default();
                        return if id_variant.contains("tumbleweed") {
                            Ok(Distribution::OpenSuseTumbleweed)
                        } else {
                            Ok(Distribution::Suse)
                        };
                    } else if id_like.contains(&"arch") || id_like.contains(&"archlinux") {
                        return Ok(Distribution::Arch);
                    } else if id_like.contains(&"alpine") {
                        return Ok(Distribution::Alpine);
                    } else if id_like.contains(&"fedora") {
                        return Ok(Distribution::match_fedora_variant(&variant));
                    }
                }
                return Err(TopgradeError::UnknownLinuxDistribution.into());
            }
        })
    }

    fn match_fedora_variant(variant: &Option<&str>) -> Self {
        if let Some("Silverblue" | "Kinoite" | "Sericea" | "Onyx" | "IoT Edition" | "Sway Atomic" | "CoreOS") = variant
        {
            Distribution::FedoraImmutable
        } else {
            Distribution::Fedora
        }
    }

    pub fn detect() -> Result<Self> {
        if PathBuf::from("/bedrock").exists() {
            return Ok(Distribution::Bedrock);
        }

        if PathBuf::from(OS_RELEASE_PATH).exists() {
            let os_release = Ini::load_from_file(OS_RELEASE_PATH)?;

            if os_release.general_section().is_empty() {
                return Err(TopgradeError::EmptyOSReleaseFile.into());
            }

            return Self::parse_os_release(&os_release);
        }

        Err(TopgradeError::EmptyOSReleaseFile.into())
    }

    pub fn upgrade(self, ctx: &ExecutionContext) -> Result<()> {
        print_separator(t!("System update"));

        match self {
            Distribution::Alpine => upgrade_alpine_linux(ctx),
            Distribution::Chimera => upgrade_chimera_linux(ctx),
            Distribution::Wolfi => upgrade_wolfi_linux(ctx),
            Distribution::Arch => archlinux::upgrade_arch_linux(ctx),
            Distribution::CentOS | Distribution::Fedora => upgrade_redhat(ctx),
            Distribution::FedoraImmutable => upgrade_fedora_immutable(ctx),
            Distribution::ClearLinux => upgrade_clearlinux(ctx),
            Distribution::Debian => upgrade_debian(ctx),
            Distribution::Gentoo => upgrade_gentoo(ctx),
            Distribution::Suse => upgrade_suse(ctx),
            Distribution::SuseMicro => upgrade_suse_micro(ctx),
            Distribution::OpenSuseTumbleweed => upgrade_opensuse_tumbleweed(ctx),
            Distribution::Vanilla => upgrade_vanilla(ctx),
            Distribution::Void => upgrade_void(ctx),
            Distribution::Solus => upgrade_solus(ctx),
            Distribution::Exherbo => upgrade_exherbo(ctx),
            Distribution::NixOS => upgrade_nixos(ctx),
            Distribution::KDELinux => upgrade_kde_linux(ctx),
            Distribution::KDENeon => upgrade_neon(ctx),
            Distribution::Bedrock => update_bedrock(ctx),
            Distribution::OpenMandriva => upgrade_openmandriva(ctx),
            Distribution::PCLinuxOS => upgrade_pclinuxos(ctx),
            Distribution::Nobara => upgrade_nobara(ctx),
            Distribution::NILRT => upgrade_nilrt(ctx),
            Distribution::AOSC => upgrade_aosc(ctx),
        }
    }

    pub fn show_summary(self) {
        if let Distribution::Arch = self {
            archlinux::show_pacnew();
        }
    }
}

fn update_bedrock(ctx: &ExecutionContext) -> Result<()> {
    let brl = require("brl")?;

    let output = ctx.execute(&brl).always().arg("list").output_checked_utf8()?;
    debug!("brl list: {:?} {:?}", output.stdout, output.stderr);

    for distribution in output.stdout.trim().lines() {
        debug!("Bedrock distribution {}", distribution);
        match distribution {
            "arch" => archlinux::upgrade_arch_linux(ctx)?,
            "debian" | "ubuntu" | "linuxmint" => upgrade_debian(ctx)?,
            "centos" | "fedora" => upgrade_redhat(ctx)?,
            "bedrock" => upgrade_bedrock_strata(ctx)?,
            _ => {
                warn!("Unknown distribution {}", distribution);
            }
        }
    }

    Ok(())
}

fn upgrade_aosc(ctx: &ExecutionContext) -> Result<()> {
    let oma = require("oma")?;
    let sudo = ctx.require_sudo()?;

    let mut cmd = sudo.execute(ctx, &oma)?;
    cmd.arg("upgrade");

    if ctx.config().yes(Step::System) {
        cmd.arg("-y");
    }

    cmd.status_checked()
}

fn upgrade_alpine_linux(ctx: &ExecutionContext) -> Result<()> {
    let apk = require("apk")?;
    let sudo = ctx.require_sudo()?;

    sudo.execute(ctx, &apk)?.arg("update").status_checked()?;
    sudo.execute(ctx, &apk)?.arg("upgrade").status_checked()
}

fn upgrade_chimera_linux(ctx: &ExecutionContext) -> Result<()> {
    let apk = require("apk")?;
    let sudo = ctx.require_sudo()?;

    sudo.execute(ctx, &apk)?.arg("update").status_checked()?;
    sudo.execute(ctx, &apk)?.arg("upgrade").status_checked()
}

fn upgrade_wolfi_linux(ctx: &ExecutionContext) -> Result<()> {
    let apk = require("apk")?;
    let sudo = ctx.require_sudo()?;

    sudo.execute(ctx, &apk)?.arg("update").status_checked()?;
    sudo.execute(ctx, &apk)?.arg("upgrade").status_checked()
}

fn upgrade_redhat(ctx: &ExecutionContext) -> Result<()> {
    if let Some(bootc) = which("bootc")
        && ctx.config().bootc()
    {
        let sudo = ctx.require_sudo()?;
        return sudo.execute(ctx, &bootc)?.arg("upgrade").status_checked();
    }

    if let Some(ostree) = which("rpm-ostree")
        && ctx.config().rpm_ostree()
    {
        let mut command = ctx.execute(ostree);
        command.arg("upgrade");
        return command.status_checked();
    };

    let dnf = require_one(["dnf", "yum"])?;
    let sudo = ctx.require_sudo()?;

    let mut command = sudo.execute(ctx, &dnf)?;
    command.arg(if ctx.config().redhat_distro_sync() {
        "distro-sync"
    } else {
        "upgrade"
    });

    if let Some(args) = ctx.config().dnf_arguments() {
        command.args(args.split_whitespace());
    }

    if ctx.config().yes(Step::System) {
        command.arg("-y");
    }

    command.status_checked()?;
    Ok(())
}

fn upgrade_nobara(ctx: &ExecutionContext) -> Result<()> {
    let dnf = require("dnf")?;
    let sudo = ctx.require_sudo()?;

    let mut update_command = sudo.execute(ctx, &dnf)?;

    if ctx.config().yes(Step::System) {
        update_command.arg("-y");
    }

    update_command.arg("update");
    // See https://nobaraproject.org/docs/upgrade-troubleshooting/how-do-i-update-the-system/
    update_command.args([
        "rpmfusion-nonfree-release",
        "rpmfusion-free-release",
        "fedora-repos",
        "nobara-repos",
    ]);
    update_command.arg("--refresh").status_checked()?;

    let mut upgrade_command = sudo.execute(ctx, &dnf)?;

    if ctx.config().yes(Step::System) {
        upgrade_command.arg("-y");
    }

    upgrade_command.arg("distro-sync");

    upgrade_command.status_checked()?;
    Ok(())
}

fn upgrade_nilrt(ctx: &ExecutionContext) -> Result<()> {
    let opkg = require("opkg")?;
    let sudo = ctx.require_sudo()?;

    sudo.execute(ctx, &opkg)?.arg("update").status_checked()?;
    sudo.execute(ctx, &opkg)?.arg("upgrade").status_checked()
}

fn upgrade_fedora_immutable(ctx: &ExecutionContext) -> Result<()> {
    if let Some(bootc) = which("bootc")
        && ctx.config().bootc()
    {
        let sudo = ctx.require_sudo()?;
        return sudo.execute(ctx, &bootc)?.arg("upgrade").status_checked();
    }

    let ostree = require("rpm-ostree")?;
    let mut command = ctx.execute(ostree);
    command.arg("upgrade");
    command.status_checked()?;
    Ok(())
}

fn upgrade_bedrock_strata(ctx: &ExecutionContext) -> Result<()> {
    let brl = require("brl")?;
    let sudo = ctx.require_sudo()?;

    sudo.execute(ctx, &brl)?.arg("update").status_checked()?;

    Ok(())
}

fn upgrade_suse(ctx: &ExecutionContext) -> Result<()> {
    let zypper = require("zypper")?;
    let sudo = ctx.require_sudo()?;

    sudo.execute(ctx, &zypper)?.arg("refresh").status_checked()?;

    let mut cmd = sudo.execute(ctx, &zypper)?;
    cmd.arg(if ctx.config().suse_dup() {
        "dist-upgrade"
    } else {
        "update"
    });
    if ctx.config().yes(Step::System) {
        cmd.arg("-y");
    }

    cmd.status_checked()?;

    Ok(())
}

fn upgrade_opensuse_tumbleweed(ctx: &ExecutionContext) -> Result<()> {
    let zypper = require("zypper")?;
    let sudo = ctx.require_sudo()?;

    sudo.execute(ctx, &zypper)?.arg("refresh").status_checked()?;

    let mut cmd = sudo.execute(ctx, &zypper)?;
    cmd.arg("dist-upgrade");
    if ctx.config().yes(Step::System) {
        cmd.arg("-y");
    }

    cmd.status_checked()?;

    Ok(())
}

fn upgrade_suse_micro(ctx: &ExecutionContext) -> Result<()> {
    let upd = require("transactional-update")?;
    let sudo = ctx.require_sudo()?;

    let mut cmd = sudo.execute(ctx, &upd)?;
    if ctx.config().yes(Step::System) {
        cmd.arg("-n");
    }

    cmd.arg("dup").status_checked()?;

    Ok(())
}

fn upgrade_openmandriva(ctx: &ExecutionContext) -> Result<()> {
    let dnf = require("dnf")?;
    let sudo = ctx.require_sudo()?;

    let mut command = sudo.execute(ctx, &dnf)?;

    command.arg("upgrade");

    if let Some(args) = ctx.config().dnf_arguments() {
        command.args(args.split_whitespace());
    }

    if ctx.config().yes(Step::System) {
        command.arg("-y");
    }

    command.status_checked()?;

    Ok(())
}

fn upgrade_pclinuxos(ctx: &ExecutionContext) -> Result<()> {
    let apt_get = require("apt-get")?;
    let sudo = ctx.require_sudo()?;

    let mut command_update = sudo.execute(ctx, &apt_get)?;

    command_update.arg("update");

    if let Some(args) = ctx.config().dnf_arguments() {
        command_update.args(args.split_whitespace());
    }

    if ctx.config().yes(Step::System) {
        command_update.arg("-y");
    }

    command_update.status_checked()?;

    let mut cmd = sudo.execute(ctx, &apt_get)?;
    cmd.arg("dist-upgrade");
    if ctx.config().yes(Step::System) {
        cmd.arg("-y");
    }
    cmd.status_checked()?;

    Ok(())
}

fn upgrade_vanilla(ctx: &ExecutionContext) -> Result<()> {
    let apx = require("apx")?;

    let mut update = ctx.execute(&apx);
    update.args(["update", "--all"]);
    if ctx.config().yes(Step::System) {
        update.arg("-y");
    }
    update.status_checked()?;

    let mut upgrade = ctx.execute(&apx);
    update.args(["upgrade", "--all"]);
    if ctx.config().yes(Step::System) {
        upgrade.arg("-y");
    }
    upgrade.status_checked()?;

    Ok(())
}

fn upgrade_void(ctx: &ExecutionContext) -> Result<()> {
    let xbps = require("xbps-install")?;
    let sudo = ctx.require_sudo()?;

    let mut command = sudo.execute(ctx, &xbps)?;
    command.args(["-Su", "xbps"]);
    if ctx.config().yes(Step::System) {
        command.arg("-y");
    }
    command.status_checked()?;

    let mut command = sudo.execute(ctx, &xbps)?;
    command.arg("-u");
    if ctx.config().yes(Step::System) {
        command.arg("-y");
    }
    command.status_checked()?;

    Ok(())
}

fn upgrade_gentoo(ctx: &ExecutionContext) -> Result<()> {
    let emerge = require("emerge")?;
    let sudo = ctx.require_sudo()?;

    if let Some(layman) = which("layman") {
        sudo.execute(ctx, &layman)?.args(["-s", "ALL"]).status_checked()?;
    }

    println!("{}", t!("Syncing portage"));
    if let Some(ego) = which("ego") {
        // The Funtoo team doesn't recommend running both ego sync and emerge --sync
        sudo.execute(ctx, &ego)?.arg("sync").status_checked()?;
    } else {
        sudo.execute(ctx, &emerge)?
            .arg("--sync")
            .args(
                ctx.config()
                    .emerge_sync_flags()
                    .map(|s| s.split_whitespace().collect())
                    .unwrap_or_else(|| vec!["-q"]),
            )
            .status_checked()?;
    }

    if let Some(eix_update) = which("eix-update") {
        sudo.execute(ctx, &eix_update)?.status_checked()?;
    }

    sudo.execute(ctx, &emerge)?
        .args(
            ctx.config()
                .emerge_update_flags()
                .map(|s| s.split_whitespace().collect())
                .unwrap_or_else(|| vec!["-uDNa", "--with-bdeps=y", "world"]),
        )
        .status_checked()?;

    Ok(())
}

enum AptKind {
    AptFast,
    Mist,
    Nala,
    Apt,
    AptGet,
}

fn detect_apt() -> Result<(AptKind, PathBuf)> {
    use AptKind::*;

    if let Some(apt_fast) = which("apt-fast") {
        Ok((AptFast, apt_fast))
    } else if let Some(mist) = which("mist") {
        Ok((Mist, mist))
    } else if Path::new("/usr/bin/nala").exists() {
        Ok((Nala, Path::new("/usr/bin/nala").to_path_buf()))
    } else if let Some(apt) = which("apt") {
        Ok((Apt, apt))
    } else {
        Ok((AptGet, require("apt-get")?))
    }
}

fn upgrade_debian(ctx: &ExecutionContext) -> Result<()> {
    use AptKind::*;

    let (kind, apt) = detect_apt()?;

    // MIST does not require `sudo`
    if let Mist = kind {
        ctx.execute(&apt).arg("update").status_checked()?;
        ctx.execute(&apt).arg("upgrade").status_checked()?;

        // Simply return as MIST does not have `clean` and `autoremove`
        // subcommands, neither the `-y` option (for now maybe?).
        return Ok(());
    }

    let sudo = ctx.require_sudo()?;
    if !matches!(kind, Nala) {
        sudo.execute(ctx, &apt)?
            .arg("update")
            .status_checked_with_codes(&[0, 100])?;
    }

    let mut command = sudo.execute(ctx, &apt)?;
    if let Nala = kind {
        command.arg("upgrade");
    } else {
        command.arg("dist-upgrade");
    };
    if ctx.config().yes(Step::System) {
        command.arg("-y");
    }
    if let Some(args) = ctx.config().apt_arguments() {
        command.args(args.split_whitespace());
    }
    command.status_checked()?;

    if ctx.config().cleanup() {
        sudo.execute(ctx, &apt)?.arg("clean").status_checked()?;

        let mut command = sudo.execute(ctx, &apt)?;
        command.arg("autoremove");
        if ctx.config().yes(Step::System) {
            command.arg("-y");
        }
        command.status_checked()?;
    }

    Ok(())
}

pub fn run_deb_get(ctx: &ExecutionContext) -> Result<()> {
    let deb_get = require("deb-get")?;

    print_separator("deb-get");

    // When the system step runs, apt is already up to date, so we set DISABLE_APT=y to skip
    // redundant apt operations in deb-get. Otherwise, we pass --dg-only to `upgrade` so that
    // deb-get only updates apt managed packages that were installed via deb-get.
    let disable_apt = ctx.config().should_run(Step::System);
    let upgrade_opt = (!disable_apt).then_some("--dg-only");

    let base_cmd = || {
        let mut cmd = ctx.execute(&deb_get);
        if disable_apt {
            cmd.env("DISABLE_APT", "y");
        }
        cmd
    };

    base_cmd().arg("update").status_checked()?;
    base_cmd().arg("upgrade").args(upgrade_opt).status_checked()?;

    if ctx.config().cleanup() {
        let output = ctx.execute(&deb_get).arg("clean").output_checked()?;
        // Swallow the output, as it's very noisy and not useful.
        //  The output is automatically printed as part of `output_checked` when an error occurs.
        println!("{}", t!("<output from `deb-get clean` omitted>"));
        debug!("`deb-get clean` output: {output:?}");
    }

    Ok(())
}

fn upgrade_solus(ctx: &ExecutionContext) -> Result<()> {
    let eopkg = require("eopkg")?;
    let sudo = ctx.require_sudo()?;

    let mut cmd = sudo.execute(ctx, &eopkg)?;
    if ctx.config().yes(Step::System) {
        cmd.arg("-y");
    }
    cmd.arg("upgrade").status_checked()?;

    Ok(())
}

pub fn run_am(ctx: &ExecutionContext) -> Result<()> {
    let am = require("am")?;

    print_separator("AM");

    let mut am = ctx.execute(am);

    if ctx.config().yes(Step::AM) {
        am.arg("-U");
    } else {
        am.arg("-u");
    }

    am.status_checked()
}

pub fn run_appman(ctx: &ExecutionContext) -> Result<()> {
    let appman = require("appman")?;

    print_separator("appman");

    ctx.execute(appman).arg("-u").status_checked()
}

pub fn run_pacdef(ctx: &ExecutionContext) -> Result<()> {
    let pacdef = require("pacdef")?;

    print_separator("pacdef");

    let output = ctx.execute(&pacdef).arg("version").output_checked()?;
    let string = String::from_utf8(output.stdout)?;
    let new_version = string.contains("version: 1");

    if new_version {
        let mut cmd = ctx.execute(&pacdef);
        cmd.args(["package", "sync"]);
        if ctx.config().yes(Step::System) {
            cmd.arg("--noconfirm");
        }
        cmd.status_checked()?;

        println!();
        ctx.execute(&pacdef).args(["package", "review"]).status_checked()?;
    } else {
        let mut cmd = ctx.execute(&pacdef);
        cmd.arg("sync");
        if ctx.config().yes(Step::System) {
            cmd.arg("--noconfirm");
        }

        cmd.status_checked()?;

        println!();
        ctx.execute(&pacdef).arg("review").status_checked()?;
    }
    Ok(())
}

pub fn run_pacstall(ctx: &ExecutionContext) -> Result<()> {
    let pacstall = require("pacstall")?;

    print_separator("Pacstall");

    let mut update_cmd = ctx.execute(&pacstall);
    let mut upgrade_cmd = ctx.execute(pacstall);

    if ctx.config().yes(Step::Pacstall) {
        update_cmd.arg("-P");
        upgrade_cmd.arg("-P");
    }

    update_cmd.arg("-U").status_checked()?;
    upgrade_cmd.arg("-Up").status_checked()
}

pub fn run_pkgfile(ctx: &ExecutionContext) -> Result<()> {
    let pkgfile = require("pkgfile")?;

    if !ctx.config().enable_pkgfile() {
        return Err(SkipStep("Pkgfile isn't enabled".to_string()).into());
    }

    print_separator("pkgfile");

    let sudo = ctx.require_sudo()?;
    sudo.execute(ctx, pkgfile)?.arg("--update").status_checked()
}

pub fn run_mandb(ctx: &ExecutionContext) -> Result<()> {
    let mandb = require("mandb")?;

    if !ctx.config().enable_mandb() {
        return Err(SkipStep(t!("ManDB isn't enabled").to_string()).into());
    }

    print_separator(t!("System Manuals"));

    let sudo = ctx.require_sudo()?;
    sudo.execute(ctx, &mandb)?.status_checked()?;

    print_separator(t!("User Manuals"));

    ctx.execute(&mandb).arg("--user-db").status_checked()
}

pub fn run_packer_nu(ctx: &ExecutionContext) -> Result<()> {
    let nu = require("nu")?;
    let packer_home = HOME_DIR.join(".local/share/nushell/packer");

    packer_home.clone().require()?;

    print_separator("packer.nu");

    ctx.execute(nu)
        .env("PWD", "/")
        .env("NU_PACKER_HOME", packer_home)
        .args([
            "-c",
            "use ~/.local/share/nushell/packer/start/packer.nu/api_layer/packer.nu; packer update",
        ])
        .status_checked()
}

fn upgrade_clearlinux(ctx: &ExecutionContext) -> Result<()> {
    let swupd = require("swupd")?;
    let sudo = ctx.require_sudo()?;

    let mut cmd = sudo.execute(ctx, &swupd)?;
    cmd.arg("update");
    if ctx.config().yes(Step::System) {
        cmd.arg("--assume=yes");
    }
    cmd.status_checked()?;

    Ok(())
}

fn upgrade_exherbo(ctx: &ExecutionContext) -> Result<()> {
    let cave = require("cave")?;
    let eclectic = require("eclectic")?;
    let sudo = ctx.require_sudo()?;

    sudo.execute(ctx, &cave)?.arg("sync").status_checked()?;

    sudo.execute(ctx, &cave)?
        .args(["resolve", "world", "-c1", "-Cs", "-km", "-Km", "-x"])
        .status_checked()?;

    if ctx.config().cleanup() {
        sudo.execute(ctx, &cave)?.args(["purge", "-x"]).status_checked()?;
    }

    sudo.execute(ctx, &cave)?
        .args(["fix-linkage", "-x", "--", "-Cs"])
        .status_checked()?;

    sudo.execute(ctx, &eclectic)?
        .args(["config", "interactive"])
        .status_checked()?;

    Ok(())
}

fn upgrade_nixos(ctx: &ExecutionContext) -> Result<()> {
    let sudo = ctx.require_sudo()?;
    let nix_handler = ctx.config().nix_handler();
    let nh_switch_args = NhSwitchArgs {
        step: "system",
        installable_type: "os",
        specific_var: "NH_OS_FLAKE",
        print_separator: false,
    };
    let can_nh_switch = can_nh_switch(&nh_switch_args);

    match (nix_handler, can_nh_switch) {
        // nh is available and we want it
        (NixHandler::Autodetect | NixHandler::Nh, Ok(nh)) => {
            nh_switch(ctx, &nh, &nh_switch_args)?;
        }
        // nh is not available but we need it
        (NixHandler::Nh, Err(e)) => return Err(e),
        // nh is not available and we don't need it, so we fall back
        (NixHandler::Autodetect, Err(_))
        // We need vanilla
        | (NixHandler::Vanilla, _) => {
            let mut command = sudo.execute(ctx, "/run/current-system/sw/bin/nixos-rebuild")?;
            command.args(["switch", "--upgrade"]);

            if let Some(args) = ctx.config().nix_arguments() {
                command.args(args.split_whitespace());
            }
            command.status_checked()?;
        }
    }

    // TODO: maybe use `nh clean` when available&&wanted ?
    if ctx.config().cleanup() {
        sudo.execute(ctx, "/run/current-system/sw/bin/nix-collect-garbage")?
            .arg("-d")
            .status_checked()?;
    }

    Ok(())
}

fn upgrade_neon(ctx: &ExecutionContext) -> Result<()> {
    // KDE neon is ubuntu based but uses it's own manager, pkcon
    // running apt update with KDE neon is an error
    // in theory rpm based distributions use pkcon as well, though that
    // seems rare
    // if that comes up we need to create a Distribution::PackageKit or some such

    let pkcon = require("pkcon")?;
    let sudo = ctx.require_sudo()?;

    // pkcon ignores update with update and refresh provided together
    sudo.execute(ctx, &pkcon)?.arg("refresh").status_checked()?;

    let mut exe = sudo.execute(ctx, &pkcon)?;
    let cmd = exe.arg("update");
    if ctx.config().yes(Step::System) {
        cmd.arg("-y");
    }
    if ctx.config().cleanup() {
        cmd.arg("--autoremove");
    }
    // from pkcon man, exit code 5 is 'Nothing useful was done.'
    cmd.status_checked_with_codes(&[5])?;

    Ok(())
}

fn upgrade_kde_linux(ctx: &ExecutionContext) -> Result<()> {
    let updatectl = require("updatectl")?;
    let mut command = ctx.execute(updatectl);
    command.arg("update");
    command.status_checked()?;

    Ok(())
}

// `dnf4` runs `needrestart` itself via the EPEL plugin during a system upgrade, but `dnf5`
// doesn't. The plugin config exists in both cases, so `dnf` version check is needed here.
fn dnf_runs_needrestart(ctx: &ExecutionContext) -> bool {
    if !Path::new("/etc/dnf/plugins/needrestart.conf").exists() {
        return false;
    }
    let Some(dnf) = which("dnf") else {
        return false;
    };
    ctx.execute(&dnf)
        .always()
        .arg("--version")
        .output_checked_utf8()
        .map(|output| !output.stdout.contains("dnf5"))
        .unwrap_or(false)
}

pub fn run_needrestart(ctx: &ExecutionContext) -> Result<()> {
    let needrestart = require("needrestart")?;

    // `needrestart` should be skipped if system update will run and its hooks exist
    const HOOKS: &[&str] = &[
        "/usr/share/libalpm/hooks/needrestart.hook",
        "/etc/pacman.d/hooks/needrestart.hook",
        "/etc/apt/apt.conf.d/99needrestart",
    ];

    if (HOOKS.iter().any(|hook| Path::new(hook).exists()) || dnf_runs_needrestart(ctx))
        && ctx.config().should_run(Step::System)
    {
        return Err(SkipStep(String::from(t!("needrestart will be ran by the package manager"))).into());
    }

    print_separator(t!("Check for needed restarts"));

    let sudo = ctx.require_sudo()?;
    sudo.execute(ctx, &needrestart)?.status_checked()?;

    Ok(())
}

pub fn run_fwupdmgr(ctx: &ExecutionContext) -> Result<()> {
    let fwupdmgr = require("fwupdmgr")?;

    if *IS_WSL {
        return Err(SkipStep(t!("Should not run in WSL").to_string()).into());
    }

    print_separator(t!("Firmware upgrades"));

    ctx.execute(&fwupdmgr).arg("refresh").status_checked_with_codes(&[2])?;

    let mut updmgr = ctx.execute(&fwupdmgr);

    if ctx.config().firmware_upgrade() {
        updmgr.arg("update");
        if ctx.config().yes(Step::System) {
            updmgr.arg("-y");
        }
        updmgr.status_checked_with_codes(&[2])
    } else {
        updmgr.arg("get-updates");

        // Exit 0 from `fwupdmgr get-updates` means firmware updates are available.
        // Exit 2 means no updates. When updates exist but `firmware_upgrade` is
        // disabled, hint to the user that they can apply them manually.
        let has_updates = std::cell::Cell::new(false);
        updmgr.status_checked_with(|status| {
            if status.success() {
                has_updates.set(true);
                Ok(())
            } else if status.code() == Some(2) {
                Ok(())
            } else {
                Err(())
            }
        })?;

        if has_updates.get() {
            print_warning(t!(
                "fwupdmgr found firmware updates, but applying these is disabled in the config. Run 'fwupdmgr update' to update firmware manually."
            ));
        }

        Ok(())
    }
}

pub fn run_flatpak(ctx: &ExecutionContext) -> Result<()> {
    let flatpak = require("flatpak")?;

    let cleanup = ctx.config().cleanup();
    let yes = ctx.config().yes(Step::Flatpak);
    print_separator("Flatpak User Packages");

    let mut update_args = vec!["update", "--user"];
    if yes {
        update_args.push("-y");
    }
    ctx.execute(&flatpak).args(&update_args).status_checked()?;

    if cleanup {
        let mut cleanup_args = vec!["uninstall", "--user", "--unused"];
        if yes {
            cleanup_args.push("-y");
        }
        ctx.execute(&flatpak).args(&cleanup_args).status_checked()?;
    }

    print_separator(t!("Flatpak System Packages"));
    if ctx.config().flatpak_use_sudo() || std::env::var("SSH_CLIENT").is_ok() {
        let sudo = ctx.require_sudo()?;
        let mut update_args = vec!["update", "--system"];
        if yes {
            update_args.push("-y");
        }
        sudo.execute(ctx, &flatpak)?.args(&update_args).status_checked()?;
        if cleanup {
            let mut cleanup_args = vec!["uninstall", "--system", "--unused"];
            if yes {
                cleanup_args.push("-y");
            }
            sudo.execute(ctx, &flatpak)?.args(&cleanup_args).status_checked()?;
        }
    } else {
        let mut update_args = vec!["update", "--system"];
        if yes {
            update_args.push("-y");
        }
        ctx.execute(&flatpak).args(&update_args).status_checked()?;
        if cleanup {
            let mut cleanup_args = vec!["uninstall", "--system", "--unused"];
            if yes {
                cleanup_args.push("-y");
            }
            ctx.execute(flatpak).args(&cleanup_args).status_checked()?;
        }
    }

    Ok(())
}

pub fn run_snap(ctx: &ExecutionContext) -> Result<()> {
    let snap = require("snap")?;

    if !PathBuf::from("/var/snapd.socket").exists() && !PathBuf::from("/run/snapd.socket").exists() {
        return Err(SkipStep(t!("Snapd socket does not exist").to_string()).into());
    }
    print_separator("snap");

    let sudo = ctx.require_sudo()?;
    sudo.execute(ctx, &snap)?.arg("refresh").status_checked()
}

pub fn run_soar(ctx: &ExecutionContext) -> Result<()> {
    let soar = require("soar")?;

    print_separator("soar");

    ctx.execute(&soar).arg("sync").status_checked()?;
    ctx.execute(&soar).arg("update").status_checked()?;
    ctx.execute(&soar).args(["self", "update"]).status_checked()
}

pub fn run_pihole_update(ctx: &ExecutionContext) -> Result<()> {
    let pihole = require("pihole")?;
    Path::new("/opt/pihole/update.sh").require()?;

    print_separator("pihole");

    let sudo = ctx.require_sudo()?;
    sudo.execute(ctx, &pihole)?.arg("-up").status_checked()
}

pub fn run_protonup_update(ctx: &ExecutionContext) -> Result<()> {
    let protonup = require("protonup")?;

    print_separator("protonup");

    let mut cmd = ctx.execute(protonup);
    if ctx.config().yes(Step::Protonup) {
        cmd.arg("--yes");
    }
    cmd.status_checked()?;

    Ok(())
}

pub fn run_distrobox_update(ctx: &ExecutionContext) -> Result<()> {
    let distrobox = require("distrobox")?;

    print_separator("Distrobox");
    match (
        match (
            ctx.execute(distrobox).arg("upgrade"),
            ctx.config().distrobox_containers(),
        ) {
            (r, Some(c)) => {
                if c.is_empty() {
                    return Err(SkipStep(t!("You need to specify at least one container").to_string()).into());
                }
                r.args(c)
            }
            (r, None) => r.arg("--all"),
        },
        ctx.config().distrobox_root(),
    ) {
        (r, true) => r.arg("--root"),
        (r, false) => r,
    }
    .status_checked()
}

pub fn run_dkp_pacman_update(ctx: &ExecutionContext) -> Result<()> {
    let dkp_pacman = require("dkp-pacman")?;

    print_separator("Devkitpro pacman");

    let sudo = ctx.require_sudo()?;

    sudo.execute(ctx, &dkp_pacman)?.arg("-Syu").status_checked()?;

    if ctx.config().cleanup() {
        sudo.execute(ctx, &dkp_pacman)?.arg("-Scc").status_checked()?;
    }

    Ok(())
}

pub fn run_config_update(ctx: &ExecutionContext) -> Result<()> {
    // The `config_update` step always requests user input, so when running with `--yes` we need to skip the step entirely
    if ctx.config().yes(Step::ConfigUpdate) {
        return Err(SkipStep(t!("Skipped in --yes").to_string()).into());
    }

    if let Ok(etc_update) = require("etc-update") {
        print_separator(t!("Configuration update"));
        let sudo = ctx.require_sudo()?;
        sudo.execute(ctx, etc_update)?.status_checked()?;
    } else if let Ok(pacdiff) = require("pacdiff") {
        // When `DIFFPROG` is unset, `pacdiff` uses `vim` by default
        if std::env::var("DIFFPROG").is_err() {
            require("vim")?;
        }

        print_separator(t!("Configuration update"));
        let sudo = ctx.require_sudo()?;
        sudo.execute_opts(ctx, &pacdiff, SudoExecuteOpts::new().preserve_env_list(&["DIFFPROG"]))?
            .status_checked()?;
    }

    Ok(())
}

pub fn run_lure_update(ctx: &ExecutionContext) -> Result<()> {
    let lure = require("lure")?;

    print_separator("LURE");

    let mut exe = ctx.execute(lure);

    if ctx.config().yes(Step::Lure) {
        exe.args(["-i=false", "up"]);
    } else {
        exe.arg("up");
    }

    exe.status_checked()
}

pub fn run_waydroid(ctx: &ExecutionContext) -> Result<()> {
    let waydroid = require("waydroid")?;

    let status = ctx.execute(&waydroid).arg("status").output_checked_utf8()?;
    // example output of `waydroid status`:
    //
    // ```sh
    // $ waydroid status
    // Session:        RUNNING
    // Container:      RUNNING
    // Vendor type:    MAINLINE
    // IP address:     192.168.240.112
    // Session user:   w568w(1000)
    // Wayland display:        wayland-0
    // ```
    //
    // ```sh
    // $ waydroid status
    // Session:        STOPPED
    // Vendor type:    MAINLINE
    // ```
    let session = status
        .stdout
        .lines()
        .find(|line| line.contains("Session:"))
        .unwrap_or_else(|| panic!("the output of `waydroid status` should contain `Session:`"));
    let is_container_running = session.contains("RUNNING");
    let assume_yes = ctx.config().yes(Step::Waydroid);

    print_separator("Waydroid");

    if is_container_running && !assume_yes {
        let update_allowed = prompt_yesno(&t!(
            "Going to execute `waydroid upgrade`, which would STOP the running container, is this ok?"
        ))?;
        if !update_allowed {
            return Err(
                SkipStep(t!("Skip the Waydroid step because the user don't want to proceed").to_string()).into(),
            );
        }
    }

    let sudo = ctx.require_sudo()?;
    sudo.execute(ctx, &waydroid)?.arg("upgrade").status_checked()
}

pub fn run_auto_cpufreq(ctx: &ExecutionContext) -> Result<()> {
    let auto_cpu_freq = require("auto-cpufreq")?.canonicalize()?;

    // The fix is *auto_cpu_freq != *"/usr/local/bin/auto-cpufreq", but that impl is not available in MSRV yet
    // TODO: once MSRV is bumped high enough that it is, remove this and apply lint
    #[expect(clippy::cmp_owned)]
    if auto_cpu_freq != PathBuf::from("/usr/local/bin/auto-cpufreq") {
        return Err(SkipStep(String::from(
            "`auto-cpufreq` was not installed by the official installer, but presumably by a package manager.",
        ))
        .into());
    }

    print_separator("auto-cpufreq");

    let sudo = ctx.require_sudo()?;
    sudo.execute(ctx, &auto_cpu_freq)?.arg("--update").status_checked()
}

pub fn run_gearlever(ctx: &ExecutionContext) -> Result<()> {
    let native = require("gearlever").is_ok();

    if !native {
        require_flatpak(ctx, "it.mijorus.gearlever")?;
    }

    print_separator(if native { "Gear Lever" } else { "Gear Lever (Flatpak)" });

    let mut list_updates = if native {
        ctx.execute(require("gearlever")?).always()
    } else {
        let flatpak = require("flatpak")?;
        let mut cmd = ctx.execute(&flatpak).always();
        cmd.args(["run", "it.mijorus.gearlever"]);
        cmd
    };
    list_updates.arg("--list-updates");

    let list_output = list_updates.output_checked_utf8()?;
    if list_output.stdout.trim() == "No updates available" {
        std::io::stdout().write_all(list_output.stdout.as_bytes())?;
        std::io::stderr().write_all(list_output.stderr.as_bytes())?;
        return Ok(());
    }

    let mut cmd = if native {
        ctx.execute(require("gearlever")?)
    } else {
        let flatpak = require("flatpak")?;
        let mut cmd = ctx.execute(&flatpak);
        cmd.args(["run", "it.mijorus.gearlever"]);
        cmd
    };
    cmd.args(["--update", "--all"]);

    if ctx.config().yes(Step::Gearlever) {
        cmd.arg("--yes");
    }

    cmd.status_checked()
}

pub fn run_cinnamon_spices_updater(ctx: &ExecutionContext) -> Result<()> {
    let cinnamon_spice_updater = require("cinnamon-spice-updater")?;

    print_separator("Cinnamon spices");

    ctx.execute(cinnamon_spice_updater).arg("--update-all").status_checked()
}

pub fn run_pkgit(ctx: &ExecutionContext) -> Result<()> {
    let pkgit = require("pkgit")?;
    print_separator("Pkgit");
    ctx.execute(pkgit).arg("-u").status_checked()
}

pub fn run_protonplus_update(ctx: &ExecutionContext) -> Result<()> {
    let (program, flatpak) = match require("protonplus") {
        Ok(protonplus) => (protonplus, false),
        Err(_) => {
            require_flatpak(ctx, "com.vysp3r.ProtonPlus")?;
            (require("flatpak")?, true)
        }
    };
    let cmd = || {
        let mut cmd = ctx.execute(&program);
        if flatpak {
            cmd.args(["run", "com.vysp3r.ProtonPlus"]);
        }
        cmd
    };

    if let Err(e) = cmd().args(["invalidarg67"]).output_checked()
        && let Some(TopgradeError::ProcessFailedWithOutput(_, _, stderr)) = e.downcast_ref()
        && stderr.contains("This application can not open files")
    {
        return Err(SkipStep("Updates unsupported for ProtonPlus versions under v0.5.17".to_string()).into());
    }

    print_separator(if flatpak { "ProtonPlus (Flatpak)" } else { "ProtonPlus" });

    cmd().args(["update", "all"]).status_checked()
}

#[cfg(test)]
mod tests {
    use super::*;

    fn test_template(os_release_file: &str, expected_distribution: Distribution) {
        let os_release = Ini::load_from_str(os_release_file).unwrap();
        assert_eq!(
            Distribution::parse_os_release(&os_release).unwrap(),
            expected_distribution
        );
    }

    #[test]
    fn test_wolfi() {
        test_template(include_str!("os_release/wolfi"), Distribution::Wolfi);
    }

    #[test]
    fn test_arch_linux() {
        test_template(include_str!("os_release/arch"), Distribution::Arch);
        test_template(include_str!("os_release/arch32"), Distribution::Arch);
    }

    #[test]
    fn test_aosc() {
        test_template(include_str!("os_release/aosc"), Distribution::AOSC);
    }

    #[test]
    fn test_centos() {
        test_template(include_str!("os_release/centos"), Distribution::CentOS);
    }

    #[test]
    fn test_rhel() {
        test_template(include_str!("os_release/rhel"), Distribution::CentOS);
    }

    #[test]
    fn test_clearlinux() {
        test_template(include_str!("os_release/clearlinux"), Distribution::ClearLinux);
    }

    #[test]
    fn test_debian() {
        test_template(include_str!("os_release/debian"), Distribution::Debian);
    }

    #[test]
    fn test_ubuntu() {
        test_template(include_str!("os_release/ubuntu"), Distribution::Debian);
    }

    #[test]
    fn test_mint() {
        test_template(include_str!("os_release/mint"), Distribution::Debian);
    }

    #[test]
    fn test_opensuse() {
        test_template(include_str!("os_release/opensuse"), Distribution::Suse);
    }

    #[test]
    fn test_oraclelinux() {
        test_template(include_str!("os_release/oracle"), Distribution::CentOS);
    }

    #[test]
    fn test_fedora() {
        test_template(include_str!("os_release/fedora"), Distribution::Fedora);
    }

    #[test]
    fn test_fedora_immutable() {
        test_template(
            include_str!("os_release/fedorasilverblue"),
            Distribution::FedoraImmutable,
        );
        test_template(include_str!("os_release/fedorakinoite"), Distribution::FedoraImmutable);
        test_template(include_str!("os_release/fedoraonyx"), Distribution::FedoraImmutable);
        test_template(include_str!("os_release/fedorasericea"), Distribution::FedoraImmutable);
        test_template(include_str!("os_release/fedoraiot"), Distribution::FedoraImmutable);
        test_template(
            include_str!("os_release/fedoraswayatomic"),
            Distribution::FedoraImmutable,
        );
    }

    #[test]
    fn test_manjaro() {
        test_template(include_str!("os_release/manjaro"), Distribution::Arch);
    }

    #[test]
    fn test_manjaro_arm() {
        test_template(include_str!("os_release/manjaro-arm"), Distribution::Arch);
    }

    #[test]
    fn test_gentoo() {
        test_template(include_str!("os_release/gentoo"), Distribution::Gentoo);
    }

    #[test]
    fn test_funtoo() {
        test_template(include_str!("os_release/funtoo"), Distribution::Gentoo);
    }

    #[test]
    fn test_exherbo() {
        test_template(include_str!("os_release/exherbo"), Distribution::Exherbo);
    }

    #[test]
    fn test_amazon_linux() {
        test_template(include_str!("os_release/amazon_linux"), Distribution::CentOS);
    }

    #[test]
    fn test_nixos() {
        test_template(include_str!("os_release/nixos"), Distribution::NixOS);
    }

    #[test]
    fn test_fedoraremixonwsl() {
        test_template(include_str!("os_release/fedoraremixforwsl"), Distribution::Fedora);
    }

    #[test]
    fn test_pengwinonwsl() {
        test_template(include_str!("os_release/pengwinonwsl"), Distribution::Debian);
    }

    #[test]
    fn test_artix() {
        test_template(include_str!("os_release/artix"), Distribution::Arch);
    }

    #[test]
    fn test_garuda() {
        test_template(include_str!("os_release/garuda"), Distribution::Arch);
    }

    #[test]
    fn test_pureos() {
        test_template(include_str!("os_release/pureos"), Distribution::Debian);
    }

    #[test]
    fn test_deepin() {
        test_template(include_str!("os_release/deepin"), Distribution::Debian);
    }

    #[test]
    fn test_vanilla() {
        test_template(include_str!("os_release/vanilla"), Distribution::Vanilla);
    }

    #[test]
    fn test_solus() {
        test_template(include_str!("os_release/solus"), Distribution::Solus);
    }

    #[test]
    fn test_nobara() {
        test_template(include_str!("os_release/nobara"), Distribution::Nobara);
    }

    #[test]
    fn test_nilrt() {
        test_template(include_str!("os_release/nilrt"), Distribution::NILRT);
    }

    #[test]
    fn test_kde_linux() {
        test_template(include_str!("os_release/kde-linux"), Distribution::KDELinux);
    }

    #[test]
    fn test_coreos() {
        test_template(include_str!("os_release/coreos"), Distribution::FedoraImmutable);
    }

    #[test]
    fn test_aurora() {
        test_template(include_str!("os_release/aurora"), Distribution::FedoraImmutable);
    }

    #[test]
    fn test_bluefin() {
        test_template(include_str!("os_release/bluefin"), Distribution::FedoraImmutable);
    }

    #[test]
    fn test_bazzite() {
        test_template(include_str!("os_release/bazzite"), Distribution::FedoraImmutable);
    }

    #[test]
    fn test_cachyos() {
        test_template(include_str!("os_release/cachyos"), Distribution::Arch);
    }

    #[test]
    fn test_origami() {
        test_template(include_str!("os_release/origami"), Distribution::FedoraImmutable);
        test_template(include_str!("os_release/origami-nvidia"), Distribution::FedoraImmutable);
        test_template(include_str!("os_release/origami-test"), Distribution::FedoraImmutable);
    }
}
