package account

import (
	"strings"
	"testing"
)

// TestRenderConfirmationEmail checks that each (purpose, locale) renders a localised
// subject, embeds the code and the one-tap deeplink in both bodies, and produces HTML.
func TestRenderConfirmationEmail(t *testing.T) {
	const deeplink = "https://erudit-game.ru/app/#/confirm/tok123"
	cases := []struct {
		name, purpose, locale, subjectSub string
	}{
		{"login ru", purposeLogin, "ru", "вход"},
		{"login en", purposeLogin, "en", "sign-in"},
		{"link ru", purposeLink, "ru", "подтвержд"},
		{"link en", purposeLink, "en", "confirmation"},
		{"change ru", purposeChange, "ru", "новый"},
		{"change en", purposeChange, "en", "new"},
	}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			msg, err := renderConfirmationEmail(c.purpose, "123456", deeplink, "https://erudit-game.ru", c.locale)
			if err != nil {
				t.Fatalf("render: %v", err)
			}
			if !strings.Contains(strings.ToLower(msg.Subject), c.subjectSub) {
				t.Errorf("subject %q does not contain %q", msg.Subject, c.subjectSub)
			}
			if !strings.Contains(msg.Text, "123456") || !strings.Contains(msg.HTML, "123456") {
				t.Error("code missing from a body")
			}
			if !strings.Contains(msg.Text, deeplink) || !strings.Contains(msg.HTML, "confirm/tok123") {
				t.Error("deeplink missing from a body")
			}
			if !strings.Contains(msg.HTML, "<html") {
				t.Error("HTML body is not HTML")
			}
		})
	}
}

// TestRenderConfirmationEmailUnknownLocaleDefaultsEnglish falls back to English for an
// unsupported locale rather than erroring or emitting an empty subject.
func TestRenderConfirmationEmailUnknownLocaleDefaultsEnglish(t *testing.T) {
	msg, err := renderConfirmationEmail(purposeLogin, "000000", "", "https://erudit-game.ru", "de")
	if err != nil {
		t.Fatalf("render: %v", err)
	}
	if !strings.Contains(strings.ToLower(msg.Subject), "sign-in") {
		t.Errorf("unknown locale should default to English, got subject %q", msg.Subject)
	}
}
