import { serve } from 'https://deno.land/std@0.168.0/http/server.ts'; const RESEND_API_KEY = Deno.env.get('RESEND_API_KEY'); const FROM = 'Fourge Branding '; const corsHeaders = { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type', }; serve(async (req) => { if (req.method === 'OPTIONS') { return new Response('ok', { headers: corsHeaders }); } try { const { type, to, data } = await req.json(); let subject = ''; let html = ''; if (type === 'new_request') { subject = `New Request: ${data.serviceType} — ${data.clientName}`; html = `

New Request Received

From: ${data.clientName} (${data.clientEmail})

Company: ${data.company || '—'}

Service: ${data.serviceType}

Project: ${data.projectName}

${data.deadline ? `

Deadline: ${data.deadline}

` : ''}

Description:

${data.description}


View Job `; } else if (type === 'sent_to_client') { subject = `Your ${data.serviceType} is ready for review!`; html = `

Hi ${data.clientFirstName},

Your ${data.serviceType} for ${data.projectName} is ready for review.

${data.message ? `

${data.message}

` : ''}

Please log in to the portal to review and approve or request changes.


Review Your Work

— The Fourge Branding Team

`; } else if (type === 'revision_submitted') { subject = `Revision Request: ${data.serviceType} — ${data.clientName}`; html = `

Revision Requested

From: ${data.clientName}

Job: ${data.serviceType} — ${data.projectName}

New Version: ${data.version}

${data.deadline ? `

New Deadline: ${data.deadline}

` : ''}

Requested changes:

${data.description}


View Job `; } else if (type === 'client_approved') { subject = `Approved! ${data.serviceType} — ${data.clientName}`; html = `

Job Approved ✓

${data.clientName} has approved ${data.serviceType} for ${data.projectName}.

This job is now complete.


View Job `; } 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: FROM, to, subject, html }), }); const result = await res.json(); return new Response(JSON.stringify(result), { headers: { ...corsHeaders, 'Content-Type': 'application/json' }, status: res.ok ? 200 : 400, }); } catch (err) { return new Response(JSON.stringify({ error: err.message }), { headers: { ...corsHeaders, 'Content-Type': 'application/json' }, status: 500, }); } });