using ColaFlow.Modules.Identity.Application.Services;
using ColaFlow.Modules.Identity.Domain.Services;
using Microsoft.Extensions.Logging;

namespace ColaFlow.Modules.Identity.Infrastructure.Services;

/// <summary>
/// Mock email service for development/testing that logs emails instead of sending them
/// Captures sent emails for testing purposes
/// </summary>
public sealed class MockEmailService : IEmailService
{
    private readonly ILogger<MockEmailService> _logger;
    private readonly List<EmailMessage> _sentEmails = new();

    /// <summary>
    /// Gets the list of emails sent by this service (for testing)
    /// </summary>
    public IReadOnlyList<EmailMessage> SentEmails => _sentEmails.AsReadOnly();

    public MockEmailService(ILogger<MockEmailService> logger)
    {
        _logger = logger;
    }

    public Task<bool> SendEmailAsync(EmailMessage message, CancellationToken cancellationToken = default)
    {
        // Capture the email for testing
        _sentEmails.Add(message);

        _logger.LogInformation(
            "[MOCK EMAIL] To: {To}, Subject: {Subject}, From: {From}",
            message.To,
            message.Subject,
            message.FromEmail ?? "default");

        _logger.LogDebug(
            "[MOCK EMAIL] HTML Body: {HtmlBody}",
            message.HtmlBody);

        // Simulate successful send
        return Task.FromResult(true);
    }

    /// <summary>
    /// Clears the list of sent emails (for testing)
    /// </summary>
    public void ClearSentEmails()
    {
        _sentEmails.Clear();
    }
}
