using Application.Abstractions.Messaging.Email;
using Infrastructure.Persistence.Identity;
using Microsoft.AspNetCore.Identity.UI.Services;

namespace Infrastructure.Messaging.Email
{
    public class IdentityEmailSender : IEmailSender
    {
        private readonly IMailService _mailService;

        public IdentityEmailSender(IMailService mailService)
        {
            _mailService = mailService;
        }

        public async Task SendEmailAsync(string email, string subject, string htmlMessage)
        {
            ArgumentException.ThrowIfNullOrWhiteSpace(email);
            ArgumentException.ThrowIfNullOrWhiteSpace(subject);
            ArgumentNullException.ThrowIfNull(htmlMessage);

            await _mailService.SendAsync(
                new SendData(
                    ToAddress: email,
                    Subject: subject,
                    MessageHtml: htmlMessage
                )
            );
        }

        public Task SendConfirmationLinkAsync(ApplicationUser user, string email, string confirmationLink)
        {
            ArgumentNullException.ThrowIfNull(user);
            ArgumentException.ThrowIfNullOrWhiteSpace(email);
            ArgumentException.ThrowIfNullOrWhiteSpace(confirmationLink);

            return _mailService.SendAsync(new SendData(
                ToAddress: email,
                Subject: "이메일 확인",
                MessageHtml: $"""
                             <p>아래 링크를 눌러 이메일 확인을 완료하세요.</p>
                             <p><a href="{confirmationLink}">이메일 확인</a></p>
                             """
            ));
        }

        public Task SendPasswordResetLinkAsync(ApplicationUser user, string email, string resetLink)
        {
            ArgumentNullException.ThrowIfNull(user);
            ArgumentException.ThrowIfNullOrWhiteSpace(email);
            ArgumentException.ThrowIfNullOrWhiteSpace(resetLink);

            return _mailService.SendAsync(new SendData(
                ToAddress: email,
                Subject: "비밀번호 재설정",
                MessageHtml: $"""
                             <p>아래 링크를 눌러 비밀번호를 재설정하세요.</p>
                             <p><a href="{resetLink}">비밀번호 재설정</a></p>
                             """
            ));
        }

        public Task SendPasswordResetCodeAsync(ApplicationUser user, string email, string resetCode)
        {
            ArgumentNullException.ThrowIfNull(user);
            ArgumentException.ThrowIfNullOrWhiteSpace(email);
            ArgumentException.ThrowIfNullOrWhiteSpace(resetCode);

            return _mailService.SendAsync(new SendData(
                ToAddress: email,
                Subject: "비밀번호 재설정 코드",
                MessageHtml: $"""
                             <p>비밀번호 재설정 코드:</p>
                             <pre>{resetCode}</pre>
                             """
            ));
        }
    }
}