import { serve } from "https://deno.land/std@0.192.0/http/server.ts"; serve(async (req) => { // ✅ CORS preflight if (req?.method === "OPTIONS") { return new Response("ok", { headers: { "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Methods": "POST, OPTIONS", "Access-Control-Allow-Headers": "*", }, }); } try { const { email, inviterName, role, token, message, siteUrl } = await req?.json(); const RESEND_API_KEY = Deno?.env?.get("RESEND_API_KEY"); if (!RESEND_API_KEY) { throw new Error("RESEND_API_KEY is not set"); } const acceptUrl = `${siteUrl || "https://broadcastbeat.com"}/invite/accept?token=${token}`; const roleLabel = role === "editor" ? "Editor" : role === "admin" ? "Administrator" : "Contributor"; const htmlBody = `

BroadcastBeat

Digital Platform for Broadcast Engineering

You've been invited to contribute

${inviterName || "A BroadcastBeat administrator"} has invited you to join BroadcastBeat as a ${roleLabel}.

${message ? `

"${message}"

` : ""}

As a ${roleLabel}, you'll be able to submit articles for publication on our platform. Click the button below to accept your invitation and set up your account.

Accept Invitation

This invitation expires in 7 days. If you did not expect this invitation, you can safely ignore this email.

Or copy this link: ${acceptUrl}

© BroadcastBeat · Digital Platform for Broadcast Engineering

`; const res = await fetch("https://api.resend.com/emails", { method: "POST", headers: { "Authorization": `Bearer ${RESEND_API_KEY}`, "Content-Type": "application/json", }, body: JSON.stringify({ from: "onboarding@resend.dev", to: [email], subject: `You've been invited to contribute to BroadcastBeat`, html: htmlBody, }), }); const data = await res?.json(); if (!res?.ok) { throw new Error(data?.message || "Failed to send email"); } return new Response(JSON.stringify({ success: true, id: data.id }), { headers: { "Content-Type": "application/json", "Access-Control-Allow-Origin": "*", }, }); } catch (error) { return new Response(JSON.stringify({ error: error.message }), { status: 500, headers: { "Content-Type": "application/json", "Access-Control-Allow-Origin": "*", }, }); } });