const nodemailer = require('nodemailer')
require('dotenv').config()

const transporter = nodemailer.createTransport({
    host: process.env.MAIL_HOST,
    port: process.env.MAIL_PORT,
    secure: false, // true for 465, false for other ports
    auth: {
        user: process.env.MAIL_USERNAME, // Perbaikan di sini
        pass: process.env.MAIL_PASSWORD  // Perbaikan di sini
    },
    tls: {
        rejectUnauthorized: false // Untuk development, hati-hati di production
    }
})

const sendMail = async ({to, subject, html}) => { // Diubah ke object destructuring
    try {
        await transporter.sendMail({
            from: `"${process.env.MAIL_FROM_NAME}" <${process.env.MAIL_FROM_ADDRESS}>`, // Ditambahkan >
            to,
            subject,
            html
        })
        console.log('Email sent successfully')
    } catch (error) {
        console.error('Error sending email:', error)
        throw error
    }
}

module.exports = {
    sendMail
}