const prerender = false;
const POST = async ({ request }) => {
  const data = await request.formData();
  const nom = data.get("nom")?.toString().trim();
  const prenom = data.get("prenom")?.toString().trim();
  const email = data.get("email")?.toString().trim();
  const telephone = data.get("telephone")?.toString().trim() ?? "";
  const centre = data.get("centre")?.toString().trim();
  const message = data.get("message")?.toString().trim();
  const rgpd = data.get("rgpd");
  if (!nom || !prenom || !email || !telephone || !centre || !message || !rgpd) {
    return new Response(JSON.stringify({ error: "Champs obligatoires manquants." }), {
      status: 400,
      headers: { "Content-Type": "application/json" }
    });
  }
  const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
  if (!emailRegex.test(email)) {
    return new Response(JSON.stringify({ error: "Adresse e-mail invalide." }), {
      status: 400,
      headers: { "Content-Type": "application/json" }
    });
  }
  const centreLabel = centre === "venelles" ? "Centre Beaujour Aix-en-Provence" : "Centre Beaujour Marseille";
  const apiKey = "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";
  const payload = {
    sender: { name: "Site Dr Florian Groff", email: "noreply@drfloriangroff.fr" },
    to: [{ email: "contact@beaujour.com", name: "Dr Florian Groff" }],
    replyTo: { email, name: `${prenom} ${nom}` },
    subject: `Nouvelle demande de contact : ${centreLabel}`,
    htmlContent: `
      <h2>Nouvelle demande de contact</h2>
      <p><strong>Nom :</strong> ${nom} ${prenom}</p>
      <p><strong>E-mail :</strong> ${email}</p>
      ${telephone ? `<p><strong>Téléphone :</strong> ${telephone}</p>` : ""}
      <p><strong>Centre souhaité :</strong> ${centreLabel}</p>
      <p><strong>Message :</strong></p>
      <blockquote style="border-left:3px solid #C8A97E;padding-left:1rem;color:#555">${message.replace(/\n/g, "<br>")}</blockquote>
    `
  };
  try {
    const res = await fetch("https://api.brevo.com/v3/smtp/email", {
      method: "POST",
      headers: {
        "api-key": apiKey,
        "Content-Type": "application/json",
        Accept: "application/json"
      },
      body: JSON.stringify(payload)
    });
    if (!res.ok) {
      const err = await res.text();
      console.error("Brevo error:", err);
      return new Response(JSON.stringify({ error: "Erreur d'envoi. Veuillez réessayer." }), {
        status: 500,
        headers: { "Content-Type": "application/json" }
      });
    }
    return new Response(JSON.stringify({ success: true }), {
      status: 200,
      headers: { "Content-Type": "application/json" }
    });
  } catch (err) {
    console.error("Fetch error:", err);
    return new Response(JSON.stringify({ error: "Erreur réseau." }), {
      status: 500,
      headers: { "Content-Type": "application/json" }
    });
  }
};

const _page = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.defineProperty({
  __proto__: null,
  POST,
  prerender
}, Symbol.toStringTag, { value: 'Module' }));

const page = () => _page;

export { page };
