package config

import (
	"testing"

	"git.gostacks.org/iwasforcedtobehere/WiseTLP/autotlp/pkg/types"
)

func TestValidatePreferences(t *testing.T) {
	validPrefs := &types.UserPreferences{
		PowerProfile:        types.PowerProfileBalanced,
		UseCase:             types.UseCaseGeneral,
		BatteryPriority:     types.BatteryPriorityBalanced,
		PerformanceMode:     types.PerformanceModeAdaptive,
		CustomSettings:      make(map[string]interface{}),
		SpecialRequirements: []string{},
	}

	if err := ValidatePreferences(validPrefs); err != nil {
		t.Errorf("ValidatePreferences() with valid preferences failed: %v", err)
	}

	if err := ValidatePreferences(nil); err == nil {
		t.Error("ValidatePreferences() with nil preferences should fail")
	}

	invalidPrefs := &types.UserPreferences{
		PowerProfile:        "invalid_profile",
		UseCase:             types.UseCaseGeneral,
		BatteryPriority:     types.BatteryPriorityBalanced,
		PerformanceMode:     types.PerformanceModeAdaptive,
		CustomSettings:      make(map[string]interface{}),
		SpecialRequirements: []string{},
	}

	if err := ValidatePreferences(invalidPrefs); err == nil {
		t.Error("ValidatePreferences() with invalid power profile should fail")
	}

	invalidPrefs2 := &types.UserPreferences{
		PowerProfile:        types.PowerProfileBalanced,
		UseCase:             "invalid_usecase",
		BatteryPriority:     types.BatteryPriorityBalanced,
		PerformanceMode:     types.PerformanceModeAdaptive,
		CustomSettings:      make(map[string]interface{}),
		SpecialRequirements: []string{},
	}

	if err := ValidatePreferences(invalidPrefs2); err == nil {
		t.Error("ValidatePreferences() with invalid use case should fail")
	}

	// Test conflicting preferences
	conflictingPrefs := &types.UserPreferences{
		PowerProfile:        types.PowerProfilePowerSaving,
		UseCase:             types.UseCaseGeneral,
		BatteryPriority:     types.BatteryPriorityBalanced,
		PerformanceMode:     types.PerformanceModeMaximum,
		CustomSettings:      make(map[string]interface{}),
		SpecialRequirements: []string{},
	}

	if err := ValidatePreferences(conflictingPrefs); err == nil {
		t.Error("ValidatePreferences() with conflicting preferences should fail")
	}
}

func TestGetRecommendedSettings(t *testing.T) {
	// Test performance profile
	perfPrefs := &types.UserPreferences{
		PowerProfile:        types.PowerProfilePerformance,
		UseCase:             types.UseCaseGaming,
		BatteryPriority:     types.BatteryPriorityBalanced,
		PerformanceMode:     types.PerformanceModeMaximum,
		CustomSettings:      make(map[string]interface{}),
		SpecialRequirements: []string{},
	}

	settings := GetRecommendedSettings(perfPrefs)

	// Should have TLP_ENABLE
	if settings["TLP_ENABLE"] != "1" {
		t.Error("TLP_ENABLE should be set to '1'")
	}

	// Performance profile should use performance governor
	if settings["CPU_SCALING_GOVERNOR_ON_AC"] != "performance" {
		t.Error("Performance profile should use performance governor on AC")
	}

	// Test power saving profile
	powerSavePrefs := &types.UserPreferences{
		PowerProfile:        types.PowerProfilePowerSaving,
		UseCase:             types.UseCaseOffice,
		BatteryPriority:     types.BatteryPriorityRuntime,
		PerformanceMode:     types.PerformanceModeEfficient,
		CustomSettings:      make(map[string]interface{}),
		SpecialRequirements: []string{},
	}

	settings2 := GetRecommendedSettings(powerSavePrefs)

	// Power saving should use powersave governor
	if settings2["CPU_SCALING_GOVERNOR_ON_AC"] != "powersave" {
		t.Error("Power saving profile should use powersave governor on AC")
	}

	if settings2["CPU_SCALING_GOVERNOR_ON_BAT"] != "powersave" {
		t.Error("Power saving profile should use powersave governor on battery")
	}

	// Test custom settings
	customPrefs := &types.UserPreferences{
		PowerProfile:    types.PowerProfileCustom,
		UseCase:         types.UseCaseGeneral,
		BatteryPriority: types.BatteryPriorityBalanced,
		PerformanceMode: types.PerformanceModeAdaptive,
		CustomSettings: map[string]interface{}{
			"cpu_governor_ac":      "ondemand",
			"cpu_governor_battery": "conservative",
			"disk_apm_ac":          "200",
			"disk_apm_battery":     "100",
		},
		SpecialRequirements: []string{},
	}

	settings3 := GetRecommendedSettings(customPrefs)

	// Custom settings should override defaults
	if settings3["CPU_SCALING_GOVERNOR_ON_AC"] != "ondemand" {
		t.Error("Custom CPU governor AC setting should be applied")
	}

	if settings3["CPU_SCALING_GOVERNOR_ON_BAT"] != "conservative" {
		t.Error("Custom CPU governor battery setting should be applied")
	}

	if settings3["DISK_APM_LEVEL_ON_AC"] != "200" {
		t.Error("Custom disk APM AC setting should be applied")
	}

	if settings3["DISK_APM_LEVEL_ON_BAT"] != "100" {
		t.Error("Custom disk APM battery setting should be applied")
	}

	// Test special requirements
	wifiPrefs := &types.UserPreferences{
		PowerProfile:        types.PowerProfileBalanced,
		UseCase:             types.UseCaseGeneral,
		BatteryPriority:     types.BatteryPriorityBalanced,
		PerformanceMode:     types.PerformanceModeAdaptive,
		CustomSettings:      make(map[string]interface{}),
		SpecialRequirements: []string{"Maximum WiFi performance"},
	}

	settings4 := GetRecommendedSettings(wifiPrefs)

	// WiFi performance requirement should disable power saving
	if settings4["WIFI_PWR_ON_AC"] != "off" || settings4["WIFI_PWR_ON_BAT"] != "off" {
		t.Error("Maximum WiFi performance should disable WiFi power saving")
	}
}
