import nodemailer from 'nodemailer';
import i18n from '../utils/i18n.js';
import UserParamValue from '../models/type/user_param_value.js';

const transporter = nodemailer.createTransport({
    host: process.env.SMTP_HOST,
    port: process.env.SMTP_PORT,
    secure: process.env.SMTP_SECURE === 'true',
    auth: {
        user: process.env.SMTP_USER,
        pass: process.env.SMTP_PASSWORD
    }
});

export const sendPasswordResetEmail = async (email, resetLink, language) => {
    i18n.setLocale(language);
    const mailOptions = {
        from: process.env.SMTP_FROM,
        to: email,
        subject: i18n.__('password_reset_subject'),
        text: i18n.__('password_reset_text', { resetLink })
    };

    await transporter.sendMail(mailOptions);
};

export const sendAccountActivationEmail = async (email, activationLink, username, resetToken, languageId) => {
    // Temporär deaktiviert für Deployment
    console.log(`📧 E-Mail-Versand deaktiviert: Aktivierungs-E-Mail für ${username} (${email})`);
    console.log(`🔗 Aktivierungs-Link: ${activationLink}`);
    console.log(`🎫 Reset-Token: ${resetToken}`);
    return;
    
    const languageObject = await UserParamValue.findOne({
        where: {
            id: languageId
        }
    });
    const language = languageObject.value;
    i18n.setLocale(language);
    const mailOptions = {
        from: process.env.SMTP_FROM,
        to: email,
        subject: i18n.__('account_activation_subject'),
        text: i18n.__('account_activation_text', { activationLink, username, resetToken }),
        html: i18n.__('account_activation_html', { username, activationLink, resetToken })
    };

    await transporter.sendMail(mailOptions);
};

export const sendAnswerEmail = async (toEmail, answer, language) => {
    i18n.setLocale(language);
    const mailOptions = {
        from: process.env.SMTP_FROM,
        to: toEmail,
        subject: 'yourPart',
        text: answer,
        html: `<p>${ answer }</p>`
    };

    await transporter.sendMail(mailOptions);
};

export const sendBlogShareEmail = async (toEmail, blogUrl, blogTitle, senderName) => {
    const subject = `yourPart: ${senderName} hat einen Blog geteilt`;
    const text = `${senderName} hat den Blog "${blogTitle}" mit dir geteilt. Du kannst ihn hier ansehen: ${blogUrl}`;
    const html = `<p><strong>${senderName}</strong> hat den Blog "${blogTitle}" mit dir geteilt.</p><p>Hier ansehen: <a href="${blogUrl}">${blogUrl}</a></p>`;
    const mailOptions = {
        from: process.env.SMTP_FROM,
        to: toEmail,
        subject,
        text,
        html
    };
    await transporter.sendMail(mailOptions);
};