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

export async function GET() {
  try {
    await requireAuth();
    const events = await prisma.timelineEvent.findMany({
      orderBy: [{ sortOrder: "asc" }, { date: "desc" }],
    });
    return Response.json(events);
  } catch {
    return Response.json({ error: "Unauthorized" }, { status: 401 });
  }
}

export async function POST(req: NextRequest) {
  try {
    await requireAuth();
    const body = await req.json();
    const event = await prisma.timelineEvent.create({ data: body });
    return Response.json(event, { status: 201 });
  } catch (err) {
    console.error("[timeline/POST]", err);
    return Response.json({ error: "Server error" }, { status: 500 });
  }
}
