import { type NextRequest } from "next/server";
import { prisma } from "@/lib/db";
import { requireAuth } from "@/lib/auth";

interface Params { params: Promise<{ id: string }> }

export async function PUT(req: NextRequest, { params }: Params) {
  try {
    await requireAuth();
    const { id } = await params;
    const { isRead } = await req.json();
    const msg = await prisma.contactMessage.update({
      where: { id },
      data: { isRead },
    });
    return Response.json(msg);
  } catch (err) {
    console.error("[messages/PUT]", err);
    return Response.json({ error: "Server error" }, { status: 500 });
  }
}

export async function DELETE(_req: NextRequest, { params }: Params) {
  try {
    await requireAuth();
    const { id } = await params;
    await prisma.contactMessage.delete({ where: { id } });
    return Response.json({ success: true });
  } catch (err) {
    console.error("[messages/DELETE]", err);
    return Response.json({ error: "Server error" }, { status: 500 });
  }
}
