import { NextRequest, NextResponse } from "next/server";
import { db } from "@/lib/db";
import { getApiUser } from "@/lib/apiAuth";
export async function GET(req: NextRequest) {
  const gameweeks = await db.gameweek.findMany({ orderBy: { number: "asc" } });
  return NextResponse.json(gameweeks);
}

export async function POST(req: NextRequest) {
  const apiUser = await getApiUser(req);
  if (!apiUser || apiUser.role !== "ADMIN")
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 });

  const body = await req.json();
  const gw = await db.gameweek.create({
    data: {
      ...body,
      deadline: new Date(body.deadline),
    },
  });
  return NextResponse.json(gw, { status: 201 });
}
