// 부서 비활성화/활성화
import { NextRequest, NextResponse } from "next/server";
import { execute } from "@/lib/db";
import { getSession } from "@/lib/session";

export async function POST(req: NextRequest) {
  const user = await getSession();
  if (!user) return NextResponse.json({ success: false }, { status: 401 });
  const { deptCode, deptCodes, activate } = await req.json() as {
    deptCode?: string; deptCodes?: string[]; activate?: boolean;
  };
  const targets = deptCodes && deptCodes.length > 0 ? deptCodes : (deptCode ? [deptCode] : []);
  if (targets.length === 0) {
    return NextResponse.json({ success: false, message: "대상 부서를 선택하세요." }, { status: 400 });
  }
  const ph = targets.map((_, i) => `$${i + 1}`).join(",");
  const newStatus = activate ? "active" : "inActive";
  await execute(`UPDATE dept_info SET status=$${targets.length + 1} WHERE dept_code IN (${ph})`, [...targets, newStatus]);
  return NextResponse.json({ success: true, count: targets.length, status: newStatus });
}
