import { NextRequest, NextResponse } from "next/server";
import { db } from "@/lib/db";
import { getApiUser } from "@/lib/apiAuth";
export async function PUT(req: NextRequest) {
  const apiUser = await getApiUser(req);
  if (!apiUser || apiUser.role !== "ADMIN")
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 });

  const rules: Array<{ position: string; eventType: string; points: number }> = await req.json();

  for (const rule of rules) {
    await db.scoringRule.upsert({
      where: { position_eventType: { position: rule.position as any, eventType: rule.eventType as any } },
      update: { points: rule.points, updatedBy: apiUser.id },
      create: { position: rule.position as any, eventType: rule.eventType as any, points: rule.points, updatedBy: apiUser.id },
    });
  }

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