package config

import "testing"

func TestLoadA2AEdgeURLPrefersEdgeURL(t *testing.T) {
	t.Setenv("A2A_EDGE_URL", "http://edge.example/a2a")
	t.Setenv("A2A_AGENT_URL", "http://legacy.example/a2a")

	cfg := Load()

	if cfg.A2AEdgeURL != "http://edge.example/a2a" {
		t.Fatalf("A2AEdgeURL: got %q", cfg.A2AEdgeURL)
	}
	if cfg.A2AAgentURL != "http://legacy.example/a2a" {
		t.Fatalf("A2AAgentURL: got %q", cfg.A2AAgentURL)
	}
}

func TestLoadA2AEdgeURLFallsBackToLegacyAgentURL(t *testing.T) {
	t.Setenv("A2A_AGENT_URL", "http://legacy.example/a2a")

	cfg := Load()

	if cfg.A2AEdgeURL != "http://legacy.example/a2a" {
		t.Fatalf("A2AEdgeURL fallback: got %q", cfg.A2AEdgeURL)
	}
	if cfg.A2AAgentURL != "http://legacy.example/a2a" {
		t.Fatalf("A2AAgentURL: got %q", cfg.A2AAgentURL)
	}
}

func TestLoadModelAndA2ADefaults(t *testing.T) {
	cfg := Load()

	if cfg.ModelBaseURL != "http://192.168.0.91:11434" {
		t.Fatalf("ModelBaseURL: got %q", cfg.ModelBaseURL)
	}
	if cfg.ModelAPIKey != "ollama" {
		t.Fatalf("ModelAPIKey: got %q", cfg.ModelAPIKey)
	}
	if cfg.ModelName != "qwen3.6:35b-a3b-bf16" {
		t.Fatalf("ModelName: got %q", cfg.ModelName)
	}
	if cfg.ModelContextSize != 262144 {
		t.Fatalf("ModelContextSize: got %d", cfg.ModelContextSize)
	}
	if cfg.ModelTimeoutSec != 300 {
		t.Fatalf("ModelTimeoutSec: got %d", cfg.ModelTimeoutSec)
	}
	if cfg.A2ATimeoutSec != 300 {
		t.Fatalf("A2ATimeoutSec: got %d", cfg.A2ATimeoutSec)
	}
}

func TestConfigLoadsWorkflowTaskTimeout(t *testing.T) {
	cfg := Load()
	if cfg.WorkflowTaskTimeoutSec != 300 {
		t.Fatalf("WorkflowTaskTimeoutSec default: got %d, want 300", cfg.WorkflowTaskTimeoutSec)
	}

	t.Setenv("WORKFLOW_TASK_TIMEOUT_SEC", "120")
	cfgOverride := Load()
	if cfgOverride.WorkflowTaskTimeoutSec != 120 {
		t.Fatalf("WorkflowTaskTimeoutSec override: got %d, want 120", cfgOverride.WorkflowTaskTimeoutSec)
	}
}

func TestConfigLoadsProtoSocketDefaultsAndOverrides(t *testing.T) {
	cfg := Load()
	if cfg.ProtoSocketPath != "/proto-socket" {
		t.Fatalf("ProtoSocketPath default: got %q, want %q", cfg.ProtoSocketPath, "/proto-socket")
	}
	if cfg.ProtoSocketHeartbeatIntervalSec != 30 {
		t.Fatalf("ProtoSocketHeartbeatIntervalSec default: got %d, want 30", cfg.ProtoSocketHeartbeatIntervalSec)
	}
	if cfg.ProtoSocketHeartbeatWaitSec != 10 {
		t.Fatalf("ProtoSocketHeartbeatWaitSec default: got %d, want 10", cfg.ProtoSocketHeartbeatWaitSec)
	}

	t.Setenv("PROTO_SOCKET_PATH", "/custom-path")
	t.Setenv("PROTO_SOCKET_HEARTBEAT_INTERVAL_SEC", "15")
	t.Setenv("PROTO_SOCKET_HEARTBEAT_WAIT_SEC", "5")

	cfgOverride := Load()
	if cfgOverride.ProtoSocketPath != "/custom-path" {
		t.Fatalf("ProtoSocketPath override: got %q, want %q", cfgOverride.ProtoSocketPath, "/custom-path")
	}
	if cfgOverride.ProtoSocketHeartbeatIntervalSec != 15 {
		t.Fatalf("ProtoSocketHeartbeatIntervalSec override: got %d, want 15", cfgOverride.ProtoSocketHeartbeatIntervalSec)
	}
	if cfgOverride.ProtoSocketHeartbeatWaitSec != 5 {
		t.Fatalf("ProtoSocketHeartbeatWaitSec override: got %d, want 5", cfgOverride.ProtoSocketHeartbeatWaitSec)
	}
}
