import { NextRequest, NextResponse } from "next/server";
import { db, notifications, eq, and } from "@epicure/db";
import { requireSession } from "@/lib/api-auth";

export async function POST(req: NextRequest) {
  const { session, response } = await requireSession();
  if (response) return response;

  const body = (await req.json().catch(() => ({}))) as { id?: string };

  await db
    .update(notifications)
    .set({ read: true })
    .where(
      body.id
        ? and(eq(notifications.userId, session!.user.id), eq(notifications.id, body.id))
        : eq(notifications.userId, session!.user.id)
    );

  return NextResponse.json({ ok: true });
}
