package dashboard

import (
	"testing"

	"github.com/tidbcloud/AgentTeam/desktop/internal/api/dto"
	"github.com/tidbcloud/AgentTeam/desktop/internal/profile"
)

func intPtr(v int) *int {
	return &v
}

func TestSettingsRequestToConfig(t *testing.T) {
	t.Run("basic conversion", func(t *testing.T) {
		req := dto.SettingsRequest{
			Host:         "0.0.0.0",
			Port:         intPtr(3000),
			Language:     "en",
			DefaultAgent: "agent-1",
		}
		cfg := settingsRequestToConfig(req)

		if cfg.Host != "0.0.0.0" {
			t.Errorf("Host = %q, want %q", cfg.Host, "0.0.0.0")
		}
		if cfg.Port != 3000 {
			t.Errorf("Port = %d, want 3000", cfg.Port)
		}
		if cfg.Language != "en" {
			t.Errorf("Language = %q, want %q", cfg.Language, "en")
		}
		if cfg.DefaultAgent != "agent-1" {
			t.Errorf("DefaultAgent = %q, want %q", cfg.DefaultAgent, "agent-1")
		}
	})

	t.Run("log fields are converted", func(t *testing.T) {
		req := dto.SettingsRequest{
			Log: dto.SettingsLog{Dir: "/var/log/agentteam", File: "agentteam.log"},
		}
		cfg := settingsRequestToConfig(req)
		if cfg.Log.Dir != "/var/log/agentteam" {
			t.Errorf("Log.Dir = %q, want %q", cfg.Log.Dir, "/var/log/agentteam")
		}
		if cfg.Log.File != "agentteam.log" {
			t.Errorf("Log.File = %q, want %q", cfg.Log.File, "agentteam.log")
		}
	})

	t.Run("desktop UI fields are converted", func(t *testing.T) {
		optimize := false
		req := dto.SettingsRequest{
			UI: dto.SettingsUI{
				OptimizeBackgroundResources: &optimize,
			},
		}
		cfg := settingsRequestToConfig(req)
		if cfg.UI.OptimizeBackgroundResources == nil || *cfg.UI.OptimizeBackgroundResources {
			t.Fatalf("OptimizeBackgroundResources = %#v, want false pointer", cfg.UI.OptimizeBackgroundResources)
		}
	})
}

func TestToSettingsResponse(t *testing.T) {
	t.Run("round-trip preserves all fields", func(t *testing.T) {
		cfg := &profile.Config{
			Host:         "0.0.0.0",
			Port:         3000,
			PIDFile:      "/tmp/agentteam.pid",
			Language:     "en",
			DefaultAgent: "agent-1",
			Log:          profile.Log{Dir: "/var/log", File: "agentteam.log"},
		}
		resp := toSettingsResponse(cfg)

		if resp.Host != "0.0.0.0" {
			t.Errorf("Host = %q, want %q", resp.Host, "0.0.0.0")
		}
		if resp.Port != 3000 {
			t.Errorf("Port = %d, want 3000", resp.Port)
		}
		if resp.Language != "en" {
			t.Errorf("Language = %q, want %q", resp.Language, "en")
		}
		if resp.UI.Language != "en" {
			t.Errorf("UI.Language = %q, want fallback en", resp.UI.Language)
		}
		if resp.DefaultAgent != "agent-1" {
			t.Errorf("DefaultAgent = %q, want %q", resp.DefaultAgent, "agent-1")
		}
		if resp.Log.Dir != "/var/log" {
			t.Errorf("Log.Dir = %q, want %q", resp.Log.Dir, "/var/log")
		}
		if resp.UI.OptimizeBackgroundResources == nil || !*resp.UI.OptimizeBackgroundResources {
			t.Fatalf("UI.OptimizeBackgroundResources = %#v, want default true pointer", resp.UI.OptimizeBackgroundResources)
		}
	})

	t.Run("security fields are not exposed", func(t *testing.T) {
		cfg := &profile.Config{
			Host: "127.0.0.1",
			Port: 8080,
			Security: profile.Security{
				PublicKey:  []byte("pub-key-data"),
				PrivateKey: []byte("priv-key-data"),
			},
		}
		resp := toSettingsResponse(cfg)
		if resp.Host != "127.0.0.1" {
			t.Errorf("Host = %q, want %q", resp.Host, "127.0.0.1")
		}
		if resp.Port != 8080 {
			t.Errorf("Port = %d, want 8080", resp.Port)
		}
	})
}
