package config

import (
	"os"
	"path/filepath"
	"testing"

	fileencoding "reasonix/internal/fileutil/encoding"
)

func TestLoadForEdit(t *testing.T) {
	dir := t.TempDir()
	path := filepath.Join(dir, "reasonix.toml")
	custom := `default_model = "custom"
[[providers]]
name = "custom"
kind = "openai"
base_url = "https://x"
model = "m"
api_key_env = "X_KEY"
`
	if err := os.WriteFile(path, []byte(custom), 0o644); err != nil {
		t.Fatal(err)
	}

	// Existing file: its providers/default override the built-in defaults, so a
	// reconfigure preserves the user's setup.
	cfg := LoadForEdit(path)
	if cfg.DefaultModel != "custom" {
		t.Errorf("default_model = %q, want custom", cfg.DefaultModel)
	}
	if len(cfg.Providers) != 1 || cfg.Providers[0].Name != "custom" {
		t.Errorf("providers = %v, want a single custom provider", cfg.Providers)
	}

	// Missing file: falls back to the built-in defaults.
	if cfg := LoadForEdit(filepath.Join(dir, "absent.toml")); cfg.DefaultModel != Default().DefaultModel {
		t.Errorf("missing-file default = %q, want %q", cfg.DefaultModel, Default().DefaultModel)
	}
}

func TestMergeTOMLProviderAccessPreservesExplicitEmpty(t *testing.T) {
	userPath := writeUserProviderAccess(t, "[desktop]\nprovider_access = []\n")

	access, declared, err := mergeTOMLProviderAccess([]string{userPath})
	if err != nil {
		t.Fatalf("mergeTOMLProviderAccess: %v", err)
	}
	if !declared {
		t.Fatal("provider_access declaration was not detected")
	}
	if access == nil || len(access) != 0 {
		t.Fatalf("provider_access = %#v, want a non-nil empty slice", access)
	}
}

func TestMergeTOMLProviderAccessIgnoresProjectOnlyList(t *testing.T) {
	isolateUserConfigHome(t)
	userPath := UserConfigPath()
	if err := os.MkdirAll(filepath.Dir(userPath), 0o755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(userPath, []byte("default_model = \"deepseek/deepseek-v4-flash\"\n"), 0o644); err != nil {
		t.Fatal(err)
	}
	projectPath := filepath.Join(t.TempDir(), "reasonix.toml")
	if err := os.WriteFile(projectPath, []byte("[desktop]\nprovider_access = [\"deepseek\"]\n"), 0o644); err != nil {
		t.Fatal(err)
	}

	access, declared, err := mergeTOMLProviderAccess([]string{userPath, projectPath})
	if err != nil {
		t.Fatalf("mergeTOMLProviderAccess: %v", err)
	}
	if declared || access != nil {
		t.Fatalf("project-only access = %#v (declared=%v); an undeclared user list means allow-all", access, declared)
	}
}

func TestMergeTOMLProviderAccessUnionsWhenUserDeclares(t *testing.T) {
	userPath := writeUserProviderAccess(t, "[desktop]\nprovider_access = [\"deepseek\"]\n")
	projectPath := filepath.Join(t.TempDir(), "reasonix.toml")
	if err := os.WriteFile(projectPath, []byte("[desktop]\nprovider_access = [\"project-b\"]\n"), 0o644); err != nil {
		t.Fatal(err)
	}

	access, declared, err := mergeTOMLProviderAccess([]string{userPath, projectPath})
	if err != nil {
		t.Fatalf("mergeTOMLProviderAccess: %v", err)
	}
	if !declared || len(access) != 2 || access[0] != "deepseek" || access[1] != "project-b" {
		t.Fatalf("access = %#v (declared=%v), want the union of both scopes", access, declared)
	}
}

func writeUserProviderAccess(t *testing.T, body string) string {
	t.Helper()
	isolateUserConfigHome(t)
	path := UserConfigPath()
	if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
		t.Fatal(err)
	}
	return path
}

