import { NextRequest, NextResponse } from "next/server";
import { execute } from "@/lib/db";
import { getSession } from "@/lib/session";

// 권한그룹 상태 변경 (changeAuthGroupStatus 대응)
export async function POST(request: NextRequest) {
  const user = await getSession();
  if (!user) return NextResponse.json({ success: false }, { status: 401 });

  try {
    const body = await request.json();
    const { objids, status } = body;
    if (!objids || !Array.isArray(objids) || objids.length === 0) {
      return NextResponse.json({ success: false, message: "대상을 선택하세요." }, { status: 400 });
    }

    const placeholders = objids.map((_: unknown, i: number) => `$${i + 2}::numeric`).join(",");
    await execute(
      `UPDATE AUTHORITY_MASTER SET STATUS = $1 WHERE OBJID IN (${placeholders})`,
      [status, ...objids]
    );

    return NextResponse.json({ success: true, message: "상태가 변경되었습니다." });
  } catch (error) {
    console.error("Auth status:", error);
    return NextResponse.json({ success: false, message: "처리 중 오류가 발생했습니다." }, { status: 500 });
  }
}
