import { NextResponse } from "next/server";
import { createNotification, getNotifications } from "@/lib/notifications";
import { notificationEmitter } from "@/lib/notifications";

export async function GET() {
  try {
    const notifications = getNotifications();
    return NextResponse.json(notifications);
  } catch (err) {
    return NextResponse.json({ error: err instanceof Error ? err.message : "Unknown error" }, { status: 500 });
  }
}

export async function POST(request: Request) {
  try {
    const body = await request.json();
    const { title, description, source, level } = body;
    if (!title) {
      return NextResponse.json({ error: "title is required" }, { status: 400 });
    }
    const notification = createNotification({ title, description, source, level });
    notificationEmitter.emitNew(notification);
    return NextResponse.json(notification, { status: 201 });
  } catch (err) {
    return NextResponse.json({ error: err instanceof Error ? err.message : "Unknown error" }, { status: 500 });
  }
}
