package config

import (
	"strings"
	"testing"
)

func TestValidate_DevRequiresDevToken(t *testing.T) {
	c := &Config{AuthMode: "dev", DevToken: ""}
	err := c.validate()
	if err == nil {
		t.Fatal("dev mode without DevToken must reject; got nil error")
	}
	if !strings.Contains(err.Error(), "CDROP_DEV_TOKEN") {
		t.Errorf("error must mention CDROP_DEV_TOKEN; got %q", err.Error())
	}
}

func TestValidate_DevWithDevTokenOK(t *testing.T) {
	c := &Config{AuthMode: "dev", DevToken: "x"}
	if err := c.validate(); err != nil {
		t.Fatalf("dev with token should pass; got %v", err)
	}
}

func TestValidate_ProdOK(t *testing.T) {
	c := &Config{AuthMode: "prod", OIDCAudience: "cdrop-web,cdrop-desktop", SessionSecret: "s3cr3t"}
	if err := c.validate(); err != nil {
		t.Fatalf("prod mode with audience + session secret should pass; got %v", err)
	}
}

func TestValidate_ProdRequiresSessionSecret(t *testing.T) {
	// SessionSecret keys at-rest encryption of browser refresh_tokens; without it
	// passwordless re-login can't run safely. Refuse to boot in prod.
	c := &Config{AuthMode: "prod", OIDCAudience: "cdrop-web", SessionSecret: ""}
	err := c.validate()
	if err == nil {
		t.Fatal("prod without CDROP_SESSION_SECRET must reject; got nil error")
	}
	if !strings.Contains(err.Error(), "CDROP_SESSION_SECRET") {
		t.Errorf("error must mention CDROP_SESSION_SECRET; got %q", err.Error())
	}
}

func TestValidate_ProdRequiresAudience(t *testing.T) {
	// R6: prod with an empty audience leaves RS256 audience checking off, so a
	// token minted for any sibling app sharing the JWKS would validate. Refuse.
	c := &Config{AuthMode: "prod", OIDCAudience: ""}
	err := c.validate()
	if err == nil {
		t.Fatal("prod without CDROP_OIDC_AUDIENCE must reject; got nil error")
	}
	if !strings.Contains(err.Error(), "CDROP_OIDC_AUDIENCE") {
		t.Errorf("error must mention CDROP_OIDC_AUDIENCE; got %q", err.Error())
	}
}

func TestValidate_VAPIDHalfPairRejected(t *testing.T) {
	// A single VAPID key is always an operator mistake: every push would fail at
	// runtime while the feature reads as "on". Reject either half alone.
	pubOnly := &Config{AuthMode: "dev", DevToken: "x", VAPIDPublicKey: "pub"}
	if err := pubOnly.validate(); err == nil {
		t.Fatal("public key without private must reject")
	}
	privOnly := &Config{AuthMode: "dev", DevToken: "x", VAPIDPrivateKey: "priv"}
	if err := privOnly.validate(); err == nil {
		t.Fatal("private key without public must reject")
	}
}

func TestValidate_VAPIDBothOrNeitherOK(t *testing.T) {
	both := &Config{AuthMode: "dev", DevToken: "x", VAPIDPublicKey: "pub", VAPIDPrivateKey: "priv"}
	if err := both.validate(); err != nil {
		t.Fatalf("both VAPID keys should pass; got %v", err)
	}
	if !both.PushEnabled() {
		t.Fatal("PushEnabled should be true when both keys set")
	}
	neither := &Config{AuthMode: "dev", DevToken: "x"}
	if err := neither.validate(); err != nil {
		t.Fatalf("no VAPID keys should pass (push disabled); got %v", err)
	}
	if neither.PushEnabled() {
		t.Fatal("PushEnabled should be false when no keys set")
	}
}

func TestVAPIDSubjectOrDefault(t *testing.T) {
	explicit := &Config{VAPIDSubject: "mailto:ops@example.com"}
	if got := explicit.VAPIDSubjectOrDefault(); got != "mailto:ops@example.com" {
		t.Fatalf("explicit subject: got %q", got)
	}
	derived := &Config{OIDCRedirectURI: "https://drop.example.com/oauth/callback"}
	if got := derived.VAPIDSubjectOrDefault(); got != "https://drop.example.com" {
		t.Fatalf("derived subject: got %q, want scheme://host", got)
	}
	fallback := &Config{}
	if got := fallback.VAPIDSubjectOrDefault(); !strings.HasPrefix(got, "https://") {
		t.Fatalf("fallback subject should be an https URL; got %q", got)
	}
}

func TestValidate_UnknownMode(t *testing.T) {
	c := &Config{AuthMode: "rogue"}
	err := c.validate()
	if err == nil {
		t.Fatal("unknown auth mode must reject")
	}
}

func TestLoad_EnvOverridesDefaults(t *testing.T) {
	t.Setenv("CDROP_AUTH_MODE", "dev")
	t.Setenv("CDROP_DEV_TOKEN", "abc")
	t.Setenv("CDROP_LISTEN", ":9090")
	t.Setenv("CDROP_DB_PATH", "/tmp/x.db")

	cfg, err := Load()
	if err != nil {
		t.Fatalf("load: %v", err)
	}
	if cfg.AuthMode != "dev" {
		t.Errorf("AuthMode: got %q, want %q", cfg.AuthMode, "dev")
	}
	if cfg.DevToken != "abc" {
		t.Errorf("DevToken: got %q, want %q", cfg.DevToken, "abc")
	}
	if cfg.Listen != ":9090" {
		t.Errorf("Listen: got %q, want %q", cfg.Listen, ":9090")
	}
	if cfg.DBPath != "/tmp/x.db" {
		t.Errorf("DBPath: got %q, want %q", cfg.DBPath, "/tmp/x.db")
	}
}
