import { NextRequest, NextResponse } from "next/server";
import { reorderTeamMembers } from "@/lib/db";
import { revalidatePath } from "next/cache";

export async function PUT(request: NextRequest) {
  const { ids } = await request.json() as { ids: number[] };

  if (!Array.isArray(ids) || ids.length === 0 || ids.length > 100 || !ids.every((id) => Number.isInteger(id) && id > 0)) {
    return NextResponse.json({ error: "ids must be a non-empty array of positive integers (max 100)" }, { status: 400 });
  }

  reorderTeamMembers(ids);
  revalidatePath("/");

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