// node dependencies
const nodemailer = require('nodemailer');
require('dotenv').config({ path: '../.env' });

async function sendStratoMail({ to, subject, html }) {
    const transporter = nodemailer.createTransport({
        host: process.env.SMTP_HOST,
        port: parseInt(process.env.SMTP_PORT),
        secure: true,
        auth: {
            user: process.env.SMTP_USER,
            pass: process.env.SMTP_PASS
        }
    });

    await transporter.sendMail({
        from: `"LupiNex Media" <${process.env.SMTP_USER}>`,
        to,
        subject,
        html
    });
}


module.exports = {
    sendMail: async (userId, userName, userEmail) => {
        // 1. Generate activation token (signed JWT)
        const activationToken = jwt.sign({ user_id: userId }, JWT_SECRET, { expiresIn: '7d' });

        // 2. Build activation URL
        const activationLink = `${process.env.FRONTEND_URL}/activate?token=${activationToken}`;

        // 3. Build email content
        const subject = 'Please activate your account';
        const html = `
            <p>Hello ${userName},</p>
            <p>Thank you for your registration. Please click the link below to activate your account:</p>
            <p><a href="${activationLink}">Activate Account</a></p>
            <p>The link is valid for 7 days.</p>
        `;

        // 4. Send email via Strato SMTP
        await sendStratoMail({
            to: userEmail,
            subject,
            html
        });

        return { message: 'Activation mail sent.' };
    }
}