import { config } from "@/config";
import nodemailer from "nodemailer";
import { ApplicationService, IEmailService, ISendEmailOptions } from "../../application";
import { LoggerMailService } from "./LoggerMailService";

export class BrevoMailService extends ApplicationService implements IEmailService {
  async sendMail(mailOptions: ISendEmailOptions, dry?: boolean): Promise<any> {
    if (dry) {
      // No enviar el email
      new LoggerMailService().sendMail(mailOptions);
      return;
    }

    const transporter = nodemailer.createTransport(config.nodemailer.brevo);

    return await transporter.sendMail({
      ...mailOptions,
      attachments: mailOptions.attachments?.map((att) => ({
        filename: att.filename,
        path: att.path,
      })),
    });
  }
}
