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

export async function POST(req: Request) {
  try {
    const { fullname, email, subject, message } = await req.json()

    const transporter = nodemailer.createTransport({
      host: process.env.EMAIL_HOST,
      port: Number(process.env.EMAIL_PORT),
      auth: {
        user: process.env.EMAIL_USER,
        pass: process.env.EMAIL_PASS,
      },
    })

    const mailOptions = {
      from: email,
      to: process.env.EMAIL_SEVENPRO,
      subject: `Nova dúvida de ${fullname}: ${subject}`,
      text: message,
      html: `<p><strong>Nome:</strong> ${fullname}</p><p><strong>Assunto:</strong> ${subject}</p><p><strong>Mensagem:</strong></p><p>${message}</p>`,
    }

    await transporter.sendMail(mailOptions)

    return NextResponse.json(
      { message: 'E-mail enviado com sucesso!' },
      { status: 200 },
    )
  } catch (error) {
    return NextResponse.json(
      { error: 'Erro ao enviar o e-mail.' },
      { status: 500 },
    )
  }
}
