package collector

import (
	"os"
	"os/exec"
	"strconv"
	"strings"
	"time"
)

// Basis-Systemdaten sammeln

func CollectHostname() string {
	if data, err := os.ReadFile("/etc/hostname"); err == nil {
		return strings.TrimSpace(string(data))
	}

	// Fallback
	if hostname, err := os.Hostname(); err == nil {
		return hostname
	}

	return "unknown"
}

func CollectOSVersion() string {
	// Versuche verschiedene Wege die OS-Version zu bestimmen
	
	// 1. /etc/os-release (Standard für moderne Linux-Distributionen)
	if data, err := os.ReadFile("/etc/os-release"); err == nil {
		lines := strings.Split(string(data), "\n")
		var name, version string
		
		for _, line := range lines {
			line = strings.TrimSpace(line)
			if strings.HasPrefix(line, "NAME=") {
				name = strings.Trim(strings.SplitN(line, "=", 2)[1], "\"")
			} else if strings.HasPrefix(line, "VERSION=") {
				version = strings.Trim(strings.SplitN(line, "=", 2)[1], "\"")
			}
		}
		
		if name != "" {
			if version != "" {
				return name + " " + version
			}
			return name
		}
	}
	
	// 2. /etc/debian_version (Debian/Ubuntu)
	if data, err := os.ReadFile("/etc/debian_version"); err == nil {
		version := strings.TrimSpace(string(data))
		return "Debian " + version
	}
	
	// 3. /etc/redhat-release (RHEL/CentOS)
	if data, err := os.ReadFile("/etc/redhat-release"); err == nil {
		return strings.TrimSpace(string(data))
	}
	
	// 4. uname fallback
	if output, err := exec.Command("/usr/bin/uname", "-sr").CombinedOutput(); err == nil {
		return strings.TrimSpace(string(output))
	}
	
	return "Linux (unknown)"
}

func CollectUptime() int64 {
	if data, err := os.ReadFile("/proc/uptime"); err == nil {
		parts := strings.Fields(string(data))
		if len(parts) > 0 {
			if uptime, err := strconv.ParseFloat(parts[0], 64); err == nil {
				return int64(uptime)
			}
		}
	}
	
	// Fallback: Boot-Zeit berechnen
	if stat, err := os.Stat("/proc/1"); err == nil {
		bootTime := stat.ModTime()
		return int64(time.Since(bootTime).Seconds())
	}
	
	return 0
}

// Hardware-Informationen sammeln
type HardwareInfo struct {
	Manufacturer string
	Model        string
	Serial       string
	BIOSVersion  string
}

func CollectHardware() HardwareInfo {
	hw := HardwareInfo{}
	
	// DMI-Informationen aus /sys/class/dmi/id/ lesen
	if data, err := os.ReadFile("/sys/class/dmi/id/sys_vendor"); err == nil {
		hw.Manufacturer = strings.TrimSpace(string(data))
	}
	
	if data, err := os.ReadFile("/sys/class/dmi/id/product_name"); err == nil {
		hw.Model = strings.TrimSpace(string(data))
	}
	
	if data, err := os.ReadFile("/sys/class/dmi/id/product_serial"); err == nil {
		hw.Serial = strings.TrimSpace(string(data))
	}
	
	if data, err := os.ReadFile("/sys/class/dmi/id/bios_version"); err == nil {
		hw.BIOSVersion = strings.TrimSpace(string(data))
	}
	
	// Fallback für Manufacturer
	if hw.Manufacturer == "" {
		if data, err := os.ReadFile("/sys/class/dmi/id/board_vendor"); err == nil {
			hw.Manufacturer = strings.TrimSpace(string(data))
		}
	}
	
	// Fallback für Model
	if hw.Model == "" {
		if data, err := os.ReadFile("/sys/class/dmi/id/board_name"); err == nil {
			hw.Model = strings.TrimSpace(string(data))
		}
	}
	
	return hw
}