package config

import (
	"testing"
	"time"
)

func envFrom(m map[string]string) func(string) (string, bool) {
	return func(k string) (string, bool) {
		v, ok := m[k]
		return v, ok
	}
}

func TestDefaults(t *testing.T) {
	cfg, err := load(envFrom(map[string]string{
		"THERMOGRAPH_API_BASE_INTERNAL": "http://backend:8137",
	}))
	if err != nil {
		t.Fatalf("load: %v", err)
	}
	if cfg.APIBaseInternal != "http://backend:8137" {
		t.Errorf("APIBaseInternal = %q", cfg.APIBaseInternal)
	}
	if cfg.APIVersion != "v2" {
		t.Errorf("APIVersion = %q, want v2", cfg.APIVersion)
	}
	if cfg.Base != "/thermograph" {
		t.Errorf("Base = %q, want /thermograph", cfg.Base)
	}
	if cfg.AssetBase != "/thermograph" {
		t.Errorf("AssetBase = %q, want /thermograph (falls back to Base)", cfg.AssetBase)
	}
	if cfg.SSRCacheTTL != 600*time.Second {
		t.Errorf("SSRCacheTTL = %v, want 10m", cfg.SSRCacheTTL)
	}
	if cfg.GoogleVerify != "" || cfg.BingVerify != "" {
		t.Errorf("verify tokens should default empty, got %q / %q", cfg.GoogleVerify, cfg.BingVerify)
	}
	if cfg.Port != "8080" {
		t.Errorf("Port = %q, want 8080", cfg.Port)
	}
	if cfg.StaticDir != "static" || cfg.ContentDir != "content" {
		t.Errorf("StaticDir/ContentDir = %q/%q", cfg.StaticDir, cfg.ContentDir)
	}
}

func TestAPIBaseInternalRequired(t *testing.T) {
	if _, err := load(envFrom(nil)); err == nil {
		t.Fatal("expected an error when THERMOGRAPH_API_BASE_INTERNAL is unset (fail-loud boot)")
	}
	if _, err := load(envFrom(map[string]string{"THERMOGRAPH_API_BASE_INTERNAL": ""})); err == nil {
		t.Fatal("expected an error when THERMOGRAPH_API_BASE_INTERNAL is empty")
	}
}

func TestBaseNormalization(t *testing.T) {
	cases := []struct{ raw, want string }{
		{"/", ""}, // deployed clean-root topology (Dockerfile sets "/")
		{"", ""},  // set-but-empty strips to nothing too
		{"/thermograph", "/thermograph"},
		{"thermograph/", "/thermograph"},
		{"/a/b/", "/a/b"},
	}
	for _, c := range cases {
		cfg, err := load(envFrom(map[string]string{
			"THERMOGRAPH_API_BASE_INTERNAL": "http://b",
			"THERMOGRAPH_BASE":              c.raw,
		}))
		if err != nil {
			t.Fatalf("load(%q): %v", c.raw, err)
		}
		if cfg.Base != c.want {
			t.Errorf("Base(%q) = %q, want %q", c.raw, cfg.Base, c.want)
		}
	}
}

func TestAssetBase(t *testing.T) {
	cfg, err := load(envFrom(map[string]string{
		"THERMOGRAPH_API_BASE_INTERNAL": "http://b",
		"THERMOGRAPH_API_BASE_PUBLIC":   "https://api.example.org/",
	}))
	if err != nil {
		t.Fatalf("load: %v", err)
	}
	if cfg.AssetBase != "https://api.example.org" {
		t.Errorf("AssetBase = %q, want trailing slash stripped", cfg.AssetBase)
	}
}

func TestSSRCacheTTL(t *testing.T) {
	// Empty string means the default (Python: float(env or 600)).
	cfg, err := load(envFrom(map[string]string{
		"THERMOGRAPH_API_BASE_INTERNAL": "http://b",
		"THERMOGRAPH_SSR_CACHE_TTL":     "",
	}))
	if err != nil {
		t.Fatalf("load: %v", err)
	}
	if cfg.SSRCacheTTL != 600*time.Second {
		t.Errorf("empty TTL = %v, want 10m", cfg.SSRCacheTTL)
	}

	cfg, err = load(envFrom(map[string]string{
		"THERMOGRAPH_API_BASE_INTERNAL": "http://b",
		"THERMOGRAPH_SSR_CACHE_TTL":     "2.5",
	}))
	if err != nil {
		t.Fatalf("load: %v", err)
	}
	if cfg.SSRCacheTTL != 2500*time.Millisecond {
		t.Errorf("TTL 2.5 = %v, want 2.5s", cfg.SSRCacheTTL)
	}

	// Garbage crashed the Python boot (ValueError at import) — error here too.
	if _, err = load(envFrom(map[string]string{
		"THERMOGRAPH_API_BASE_INTERNAL": "http://b",
		"THERMOGRAPH_SSR_CACHE_TTL":     "ten minutes",
	})); err == nil {
		t.Fatal("expected an error for an unparsable TTL")
	}
}

func TestVerifyTokensTrimmed(t *testing.T) {
	cfg, err := load(envFrom(map[string]string{
		"THERMOGRAPH_API_BASE_INTERNAL": "http://b",
		"THERMOGRAPH_GOOGLE_VERIFY":     "  g-token  ",
		"THERMOGRAPH_BING_VERIFY":       "\tb-token\n",
	}))
	if err != nil {
		t.Fatalf("load: %v", err)
	}
	if cfg.GoogleVerify != "g-token" || cfg.BingVerify != "b-token" {
		t.Errorf("tokens not trimmed: %q / %q", cfg.GoogleVerify, cfg.BingVerify)
	}
}
