

using Indotalent.Data.Entities;
using Indotalent.Infrastructure.Email.Mailgun;
using Indotalent.Infrastructure.Email.Mailjet;
using Indotalent.Infrastructure.Email.SendGrid;
using Indotalent.Infrastructure.Email.Smtp;
using Microsoft.AspNetCore.Identity;

namespace Indotalent.Infrastructure.Email;

public static class DI
{
    public static IServiceCollection AddEmailService(this IServiceCollection services, IConfiguration configuration)
    {
        services.AddScoped<EmailService>();


        var emailSettings = configuration.GetSection("EmailSettings").Get<EmailSettingsModel>() ?? new EmailSettingsModel();

        if (emailSettings.SendGrid?.IsUsed == true)
        {
            services.AddHttpClient<IEmailSender, SendGridEmailSender>();
        }
        else if (emailSettings.Mailgun?.IsUsed == true)
        {
            services.AddHttpClient<IEmailSender, MailgunEmailSender>();
        }
        else if (emailSettings.Mailjet?.IsUsed == true)
        {
            services.AddHttpClient<IEmailSender, MailjetEmailSender>();
        }
        else if (emailSettings.Smtp?.IsUsed == true)
        {
            services.AddTransient<IEmailSender, SmtpEmailSender>();
        }
        else
        {
            services.AddScoped<IEmailSender, DefaultEmailSender>();
        }

        services.AddTransient<IEmailSender<ApplicationUser>, IdentityEmailManager<ApplicationUser>>();

        return services;
    }
}
