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 body = await req.json();
    const event = await prisma.timelineEvent.update({ where: { id }, data: body });
    return Response.json(event);
  } catch (err) {
    console.error("[timeline/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.timelineEvent.delete({ where: { id } });
    return Response.json({ success: true });
  } catch (err) {
    console.error("[timeline/DELETE]", err);
    return Response.json({ error: "Server error" }, { status: 500 });
  }
}
