package config_test

import (
	"cursor-api-proxy/internal/config"
	"cursor-api-proxy/internal/env"
	"path/filepath"
	"strings"
	"testing"
)

func TestLoadBridgeConfig_Defaults(t *testing.T) {
	cfg := config.LoadBridgeConfig(env.EnvSource{}, "/workspace")

	if cfg.AgentBin != "agent" {
		t.Errorf("AgentBin = %q, want %q", cfg.AgentBin, "agent")
	}
	if cfg.Host != "127.0.0.1" {
		t.Errorf("Host = %q, want %q", cfg.Host, "127.0.0.1")
	}
	if cfg.Port != 8765 {
		t.Errorf("Port = %d, want 8765", cfg.Port)
	}
	if cfg.RequiredKey != "" {
		t.Errorf("RequiredKey = %q, want empty", cfg.RequiredKey)
	}
	if cfg.DefaultModel != "auto" {
		t.Errorf("DefaultModel = %q, want %q", cfg.DefaultModel, "auto")
	}
	if cfg.Force {
		t.Error("Force should be false")
	}
	if cfg.ApproveMcps {
		t.Error("ApproveMcps should be false")
	}
	if !cfg.StrictModel {
		t.Error("StrictModel should be true")
	}
	if cfg.Mode != "ask" {
		t.Errorf("Mode = %q, want %q", cfg.Mode, "ask")
	}
	if cfg.Workspace != "/workspace" {
		t.Errorf("Workspace = %q, want /workspace", cfg.Workspace)
	}
	if !cfg.ChatOnlyWorkspace {
		t.Error("ChatOnlyWorkspace should be true")
	}
	if cfg.WinCmdlineMax != 30000 {
		t.Errorf("WinCmdlineMax = %d, want 30000", cfg.WinCmdlineMax)
	}
}

func TestLoadBridgeConfig_FromEnv(t *testing.T) {
	e := env.EnvSource{
		"CURSOR_AGENT_BIN":                  "/usr/bin/agent",
		"CURSOR_BRIDGE_HOST":                "0.0.0.0",
		"CURSOR_BRIDGE_PORT":                "9999",
		"CURSOR_BRIDGE_API_KEY":             "sk-secret",
		"CURSOR_BRIDGE_DEFAULT_MODEL":       "org/claude-3-opus",
		"CURSOR_BRIDGE_FORCE":               "true",
		"CURSOR_BRIDGE_APPROVE_MCPS":        "yes",
		"CURSOR_BRIDGE_STRICT_MODEL":        "false",
		"CURSOR_BRIDGE_WORKSPACE":           "./my-workspace",
		"CURSOR_BRIDGE_TIMEOUT_MS":          "60000",
		"CURSOR_BRIDGE_CHAT_ONLY_WORKSPACE": "false",
		"CURSOR_BRIDGE_VERBOSE":             "1",
		"CURSOR_BRIDGE_TLS_CERT":            "./certs/test.crt",
		"CURSOR_BRIDGE_TLS_KEY":             "./certs/test.key",
	}
	cfg := config.LoadBridgeConfig(e, "/tmp/project")

	if cfg.AgentBin != "/usr/bin/agent" {
		t.Errorf("AgentBin = %q, want /usr/bin/agent", cfg.AgentBin)
	}
	if cfg.Host != "0.0.0.0" {
		t.Errorf("Host = %q, want 0.0.0.0", cfg.Host)
	}
	if cfg.Port != 9999 {
		t.Errorf("Port = %d, want 9999", cfg.Port)
	}
	if cfg.RequiredKey != "sk-secret" {
		t.Errorf("RequiredKey = %q, want sk-secret", cfg.RequiredKey)
	}
	if cfg.DefaultModel != "claude-3-opus" {
		t.Errorf("DefaultModel = %q, want claude-3-opus", cfg.DefaultModel)
	}
	if !cfg.Force {
		t.Error("Force should be true")
	}
	if !cfg.ApproveMcps {
		t.Error("ApproveMcps should be true")
	}
	if cfg.StrictModel {
		t.Error("StrictModel should be false")
	}
	if !filepath.IsAbs(cfg.Workspace) {
		t.Errorf("Workspace should be absolute, got %q", cfg.Workspace)
	}
	if !strings.Contains(cfg.Workspace, "my-workspace") {
		t.Errorf("Workspace %q should contain 'my-workspace'", cfg.Workspace)
	}
	if cfg.TimeoutMs != 60000 {
		t.Errorf("TimeoutMs = %d, want 60000", cfg.TimeoutMs)
	}
	if cfg.ChatOnlyWorkspace {
		t.Error("ChatOnlyWorkspace should be false")
	}
	if !cfg.Verbose {
		t.Error("Verbose should be true")
	}
	if cfg.TLSCertPath != "/tmp/project/certs/test.crt" {
		t.Errorf("TLSCertPath = %q, want /tmp/project/certs/test.crt", cfg.TLSCertPath)
	}
	if cfg.TLSKeyPath != "/tmp/project/certs/test.key" {
		t.Errorf("TLSKeyPath = %q, want /tmp/project/certs/test.key", cfg.TLSKeyPath)
	}
}

func TestLoadBridgeConfig_WideHost(t *testing.T) {
	cfg := config.LoadBridgeConfig(env.EnvSource{"CURSOR_BRIDGE_HOST": "0.0.0.0"}, "/workspace")
	if cfg.Host != "0.0.0.0" {
		t.Errorf("Host = %q, want 0.0.0.0", cfg.Host)
	}
}
