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

// SCM 발주 접수 처리 — reception_status='reception' 업데이트
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 purchase_order_master SET reception_status = 'reception' WHERE objid::text IN (${ph})`,
      objIds
    );
    return NextResponse.json({ success: true, message: `${objIds.length}건 접수 완료되었습니다.` });
  } catch (error) {
    console.error("SCM receipt:", error);
    return NextResponse.json({ success: false, message: "접수 중 오류가 발생했습니다." }, { status: 500 });
  }
}
