import { validateExternalAuth } from "../_shared/auth.ts"; 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("ok", { headers: corsHeaders }); try { const authHeader = req.headers.get("Authorization"); const supabase = createClient( Deno.env.get("SUPABASE_URL")!, Deno.env.get("SUPABASE_ANON_KEY")!, { global: { headers: { Authorization: authHeader! } } } ); const { data: { user }, } = await supabase.auth.getUser(); if (!user) throw new Error("Unauthorized"); const { months } = await req.json(); const monthsCount = months || 12; // Buscar stats por mês (em produção, agregar do external) const stats = []; for (let i = 0; i < monthsCount; i++) { const date = new Date(); date.setMonth(date.getMonth() - (monthsCount - i - 1)); stats.push({ month: date.toISOString().substring(0, 7), no_show_count: Math.floor(Math.random() * 20), total_appointments: Math.floor(Math.random() * 100) + 50, no_show_rate: (Math.random() * 20).toFixed(2) + "%", }); } const data = { stats, months: monthsCount }; return new Response(JSON.stringify({ success: true, data }), { headers: { ...corsHeaders, "Content-Type": "application/json" }, }); } catch (error: any) { return new Response( JSON.stringify({ success: false, error: error.message }), { status: 400, headers: { ...corsHeaders, "Content-Type": "application/json" }, } ); } });