import { NextResponse } from "next/server";
import { auth } from "@/auth";
import { getNotifications } from "@/lib/notifications";

export async function GET() {
  const session = await auth();
  if (!session?.user) {
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  }

  const { items, unread } = await getNotifications(session.user.id);
  return NextResponse.json({
    unread,
    items: items.map((n) => ({
      id: n.id,
      type: n.type,
      title: n.title,
      body: n.body,
      link: n.link,
      read: Boolean(n.readAt),
      createdAt: n.createdAt.toISOString(),
    })),
  });
}
