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

// CS 결재상신 처리 — status_cd='approvalRequest' 업데이트
export async function POST(request: NextRequest) {
  const user = await getSession();
  if (!user) return NextResponse.json({ success: false }, { status: 401 });
  const { objIds } = await request.json();
  if (!objIds?.length) return NextResponse.json({ success: false, message: "상신할 항목을 선택하세요." });
  try {
    const ph = objIds.map((_: string, i: number) => `$${i + 1}`).join(",");
    await execute(
      `UPDATE as_mng SET status_cd = 'approvalRequest' WHERE objid IN (${ph})`,
      objIds
    );
    return NextResponse.json({ success: true, message: `${objIds.length}건 결재상신되었습니다.` });
  } catch (error) {
    console.error("CS approval:", error);
    return NextResponse.json({ success: false, message: "상신 중 오류가 발생했습니다." }, { status: 500 });
  }
}
