package config

import "testing"

func TestEmailConfigResolvedProvider(t *testing.T) {
	tests := []struct {
		name string
		cfg  EmailConfig
		want string
	}{
		{"explicit", EmailConfig{Provider: "noop", CloudflareAPIToken: "token"}, "noop"},
		{"cloudflare", EmailConfig{CloudflareAccountID: "account"}, "cloudflare"},
		{"postmark", EmailConfig{PostmarkToken: "token"}, "postmark"},
		{"smtp", EmailConfig{SMTPHost: "localhost"}, "smtp"},
		{"noop", EmailConfig{}, "noop"},
	}
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			if got := test.cfg.ResolvedProvider(); got != test.want {
				t.Fatalf("ResolvedProvider() = %q, want %q", got, test.want)
			}
		})
	}
}

func TestLoadCloudflareEmailEnvironment(t *testing.T) {
	t.Setenv("CAIRNQUIRE_EMAIL_PROVIDER", "cloudflare")
	t.Setenv("CAIRNQUIRE_EMAIL_CLOUDFLARE_ACCOUNT_ID", "account")
	t.Setenv("CAIRNQUIRE_EMAIL_CLOUDFLARE_API_TOKEN", "token")
	t.Setenv("CAIRNQUIRE_EMAIL_CLOUDFLARE_API_URL", "https://api.example.test")
	t.Setenv("CAIRNQUIRE_EMAIL_FROM", "notifications@example.com")

	cfg, err := Load()
	if err != nil {
		t.Fatalf("Load: %v", err)
	}
	if cfg.Email.ResolvedProvider() != "cloudflare" {
		t.Fatalf("provider = %q", cfg.Email.ResolvedProvider())
	}
	if cfg.Email.CloudflareAccountID != "account" || cfg.Email.CloudflareAPIToken != "token" {
		t.Fatalf("cloudflare config = %#v", cfg.Email)
	}
	if cfg.Email.CloudflareAPIURL != "https://api.example.test" || cfg.Email.From != "notifications@example.com" {
		t.Fatalf("cloudflare endpoint/from = %#v", cfg.Email)
	}
}
