import nodemailer from 'nodemailer';
import { SentMessageInfo } from 'nodemailer/lib/smtp-transport';
import { Options } from 'nodemailer/lib/mailer';
import Config from '#config';

class MailService {
	private static instance: MailService;
	transporter: nodemailer.Transporter<SentMessageInfo>;

	private constructor() {
		this.transporter = nodemailer.createTransport({
			host: Config.email.host,
			port: Config.email.port,
			secure: false, // true for 465, false for other ports
			auth: {
				user: Config.email.user,
				pass: Config.email.pass,
			},
		});
	}

	static getInstance(): MailService {
		MailService.instance ??= new MailService();
		return MailService.instance;
	}

	async sendmail(mailOptions: Options) {
		return this.transporter.sendMail(mailOptions);
	}
}

export default MailService;
