package handlers

import (
	"testing"
)

func TestIsAllowedEmailDomain_Default(t *testing.T) {
	t.Setenv("ALLOWED_EMAIL_DOMAINS", "")
	cases := []struct {
		email string
		want  bool
	}{
		{"alice@hoganlovells.com", true},
		{"alice@HOGANLOVELLS.COM", true},
		{"alice@hlc.com", true},
		{"alice@hlc.de", true},
		{"alice@gmail.com", false},
		{"alice@evil.hoganlovells.com.attacker.net", false},
		{"", false},
		{"no-at-sign", false},
	}
	for _, tc := range cases {
		if got := isAllowedEmailDomain(tc.email); got != tc.want {
			t.Errorf("isAllowedEmailDomain(%q) = %v; want %v", tc.email, got, tc.want)
		}
	}
}

func TestIsAllowedEmailDomain_EnvOverride(t *testing.T) {
	t.Setenv("ALLOWED_EMAIL_DOMAINS", "example.com, Other.Org ")
	if !isAllowedEmailDomain("u@example.com") {
		t.Error("example.com should be allowed by env override")
	}
	if !isAllowedEmailDomain("u@other.org") {
		t.Error("other.org should be allowed (case-insensitive, trimmed)")
	}
	if isAllowedEmailDomain("u@hoganlovells.com") {
		t.Error("env override should replace defaults — hoganlovells.com no longer allowed")
	}
}
