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

export async function POST(req: Request) {
    try {
        const body = await req.json()
        const { name, email, phone, service, message, recaptcha } = body

        // 1. Verify reCaptcha
        const recaptchaResponse = await fetch(
            `https://www.google.com/recaptcha/api/siteverify?secret=${process.env.RECAPTCHA_SECRET_KEY}&response=${recaptcha}`,
            { method: "POST" }
        )
        const recaptchaData = await recaptchaResponse.json()

        // For local testing without secret key, we might skip this
        if (process.env.RECAPTCHA_SECRET_KEY && !recaptchaData.success) {
            return NextResponse.json({ error: "reCaptcha verification failed" }, { status: 400 })
        }

        // 2. Configure Nodemailer
        // NOTE: In a real environment, you'd use your SMTP credentials (Gmail, SendGrid, etc.)
        const transporter = nodemailer.createTransport({
            host: process.env.SMTP_HOST || "smtp.gmail.com",
            port: Number(process.env.SMTP_PORT) || 587,
            secure: false,
            auth: {
                user: process.env.SMTP_USER,
                pass: process.env.SMTP_PASS,
            },
        })

        // 3. Create HTML Email
        const htmlEmail = `
      <!DOCTYPE html>
      <html>
      <head>
        <style>
          body { font-family: 'Inter', sans-serif; line-height: 1.6; color: #1a1a1a; margin: 0; padding: 0; }
          .container { max-width: 600px; margin: 40px auto; border: 1px solid #e2e8f0; border-radius: 24px; overflow: hidden; box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1); }
          .header { background-color: #D9A321; background: linear-gradient(135deg, #D9A321 0%, #1A1A1A 100%); color: white; padding: 40px; text-align: center; }
          .header h1 { margin: 0; font-size: 24px; text-transform: uppercase; letter-spacing: 2px; }
          .content { padding: 40px; background-color: #ffffff; }
          .field { margin-bottom: 24px; }
          .label { font-weight: 800; font-size: 12px; text-transform: uppercase; color: #D9A321; display: block; margin-bottom: 4px; }
          .value { font-size: 16px; color: #1e293b; background: #f8fafc; padding: 12px 16px; border-radius: 12px; border: 1px solid #f1f5f9; }
          .footer { background-color: #f8fafc; padding: 20px; text-align: center; font-size: 12px; color: #64748b; }
        </style>
      </head>
      <body>
        <div className="container">
          <div className="header">
            <h1>Új üzenet érkezett</h1>
          </div>
          <div className="content">
            <div className="field">
              <span className="label">Név</span>
              <div className="value">${name}</div>
            </div>
            <div className="field">
              <span className="label">Email</span>
              <div className="value">${email}</div>
            </div>
            <div className="field">
              <span className="label">Telefonszám</span>
              <div className="value">${phone || "-"}</div>
            </div>
            <div className="field">
              <span className="label">Szolgáltatás típusa</span>
              <div className="value">${service}</div>
            </div>
            <div className="field">
              <span className="label">Üzenet</span>
              <div className="value" style="white-space: pre-wrap;">${message}</div>
            </div>
          </div>
          <div className="footer">
            &copy; 2026 SkyFly Travel Landing Page System
          </div>
        </div>
      </body>
      </html>
    `

        // 4. Send Email
        await transporter.sendMail({
            from: `"SkyFly Web" <${process.env.SMTP_USER}>`,
            to: "bognar.szialrd83@gmail.com",
            subject: `Weboldal Üzenet: ${name} (${service})`,
            html: htmlEmail,
        })

        return NextResponse.json({ success: true })
    } catch (error) {
        console.error("Email error:", error)
        return NextResponse.json({ error: "Failed to send email" }, { status: 500 })
    }
}
