import { db,requireAdmin } from "@/lib/server";function csv(v:unknown){return `"${String(v??"").replaceAll('"','""')}"`}
export async function GET(request:Request){if(!await requireAdmin(request))return new Response("Unauthorized",{status:401});const type=new URL(request.url).searchParams.get("type")||"registrations";const table=type==="support"?"payments WHERE purpose='support'":type==="payments"?"payments":"registrations";const rows=(await db().prepare(`SELECT * FROM ${table} ORDER BY created_at DESC`).all()).results as Record<string,unknown>[];const keys=rows[0]?Object.keys(rows[0]):["No records"];const body=[keys.map(csv).join(","),...rows.map(r=>keys.map(k=>csv(r[k])).join(","))].join("\n");return new Response(body,{headers:{"content-type":"text/csv;charset=utf-8","content-disposition":`attachment; filename=ofm-${type}.csv`}})}
