import { NextRequest, NextResponse } from "next/server" import { getSessionUser } from "@/lib/auth" import { query } from "@/lib/db" export async function GET() { try { const user = await getSessionUser() if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 }) const isAdmin = user.role === "admin" || user.role === "super_admin" if (!isAdmin) return NextResponse.json({ error: "Forbidden" }, { status: 403 }) const result = await query( `SELECT id, recipient, subject, body_text, created_at FROM sent_emails ORDER BY created_at DESC LIMIT 50`, ) return NextResponse.json({ emails: result.rows.map((r: any) => ({ id: r.id, recipient: r.recipient, subject: r.subject, bodyText: r.body_text, createdAt: r.created_at, })) }) } catch (error) { console.error("Emails GET error:", error) return NextResponse.json({ error: "Failed to load sent emails" }, { status: 500 }) } }