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

// 첨부파일 삭제 (soft delete: status='inactive')
// body: { objId } 단일 또는 { objIds: string[] } 배열 모두 허용
export async function POST(request: NextRequest) {
  const user = await getSession();
  if (!user) return NextResponse.json({ success: false }, { status: 401 });

  const body = await request.json();
  const list: string[] = Array.isArray(body.objIds)
    ? body.objIds.map(String).filter(Boolean)
    : body.objId
      ? [String(body.objId)]
      : [];

  if (list.length === 0) {
    return NextResponse.json({ success: false, message: "삭제할 파일을 선택하세요." });
  }

  try {
    const ph = list.map((_, i) => `$${i + 1}`).join(",");
    await execute(
      `UPDATE attach_file_info SET status = 'inactive' WHERE objid IN (${ph})`,
      list,
    );
    return NextResponse.json({ success: true, message: `${list.length}개 파일이 삭제되었습니다.` });
  } catch (error) {
    console.error("Files delete:", error);
    return NextResponse.json({ success: false, message: "삭제 중 오류가 발생했습니다." }, { status: 500 });
  }
}
