package service

import (
	"context"
	"testing"
)

// =============================================================================
// Email Provider Tests
// =============================================================================

func TestNewSMTPEmailProvider(t *testing.T) {
	t.Run("create SMTP provider", func(t *testing.T) {
		cfg := SMTPEmailConfig{
			Host:     "smtp.test.com",
			Port:     587,
			Username: "user",
			Password: "pass",
			FromEmail: "from@test.com",
			FromName: "Test Sender",
		}
		provider := NewSMTPEmailProvider(cfg)
		if provider == nil {
			t.Error("Expected provider to be created")
		}
	})
}

func TestSMTPEmailProvider_SendMail(t *testing.T) {
	t.Run("send mail with invalid server", func(t *testing.T) {
		cfg := SMTPEmailConfig{
			Host:     "localhost",
			Port:     25,
			FromEmail: "test@test.com",
		}
		provider := NewSMTPEmailProvider(cfg)
		ctx := context.Background()

		err := provider.SendMail(ctx, "to@test.com", "Test Subject", "<html>body</html>")
		// Expect error because no SMTP server is running
		if err == nil {
			t.Log("SendMail succeeded unexpectedly")
		} else {
			t.Logf("SendMail failed as expected: %v", err)
		}
	})

	t.Run("send mail with auth config", func(t *testing.T) {
		cfg := SMTPEmailConfig{
			Host:     "localhost",
			Port:     587,
			Username: "user",
			Password: "pass",
			FromEmail: "from@test.com",
			FromName: "Test Sender",
		}
		provider := NewSMTPEmailProvider(cfg)
		ctx := context.Background()

		err := provider.SendMail(ctx, "to@test.com", "Test Subject", "<html>body</html>")
		// Expect error because no SMTP server is running
		_ = err
	})
}

func TestMockEmailProvider_SendMail(t *testing.T) {
	t.Run("mock send mail", func(t *testing.T) {
		provider := &MockEmailProvider{}
		ctx := context.Background()

		err := provider.SendMail(ctx, "to@test.com", "Test Subject", "<html>body</html>")
		if err != nil {
			t.Errorf("MockEmailProvider should not return error: %v", err)
		}
	})
}
