import { createClient } from 'jsr:@supabase/supabase-js@2' import { buildInquiryNotificationEmail } from '../_shared/email.ts' const FROM_ADDRESS = 'bookings@braidsbymira.com' const MIRA_EMAIL = 'hello@braidsbymira.com' Deno.serve(async (req) => { const payload = await req.json() const inquiry = payload.record const supabaseAdmin = createClient( Deno.env.get('SUPABASE_URL')!, Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!, ) const { data: product } = await supabaseAdmin .from('products') .select('name') .eq('id', inquiry.product_id) .single() const { subject, html } = buildInquiryNotificationEmail({ productName: product?.name ?? inquiry.product_id, contact: { name: inquiry.contact_name, email: inquiry.contact_email, notes: inquiry.contact_notes }, }) const resendApiKey = Deno.env.get('RESEND_API_KEY') await fetch('https://api.resend.com/emails', { method: 'POST', headers: { Authorization: `Bearer ${resendApiKey}`, 'Content-Type': 'application/json', }, body: JSON.stringify({ from: FROM_ADDRESS, to: MIRA_EMAIL, subject, html }), }) return new Response(JSON.stringify({ ok: true }), { headers: { 'Content-Type': 'application/json' } }) })