import nodemailer from "nodemailer";

export async function POST(request: Request) {
  try {
    const data = await request.json();

    const { name, email, phone, message } = data;

    const transporter = nodemailer.createTransport({
      host: "mail.clipmatic.co.uk",
      port: 465,
      secure: true,
      auth: {
        user: "info@sunventus.com",
        pass: "Sunventus20@%",
      },
    });

    await transporter.sendMail({
      from: `"${name}" <info@sunventus.com>`,
      to: "info@sunventus.com",
      subject: `[New Contact] Sunventus - Website Inquiry`,
      text: `
        Name: ${name}
        Email: ${email}
        Phone: ${phone}
        Message: ${message}
      `,
      html: `
        <h2>📩 New contact from <a href="https://sunventus.com">Sunventus</a> website</h2>

        <p>You have received a new inquiry from a visitor interested in Sunventus.</p>

        <hr>

        <p><b>Name:</b> ${name}</p>
        <p><b>Email:</b> <a href="mailto:${email}">${email}</a></p>
        <p><b>Phone:</b> ${phone || "Not provided"}</p>

        <hr>

        <p><b>Message:</b></p>
        <p>${message}</p>

        <hr>

        <p style="font-size: 12px; color: gray;">
        This email was automatically sent from the Sunventus website contact form.
        Please reply using the email address or phone number provided above.
        </p>

      `,
    });

  await transporter.sendMail({
      from: `"Sunventus" <info@sunventus.com>`,
      to: email,
      subject: `We have received your inquiry - Sunventus`,
      text: `
        Hello ${name},

        Thank you for contacting Sunventus.
        We have successfully received your message and our team will review your inquiry shortly.

        Here's a copy of what you sent us:
        ----------------------------------------------------
        Name: ${name}
        Email: ${email}
        Phone: ${phone || "Not provided"}
        Message:
        ${message}
        ----------------------------------------------------

        We will get back to you as soon as possible.

        Best regards,
        The Sunventus Team
      `,
      html: `
        <h2>Thank you for contacting <a href="https://sunventus.com">Sunventus</a>!</h2>
        <p>Hi ${name},</p>
        <p>We have successfully received your message and our team will review your inquiry shortly.</p>
        <h3>Here's a copy of your message:</h3>
        <hr>
        <p><b>Name:</b> ${name}</p>
        <p><b>Email:</b> ${email}</p>
        <p><b>Phone:</b> ${phone || "Not provided"}</p>
        <p><b>Message:</b></p>
        <p>${message}</p>
        <hr>
        <p>We will get back to you as soon as possible.</p>
        <p>Best regards,<br>The Sunventus Team</p>
      `,
    });

    return new Response(JSON.stringify({ success: true }), { status: 200 });
  } catch (error) {
    return new Response(JSON.stringify({ error }), { status: 500 });
  }
}
