package config_test

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

	"iop/packages/go/config"
)

func TestEdgeRefreshDefaults(t *testing.T) {
	dir := t.TempDir()
	f := filepath.Join(dir, "edge.yaml")
	if err := os.WriteFile(f, []byte("server:\n  listen: \"0.0.0.0:9090\"\n"), 0o600); err != nil {
		t.Fatalf("write yaml: %v", err)
	}
	cfg, err := config.LoadEdge(f)
	if err != nil {
		t.Fatalf("load: %v", err)
	}
	if cfg.Refresh.Enabled {
		t.Fatalf("expected refresh.enabled=false by default, got true")
	}
	if cfg.Refresh.Listen != "127.0.0.1:19093" {
		t.Fatalf("expected refresh.listen=%q by default, got %q", "127.0.0.1:19093", cfg.Refresh.Listen)
	}
}

func TestEdgeRefreshUnmarshal(t *testing.T) {
	dir := t.TempDir()
	f := filepath.Join(dir, "edge.yaml")
	yaml := "server:\n  listen: \"0.0.0.0:9090\"\nrefresh:\n  enabled: true\n  listen: \"127.0.0.1:0\"\n"
	if err := os.WriteFile(f, []byte(yaml), 0o600); err != nil {
		t.Fatalf("write yaml: %v", err)
	}
	cfg, err := config.LoadEdge(f)
	if err != nil {
		t.Fatalf("load: %v", err)
	}
	if !cfg.Refresh.Enabled {
		t.Fatalf("expected refresh.enabled=true")
	}
	if cfg.Refresh.Listen != "127.0.0.1:0" {
		t.Fatalf("expected refresh.listen=%q, got %q", "127.0.0.1:0", cfg.Refresh.Listen)
	}
}

func TestLoadEdge_AdvertiseHostAndArtifactDefaultsEmpty(t *testing.T) {
	dir := t.TempDir()
	f := filepath.Join(dir, "edge.yaml")
	if err := os.WriteFile(f, []byte("server:\n  listen: \"0.0.0.0:9090\"\n"), 0o600); err != nil {
		t.Fatalf("write yaml: %v", err)
	}
	cfg, err := config.LoadEdge(f)
	if err != nil {
		t.Fatalf("load: %v", err)
	}
	if cfg.Server.AdvertiseHost != "" {
		t.Errorf("expected empty server.advertise_host by default, got %q", cfg.Server.AdvertiseHost)
	}
	if cfg.Bootstrap.ArtifactBaseURL != "" {
		t.Errorf("expected empty bootstrap.artifact_base_url by default, got %q", cfg.Bootstrap.ArtifactBaseURL)
	}
	if cfg.Logging.Path != "" {
		t.Errorf("expected empty logging.path by default, got %q", cfg.Logging.Path)
	}
}

func TestLoadEdge_AdvertiseHostAndArtifactExplicit(t *testing.T) {
	dir := t.TempDir()
	f := filepath.Join(dir, "edge.yaml")
	yaml := `server:
  listen: "0.0.0.0:9090"
  advertise_host: "edge.example.test"
bootstrap:
  artifact_base_url: "http://edge.example.test:18080"
logging:
  path: "/var/log/iop/edge.log"
`
	if err := os.WriteFile(f, []byte(yaml), 0o600); err != nil {
		t.Fatalf("write yaml: %v", err)
	}
	cfg, err := config.LoadEdge(f)
	if err != nil {
		t.Fatalf("load: %v", err)
	}
	if cfg.Server.AdvertiseHost != "edge.example.test" {
		t.Errorf("server.advertise_host=%q", cfg.Server.AdvertiseHost)
	}
	if cfg.Bootstrap.ArtifactBaseURL != "http://edge.example.test:18080" {
		t.Errorf("bootstrap.artifact_base_url=%q", cfg.Bootstrap.ArtifactBaseURL)
	}
	if cfg.Logging.Path != "/var/log/iop/edge.log" {
		t.Errorf("logging.path=%q", cfg.Logging.Path)
	}
}

func TestLoadEdge_A2ADefaults(t *testing.T) {
	dir := t.TempDir()
	f := filepath.Join(dir, "edge.yaml")
	if err := os.WriteFile(f, []byte("server:\n  listen: \"0.0.0.0:9090\"\n"), 0o600); err != nil {
		t.Fatalf("write yaml: %v", err)
	}
	cfg, err := config.LoadEdge(f)
	if err != nil {
		t.Fatalf("load: %v", err)
	}
	if cfg.A2A.Enabled {
		t.Fatal("expected a2a.enabled=false by default")
	}
	if cfg.A2A.Listen != "0.0.0.0:8081" {
		t.Fatalf("expected a2a.listen default, got %q", cfg.A2A.Listen)
	}
	if cfg.A2A.Path != "/a2a" {
		t.Fatalf("expected a2a.path=%q, got %q", "/a2a", cfg.A2A.Path)
	}
	if cfg.A2A.Adapter != "openai_compat" {
		t.Fatalf("expected a2a.adapter=%q, got %q", "openai_compat", cfg.A2A.Adapter)
	}
	if cfg.A2A.SessionID != "a2a" {
		t.Fatalf("expected a2a.session_id=%q, got %q", "a2a", cfg.A2A.SessionID)
	}
	if cfg.A2A.TimeoutSec != 120 {
		t.Fatalf("expected a2a.timeout_sec=120, got %d", cfg.A2A.TimeoutSec)
	}
	if cfg.A2A.BearerToken != "" {
		t.Fatalf("expected a2a.bearer_token empty by default, got %q", cfg.A2A.BearerToken)
	}
}

