import { NextRequest, NextResponse } from "next/server";
import { db } from "@/lib/db";
import { getServerSession } from "next-auth";
import { authOptions } from "@/lib/auth";

// PATCH /api/admin/players/[id]/golden-toggle
export async function PATCH(req: NextRequest, { params }: { params: Promise<{ id: string }> }) {
  const session = await getServerSession(authOptions);
  if (!session || (session.user as any).role !== "ADMIN") {
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  }

  const { id } = await params;

  const player = await db.player.findUnique({ where: { id } });
  if (!player) return NextResponse.json({ error: "Player not found" }, { status: 404 });

  const updated = await db.player.update({
    where: { id },
    data: { isGoldenCardEligible: !player.isGoldenCardEligible },
  });

  return NextResponse.json({ isGoldenCardEligible: updated.isGoldenCardEligible });
}
