using System.Net;
using System.Net.Mail;
using System.Text;
using Microsoft.Extensions.Configuration;

public class EmailService
{
    private readonly IConfiguration _configuration;

    public EmailService(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    public async Task EnvoyerMailAsync(string sujet, StringBuilder contenuHtml)
    {
        var smtpConfig = _configuration.GetSection("Smtp");

        var client = new SmtpClient
        {
            Host = smtpConfig["Host"],
            Port = int.Parse(smtpConfig["Port"]),
            EnableSsl = true,
            Credentials = new NetworkCredential(
                smtpConfig["User"],
                smtpConfig["Password"]
            )
        };

        var mail = new MailMessage
        {
            From = new MailAddress(smtpConfig["From"]),
            Subject = sujet,
            Body = contenuHtml.ToString(),
            IsBodyHtml = true
        };

        mail.To.Add(smtpConfig["From"]);

        await client.SendMailAsync(mail);
    }

    public async Task EnvoyerMailContactAsync(string Contact, string sujet, StringBuilder contenuHtml)
    {
        var smtpConfig = _configuration.GetSection("Smtp");

        var client = new SmtpClient
        {
            Host = smtpConfig["Host"],
            Port = int.Parse(smtpConfig["Port"]),
            EnableSsl = true,
            Credentials = new NetworkCredential(
                smtpConfig["User"],
                smtpConfig["Password"]
            )
        };

        var mail = new MailMessage
        {
            From = new MailAddress(smtpConfig["From"]),
            Subject = sujet,
            Body = contenuHtml.ToString(),
            IsBodyHtml = true
        };

        mail.To.Add(Contact);

        await client.SendMailAsync(mail);
    }
}