func TestLoadForEditDecodesGB18030TOML(t *testing.T) {
	dir := t.TempDir()
	path := filepath.Join(dir, "config.toml")
	body := `default_model = "local/中文模型"

[[providers]]
name = "local"
kind = "openai"
base_url = "https://example.com/v1"
model = "中文模型"
api_key_env = "LOCAL_KEY"
`
	if err := os.WriteFile(path, fileencoding.Encode(body, fileencoding.GB18030), 0o644); err != nil {
		t.Fatal(err)
	}

	cfg := LoadForEdit(path)
	if cfg.DefaultModel != "local/中文模型" {
		t.Fatalf("default_model = %q", cfg.DefaultModel)
	}
	if len(cfg.Providers) != 1 || cfg.Providers[0].Model != "中文模型" {
		t.Fatalf("providers = %+v, want decoded Chinese model", cfg.Providers)
	}
}

func TestLoadForEditNormalizesLegacyMCPTiersWithoutWriting(t *testing.T) {
	dir := t.TempDir()
	path := filepath.Join(dir, "reasonix.toml")
	body := `
[[plugins]]
name = "playwright"
command = "npx"
tier = "lazy"

[[providers]]
name = "local"
kind = "openai"
base_url = "https://x"
model = "m"
`
	if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
		t.Fatal(err)
	}

	cfg := LoadForEdit(path)
	if len(cfg.Plugins) != 1 || cfg.Plugins[0].Tier != "" {
		t.Fatalf("plugins after migration = %+v, want empty tier", cfg.Plugins)
	}
	updated, err := os.ReadFile(path)
	if err != nil {
		t.Fatal(err)
	}
	if string(updated) != body {
		t.Fatalf("LoadForEdit must not rewrite config outside its caller's edit transaction:\n%s", updated)
	}
}

func TestLoadForEditReadOnlyStrictDoesNotMigrateDisk(t *testing.T) {
	path := filepath.Join(t.TempDir(), "reasonix.toml")
	body := []byte(`
[[plugins]]
name = "playwright"
command = "npx"
tier = "lazy"
`)
	if err := os.WriteFile(path, body, 0o644); err != nil {
		t.Fatal(err)
	}
	cfg, err := LoadForEditReadOnlyStrict(path)
	if err != nil {
		t.Fatal(err)
	}
	if len(cfg.Plugins) != 1 || cfg.Plugins[0].Tier != "" {
		t.Fatalf("read-only normalized plugins = %+v", cfg.Plugins)
	}
	after, err := os.ReadFile(path)
	if err != nil {
		t.Fatal(err)
	}
	if string(after) != string(body) {
		t.Fatalf("read-only load changed config on disk:\n%s", after)
	}
}

func TestLoadForEditIgnoresProjectDotEnvForProviderCredentials(t *testing.T) {
	project := t.TempDir()
	launch := t.TempDir()
	home := t.TempDir()
	path := filepath.Join(project, "reasonix.toml")
	body := `default_model = "custom/m"
[[providers]]
name = "custom"
kind = "openai"
base_url = "https://example.invalid/v1"
model = "m"
api_key_env = "PROJECT_ONLY_KEY"
`
	if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(filepath.Join(project, ".env"), []byte("PROJECT_ONLY_KEY=from-project\n"), 0o600); err != nil {
		t.Fatal(err)
	}
	t.Chdir(launch)
	t.Setenv("HOME", home)
	t.Setenv("USERPROFILE", home)
	t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
	t.Setenv("AppData", filepath.Join(home, "AppData"))
	t.Setenv("PROJECT_ONLY_KEY", "")
	os.Unsetenv("PROJECT_ONLY_KEY")

	cfg := LoadForEdit(path)
	provider, ok := cfg.Provider("custom")
	if !ok {
		t.Fatalf("provider missing from edited config: %+v", cfg.Providers)
	}
	if provider.Configured() {
		t.Fatalf("provider should not resolve api_key_env from project .env next to edited config")
	}
	if got := ResolveCredentialForRootGlobalFirst(project, "PROJECT_ONLY_KEY"); got.Set {
		t.Fatalf("credential = %+v, want project .env ignored for provider key", got)
	}
}
