package config

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

	"github.com/muyue/muyue/internal/version"
)

func TestDefault(t *testing.T) {
	cfg := Default()
	if cfg.Version != version.Version {
		t.Errorf("Expected version %s, got %s", version.Version, cfg.Version)
	}
	if cfg.Profile.Pseudo != "muyue" {
		t.Errorf("Expected pseudo muyue, got %s", cfg.Profile.Pseudo)
	}
	if len(cfg.AI.Providers) == 0 {
		t.Error("Expected at least one AI provider")
	}
	found := false
	for _, p := range cfg.AI.Providers {
		if p.Name == "minimax" && p.Active {
			found = true
		}
	}
	if !found {
		t.Error("Expected minimax to be active")
	}
}

func TestSaveAndLoad(t *testing.T) {
	tmpDir := t.TempDir()
	origHome := os.Getenv("HOME")
	os.Setenv("HOME", tmpDir)
	defer os.Setenv("HOME", origHome)

	origConfig := os.Getenv("XDG_CONFIG_HOME")
	os.Setenv("XDG_CONFIG_HOME", filepath.Join(tmpDir, ".config"))
	defer os.Setenv("XDG_CONFIG_HOME", origConfig)

	cfg := Default()
	cfg.Profile.Name = "Test User"
	cfg.Profile.Email = "test@example.com"
	cfg.Profile.Pseudo = "tester"
	cfg.Profile.Languages = []string{"go", "python"}
	cfg.AI.Providers[0].APIKey = "test-key-123"

	if err := Save(cfg); err != nil {
		t.Fatalf("Save failed: %v", err)
	}

	if !Exists() {
		t.Error("Exists should return true after Save")
	}

	loaded, err := Load()
	if err != nil {
		t.Fatalf("Load failed: %v", err)
	}

	if loaded.Profile.Name != "Test User" {
		t.Errorf("Expected name Test User, got %s", loaded.Profile.Name)
	}
	if loaded.Profile.Pseudo != "tester" {
		t.Errorf("Expected pseudo tester, got %s", loaded.Profile.Pseudo)
	}
	if loaded.Profile.Email != "test@example.com" {
		t.Errorf("Expected email test@example.com, got %s", loaded.Profile.Email)
	}
	if len(loaded.Profile.Languages) != 2 {
		t.Errorf("Expected 2 languages, got %d", len(loaded.Profile.Languages))
	}
	if loaded.AI.Providers[0].APIKey != "test-key-123" {
		t.Error("API key should be decrypted on load")
	}
}

func TestExistsFalse(t *testing.T) {
	tmpDir := t.TempDir()
	origHome := os.Getenv("HOME")
	os.Setenv("HOME", tmpDir)
	defer os.Setenv("HOME", origHome)

	origConfig := os.Getenv("XDG_CONFIG_HOME")
	os.Setenv("XDG_CONFIG_HOME", filepath.Join(tmpDir, ".config"))
	defer os.Setenv("XDG_CONFIG_HOME", origConfig)

	if Exists() {
		t.Error("Exists should return false for non-existent config")
	}
}

func TestConfigDir(t *testing.T) {
	tmpDir := t.TempDir()
	origConfig := os.Getenv("XDG_CONFIG_HOME")
	os.Setenv("XDG_CONFIG_HOME", filepath.Join(tmpDir, ".config"))
	defer os.Setenv("XDG_CONFIG_HOME", origConfig)

	dir, err := ConfigDir()
	if err != nil {
		t.Fatalf("ConfigDir failed: %v", err)
	}
	expected := filepath.Join(tmpDir, ".config", "muyue")
	if dir != expected {
		t.Errorf("Expected %s, got %s", expected, dir)
	}
}

func TestConfigPath(t *testing.T) {
	tmpDir := t.TempDir()
	origConfig := os.Getenv("XDG_CONFIG_HOME")
	os.Setenv("XDG_CONFIG_HOME", filepath.Join(tmpDir, ".config"))
	defer os.Setenv("XDG_CONFIG_HOME", origConfig)

	path, err := ConfigPath()
	if err != nil {
		t.Fatalf("ConfigPath failed: %v", err)
	}
	expected := filepath.Join(tmpDir, ".config", "muyue", "config.yaml")
	if path != expected {
		t.Errorf("Expected %s, got %s", expected, path)
	}
}

func TestRoundtripEmptyFields(t *testing.T) {
	tmpDir := t.TempDir()
	origHome := os.Getenv("HOME")
	os.Setenv("HOME", tmpDir)
	defer os.Setenv("HOME", origHome)

	origConfig := os.Getenv("XDG_CONFIG_HOME")
	os.Setenv("XDG_CONFIG_HOME", filepath.Join(tmpDir, ".config"))
	defer os.Setenv("XDG_CONFIG_HOME", origConfig)

	cfg := Default()
	cfg.Profile.Name = ""
	cfg.AI.Providers[0].APIKey = ""

	if err := Save(cfg); err != nil {
		t.Fatalf("Save failed: %v", err)
	}

	loaded, err := Load()
	if err != nil {
		t.Fatalf("Load failed: %v", err)
	}

	if loaded.Profile.Name != "" {
		t.Errorf("Expected empty name, got %s", loaded.Profile.Name)
	}
	if loaded.AI.Providers[0].APIKey != "" {
		t.Error("Expected empty API key")
	}
}
