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

export async function POST(req: NextRequest, { params }: { params: Promise<{ id: string }> }) {
  const { id } = await params;
  const session = await getServerSession(authOptions);
  if (!session || (session.user as any).role !== "ADMIN")
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 });

  const lineups: Array<{ countryId: string; formation: string; playerIds: string[] }> = await req.json();

  // حذف ترکیب‌های قبلی
  await db.matchLineup.deleteMany({ where: { matchId: id } });

  for (const l of lineups) {
    await db.matchLineup.create({
      data: { matchId: id, countryId: l.countryId, formation: l.formation, playerIds: l.playerIds },
    });
  }

  return NextResponse.json({ success: true });
}
