package collector

import (
	"os/exec"
	"strings"
)

type HardwareInfo struct {
	Manufacturer string `json:"manufacturer"`
	Model        string `json:"model"`
	Serial       string `json:"serial"`
	BIOSVersion  string `json:"bios_version"`
}

func CollectHardware() HardwareInfo {
	info := HardwareInfo{}

	// Hersteller via kenv
	info.Manufacturer = kenvGet("smbios.system.maker")
	info.Model = kenvGet("smbios.system.product")
	info.Serial = kenvGet("smbios.system.serial")
	info.BIOSVersion = kenvGet("smbios.bios.version")

	// Fallback auf dmidecode wenn kenv leer
	if info.Manufacturer == "" {
		info.Manufacturer = dmidecodeGet("system-manufacturer")
	}
	if info.Model == "" {
		info.Model = dmidecodeGet("system-product-name")
	}
	if info.Serial == "" {
		info.Serial = dmidecodeGet("system-serial-number")
	}
	if info.BIOSVersion == "" {
		info.BIOSVersion = dmidecodeGet("bios-version")
	}

	return info
}

func kenvGet(key string) string {
	out, err := exec.Command("kenv", key).Output()
	if err != nil {
		return ""
	}
	return strings.TrimSpace(string(out))
}

func dmidecodeGet(keyword string) string {
	out, err := exec.Command("dmidecode", "-s", keyword).Output()
	if err != nil {
		return ""
	}
	return strings.TrimSpace(string(out))
}