func TestLoadEdge_A2AOverride(t *testing.T) {
	dir := t.TempDir()
	f := filepath.Join(dir, "edge.yaml")
	yaml := `
server:
  listen: "0.0.0.0:9090"
a2a:
  enabled: true
  listen: "127.0.0.1:8082"
  path: "/agent"
  node: "node0"
  adapter: "cli"
  target: "claude"
  session_id: "nomad"
  timeout_sec: 60
  bearer_token: "secret-token"
`
	if err := os.WriteFile(f, []byte(yaml), 0o600); err != nil {
		t.Fatalf("write yaml: %v", err)
	}
	cfg, err := config.LoadEdge(f)
	if err != nil {
		t.Fatalf("load: %v", err)
	}
	if !cfg.A2A.Enabled {
		t.Fatal("expected a2a.enabled=true")
	}
	if cfg.A2A.Listen != "127.0.0.1:8082" {
		t.Fatalf("expected a2a.listen=%q, got %q", "127.0.0.1:8082", cfg.A2A.Listen)
	}
	if cfg.A2A.Path != "/agent" {
		t.Fatalf("expected a2a.path=%q, got %q", "/agent", cfg.A2A.Path)
	}
	if cfg.A2A.NodeRef != "node0" {
		t.Fatalf("expected a2a.node=%q, got %q", "node0", cfg.A2A.NodeRef)
	}
	if cfg.A2A.Target != "claude" {
		t.Fatalf("expected a2a.target=%q, got %q", "claude", cfg.A2A.Target)
	}
	if cfg.A2A.SessionID != "nomad" {
		t.Fatalf("expected a2a.session_id=%q, got %q", "nomad", cfg.A2A.SessionID)
	}
	if cfg.A2A.TimeoutSec != 60 {
		t.Fatalf("expected a2a.timeout_sec=60, got %d", cfg.A2A.TimeoutSec)
	}
	if cfg.A2A.BearerToken != "secret-token" {
		t.Fatalf("expected a2a.bearer_token=%q, got %q", "secret-token", cfg.A2A.BearerToken)
	}
}

func TestLoadEdge_ControlPlaneDefaults(t *testing.T) {
	dir := t.TempDir()
	f := filepath.Join(dir, "edge.yaml")
	if err := os.WriteFile(f, []byte("server:\n  listen: \"0.0.0.0:9090\"\n"), 0o600); err != nil {
		t.Fatalf("write yaml: %v", err)
	}
	cfg, err := config.LoadEdge(f)
	if err != nil {
		t.Fatalf("load: %v", err)
	}
	if cfg.ControlPlane.Enabled {
		t.Fatal("expected control_plane.enabled=false by default")
	}
	if cfg.ControlPlane.WireAddr != "" {
		t.Fatalf("expected control_plane.wire_addr empty by default, got %q", cfg.ControlPlane.WireAddr)
	}
	if cfg.ControlPlane.ReconnectIntervalSec != 5 {
		t.Fatalf("expected control_plane.reconnect_interval_sec=5, got %d", cfg.ControlPlane.ReconnectIntervalSec)
	}
}

func TestLoadEdge_ControlPlaneOverride(t *testing.T) {
	dir := t.TempDir()
	f := filepath.Join(dir, "edge.yaml")
	yaml := `
server:
  listen: "0.0.0.0:9090"
control_plane:
  enabled: true
  wire_addr: "cp.example.test:7070"
  reconnect_interval_sec: 10
`
	if err := os.WriteFile(f, []byte(yaml), 0o600); err != nil {
		t.Fatalf("write yaml: %v", err)
	}
	cfg, err := config.LoadEdge(f)
	if err != nil {
		t.Fatalf("load: %v", err)
	}
	if !cfg.ControlPlane.Enabled {
		t.Fatal("expected control_plane.enabled=true")
	}
	if cfg.ControlPlane.WireAddr != "cp.example.test:7070" {
		t.Fatalf("expected control_plane.wire_addr=%q, got %q", "cp.example.test:7070", cfg.ControlPlane.WireAddr)
	}
	if cfg.ControlPlane.ReconnectIntervalSec != 10 {
		t.Fatalf("expected control_plane.reconnect_interval_sec=10, got %d", cfg.ControlPlane.ReconnectIntervalSec)
	}
}
