package system

import (
	"context"
	"runtime"
	"testing"
)

func TestDetectPackageManager(t *testing.T) {
	tests := []struct {
		distro   string
		expected string
	}{
		{"ubuntu", "apt"},
		{"debian", "apt"},
		{"fedora", "dnf"},
		{"centos", "dnf"},
		{"arch", "pacman"},
		{"manjaro", "pacman"},
		{"opensuse", "zypper"},
		{"alpine", "apk"},
	}

	for _, test := range tests {
		result := detectPackageManager(test.distro)
		if result != test.expected {
			t.Errorf("detectPackageManager(%q) = %q, want %q", test.distro, result, test.expected)
		}
	}

	unknownResult := detectPackageManager("totally_unknown_distro")
	if unknownResult == "" {
		t.Error("detectPackageManager with unknown distro should return something")
	}
}

func TestDetectSystem(t *testing.T) {
	if runtime.GOOS != "linux" {
		t.Skip("Skipping Linux-specific test on non-Linux system")
	}

	ctx := context.Background()
	info, err := DetectSystem(ctx)
	if err != nil {
		t.Fatalf("DetectSystem() failed: %v", err)
	}

	if info.Distribution == "" {
		t.Error("Distribution should not be empty")
	}

	if info.Architecture == "" {
		t.Error("Architecture should not be empty")
	}

	if info.PackageManager == "" {
		t.Error("PackageManager should not be empty")
	}

	// Architecture should match runtime.GOARCH
	if info.Architecture != runtime.GOARCH {
		t.Errorf("Architecture = %q, want %q", info.Architecture, runtime.GOARCH)
	}
}

func TestGetChassisType(t *testing.T) {
	tests := []struct {
		input    int
		expected string
	}{
		{1, "Other"},
		{3, "Desktop"},
		{9, "Laptop"},
		{10, "Notebook"},
		{999, "Unknown"}, // Invalid type
		{0, "Unknown"},   // Invalid type
	}

	for _, test := range tests {
		result := getChassisType(test.input)
		if result != test.expected {
			t.Errorf("getChassisType(%d) = %q, want %q", test.input, result, test.expected)
		}
	}
}

func TestGatherSystemInfo(t *testing.T) {
	if runtime.GOOS != "linux" {
		t.Skip("Skipping Linux-specific test on non-Linux system")
	}

	ctx := context.Background()
	sysInfo, err := GatherSystemInfo(ctx)
	if err != nil {
		t.Fatalf("GatherSystemInfo() failed: %v", err)
	}

	// Basic validation
	if sysInfo.Distribution.ID == "" {
		t.Error("Distribution ID should not be empty")
	}

	if sysInfo.CPU.Architecture == "" {
		t.Error("CPU Architecture should not be empty")
	}

	if sysInfo.Memory.Total <= 0 {
		t.Error("Memory Total should be greater than 0")
	}

	// CPU should have at least 1 core (but may be 0 if /proc/cpuinfo is not accessible in test environment)
	if sysInfo.CPU.Cores < 0 {
		t.Error("CPU Cores should not be negative")
	}

	// If we can read CPU info, we should have at least 1 core
	// In test environments, this might not be available, so we just log it
	if sysInfo.CPU.Cores == 0 {
		t.Logf("Warning: CPU cores is 0, possibly due to test environment limitations")
	}
}
