import { NextResponse } from 'next/server';
import nodemailer from 'nodemailer';
import ejs from 'ejs';

// Define the expected body structure
interface EmailRequestBody {
    fullName: string;
    email: string;
    message: string;
}

const thankyouEmail = async () => {
    const locals = { baseUrl: '', };

    const emailBody = await ejs.renderFile('components/EmailTemplate/Thankyou.ejs', { locals: locals });
    return emailBody;
};

export async function POST(req: Request) {
    try {
        // const { fullName, email, message }: EmailRequestBody = await req.json();

        const { email }: EmailRequestBody = await req.json();

        const htmlContent = await thankyouEmail();

        // Create a transporter
        const transporter = nodemailer.createTransport({
            service: "Gmail",
            host: process.env.SMTP_HOST,
            port: Number(process.env.SMTP_PORT),
            secure: true, // true for 465, false for other ports
            auth: {
                user: process.env.SMTP_USER,
                pass: process.env.SMTP_PASS,
            },
        });

        console.log("process.env.SMTP_USER", process.env.SMTP_USER)
        console.log("process.env.SMTP_PASS", process.env.SMTP_PASS);
        // Create the email body
        // const emailBody = `
        //     Full Name: ${fullName}
        //     Email: ${email}

        //     Message:
        //     ${message}
        // `;
        const emailBody = `
            
        `
        console.log("emailBody", emailBody, process.env.SMTP_USER)
        // Send mail with a fixed subject
        await transporter.sendMail({
            from: process.env.SMTP_USER,
            to: email, // Send the form details to your own email
            subject: 'Metaboost - Thanks for contacting us',
            // text: emailBody, // Set the email body to include the form details,
            html: htmlContent
        });

        return NextResponse.json({ message: 'Email sent successfully!', status: 200 });
    } catch (error) {
        console.error('Error sending email:', error);
        return NextResponse.json({ message: 'Something went wrong. Please try again later.' }, { status: 500 });
    }
}
