package smtp

import (
	"testing"
)

func TestAddress(t *testing.T) {
	s := &SMTP{
		Host: "smtp.example.com",
		Port: "587",
	}
	expected := "smtp.example.com:587"
	if got := s.Address(); got != expected {
		t.Errorf("Address() = %q, want %q", got, expected)
	}
}

func TestAuth(t *testing.T) {
	s := &SMTP{
		User:     "user",
		Password: "password",
		Host:     "smtp.example.com",
	}

	// Verify return of non-nil Auth mechanism.
	if got := s.Auth(); got == nil {
		t.Error("Auth() returned nil")
	}
}

func TestValidate(t *testing.T) {
	tests := []struct {
		name        string
		s           *SMTP
		shouldError bool
	}{
		{
			name: "valid configuration",
			s: &SMTP{
				From:     "sender@example.com",
				Host:     "smtp.example.com",
				Password: "secretpassword",
				Port:     "587",
				User:     "user",
			},
			shouldError: false,
		},
		{
			name: "missing from",
			s: &SMTP{
				Host:     "smtp.example.com",
				Password: "p",
				Port:     "587",
				User:     "u",
			},
			shouldError: true,
		},
		{
			name: "missing host",
			s: &SMTP{
				From:     "f",
				Password: "p",
				Port:     "587",
				User:     "u",
			},
			shouldError: true,
		},
		{
			name: "missing password",
			s: &SMTP{
				From: "f",
				Host: "h",
				Port: "587",
				User: "u",
			},
			shouldError: true,
		},
		{
			name: "missing port",
			s: &SMTP{
				From:     "f",
				Host:     "h",
				Password: "p",
				User:     "u",
			},
			shouldError: true,
		},
		{
			name: "missing user",
			s: &SMTP{
				From:     "f",
				Host:     "h",
				Password: "p",
				Port:     "587",
			},
			shouldError: true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			err := tt.s.Validate()
			if tt.shouldError && err == nil {
				t.Fatalf("expected error, got nil")
			}
			if !tt.shouldError && err != nil {
				t.Fatalf("unexpected error: %v", err)
			}
		})
	}
}
