package account

import (
	"slices"
	"testing"
)

// TestSplitAddrs covers the comma-separated recipient parsing used for the admin alert
// To (several operator mailboxes in one message), including trimming and empty entries.
func TestSplitAddrs(t *testing.T) {
	cases := []struct {
		in   string
		want []string
	}{
		{"a@x.ru", []string{"a@x.ru"}},
		{"a@x.ru, b@y.ru", []string{"a@x.ru", "b@y.ru"}},
		{" a@x.ru ,, b@y.ru ,", []string{"a@x.ru", "b@y.ru"}},
		{"", nil},
	}
	for _, c := range cases {
		if got := splitAddrs(c.in); !slices.Equal(got, c.want) {
			t.Errorf("splitAddrs(%q) = %v, want %v", c.in, got, c.want)
		}
	}
}

// TestSMTPTLSMode covers the explicit TLS mode and the port-based fallback, including
// the non-standard Selectel ports (1127 = SSL, 1126 = STARTTLS) that the 465 heuristic
// alone cannot classify.
func TestSMTPTLSMode(t *testing.T) {
	cases := []struct {
		name string
		tls  string
		port int
		want string
	}{
		{"explicit ssl on a custom port (Selectel 1127)", "ssl", 1127, smtpTLSImplicit},
		{"explicit starttls on a custom port (Selectel 1126)", "starttls", 1126, smtpTLSSTARTTLS},
		{"tls is an alias for ssl", "TLS", 2525, smtpTLSImplicit},
		{"empty derives implicit TLS on 465", "", 465, smtpTLSImplicit},
		{"empty derives STARTTLS on 587", "", 587, smtpTLSSTARTTLS},
		{"an unknown value falls back to the port heuristic", "bogus", 465, smtpTLSImplicit},
	}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			if got := (SMTPConfig{TLS: c.tls}).tlsMode(c.port); got != c.want {
				t.Errorf("tlsMode(TLS=%q, port=%d) = %q, want %q", c.tls, c.port, got, c.want)
			}
		})
	}
}
