import { NextResponse } from "next/server"
import { auth } from "@/lib/auth"
import { db } from "@/lib/db"

export async function PATCH(
  _req: Request,
  { params }: { params: Promise<{ id: string }> }
) {
  const session = await auth()
  if (!session?.user?.id) return NextResponse.json({ error: "Unauthorized" }, { status: 401 })

  try {
    const { id } = await params
    await db.notificationRecipient.updateMany({
      where: {
        notificationId: id,
        userId: session.user.id,
      },
      data: { readAt: new Date() },
    })

    return NextResponse.json({ ok: true })
  } catch (err) {
    console.error("markAsRead error:", err)
    return NextResponse.json({ error: "Failed to mark as read" }, { status: 500 })
  }
}