const corsHeaders = { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type', }; Deno.serve(async (req) => { if (req.method === 'OPTIONS') { return new Response(null, { headers: corsHeaders }); } try { const { webhookUrl, message, product, profile } = await req.json(); if (!webhookUrl) { throw new Error('Discord webhook URL is required'); } const embed = { title: '💰 Price Alert Triggered!', description: message, color: 0x00ff00, fields: [ { name: 'Product', value: product.name, inline: false }, { name: 'Current Price', value: `${product.current_price} ${product.currency}`, inline: true }, { name: 'Target Price', value: `${profile.target_price} ${product.currency}`, inline: true }, { name: 'Store', value: product.store, inline: true }, { name: 'Link', value: product.url, inline: false } ], timestamp: new Date().toISOString() }; const response = await fetch(webhookUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ content: `🎯 **${profile.name}** - Price target reached!`, embeds: [embed] }) }); if (!response.ok) { throw new Error(`Discord API error: ${response.status}`); } console.log('Discord notification sent successfully'); return new Response( JSON.stringify({ success: true }), { headers: { ...corsHeaders, 'Content-Type': 'application/json' } } ); } catch (error) { console.error('Error sending Discord alert:', error); const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred'; return new Response( JSON.stringify({ error: errorMessage }), { status: 500, headers: { ...corsHeaders, 'Content-Type': 'application/json' } } ); } });