package web

import (
	"os/exec"
	"strings"
)

// IsCommandAvailable checks if a command is available in PATH
func IsCommandAvailable(cmd string) bool {
	_, err := exec.LookPath(cmd)
	return err == nil
}

// IsFlatpakAvailable checks if flatpak package manager is available
func IsFlatpakAvailable() bool {
	if !IsCommandAvailable("flatpak") {
		return false
	}

	// Verify flatpak is working
	cmd := exec.Command("flatpak", "--version")
	err := cmd.Run()
	return err == nil
}

// IsBrewAvailable checks if Homebrew is available
func IsBrewAvailable() bool {
	if !IsCommandAvailable("brew") {
		return false
	}

	cmd := exec.Command("brew", "--version")
	err := cmd.Run()
	return err == nil
}

// IsPackageManagerAvailable checks if a package manager is available
func IsPackageManagerAvailable(pmFamily string) bool {
	switch strings.ToLower(pmFamily) {
	case "debian":
		return IsCommandAvailable("apt")
	case "rhel":
		return IsCommandAvailable("dnf") || IsCommandAvailable("yum")
	case "arch":
		return IsCommandAvailable("pacman")
	}
	return false
}

// GetPackageManagerCommand returns the command name for a package manager family
func GetPackageManagerCommand(pmFamily string) string {
	switch strings.ToLower(pmFamily) {
	case "debian":
		return "apt"
	case "rhel":
		if IsCommandAvailable("dnf") {
			return "dnf"
		}
		if IsCommandAvailable("yum") {
			return "yum"
		}
		return ""
	case "arch":
		return "pacman"
	}
	return ""
}
