import { INotificationsAdapter, NotificationDataEmail } from "./Notifications.Interface";
import nodemailer, { SentMessageInfo } from "nodemailer";

export class NotificationsNodemailerAdapter implements INotificationsAdapter<NotificationDataEmail> {
    async send(data: NotificationDataEmail): Promise<void> {
        const mailTransporter = nodemailer.createTransport({
            service: "gmail",
            secure: true,
            port: 465,
            auth: {
                user: process.env.GMAIL_EMAIL,
                pass: process.env.GMAIL_PASSWORD,
            },
        });

        const mailOptions = {
            from: process.env.GMAIL_EMAIL,
            to: data.email,
            subject: data.subject,
            text: data.message,
        };

        await mailTransporter.sendMail(
            mailOptions,
            (error: Error | null, info: SentMessageInfo) => {
                if (error) {
                    return console.log(error);
                }
                console.log("Correo enviado: " + info.response);
            }
        );
    }
}
