import { NextResponse } from "next/server"; import { createServiceClient } from "@/lib/supabase"; import { requireAdmin } from "@/lib/admin-auth"; export const dynamic = "force-dynamic"; export async function GET() { const check = await requireAdmin(); if (check instanceof NextResponse) return check; const db = createServiceClient(); const { data: alleAnfragen } = await db .from("anfragen") .select("id, status, created_at, name, betreff, email"); const anfragen = alleAnfragen ?? []; const kpi = { gesamt: anfragen.length, offen: anfragen.filter((a) => a.status === "offen").length, inBearbeitung: anfragen.filter((a) => a.status === "in_bearbeitung").length, abgeschlossen: anfragen.filter((a) => a.status === "abgeschlossen").length, }; const heute = new Date(); const monatsStats: { monat: string; label: string; count: number }[] = []; for (let i = 5; i >= 0; i--) { const d = new Date(heute); d.setMonth(d.getMonth() - i); const monat = `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}`; const label = d.toLocaleDateString("de-DE", { month: "short", year: "2-digit" }); const count = anfragen.filter((a) => a.created_at.startsWith(monat)).length; monatsStats.push({ monat, label, count }); } const { data: recentAnfragen } = await db .from("anfragen") .select("id, created_at, name, betreff, status, email") .order("created_at", { ascending: false }) .limit(20); return NextResponse.json({ kpi, monatsStats, recentAnfragen: recentAnfragen ?? [] }); }