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

// 구조 순번 저장 (원본: partMng/structureSeqSave.do)
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 childObjId = String(body.CHILD_OBJID || "");
  const seq = String(body.SEQ ?? "");

  if (!childObjId) {
    return NextResponse.json({ success: false, message: "CHILD_OBJID 누락" }, { status: 400 });
  }

  try {
    await execute(
      `UPDATE bom_part_qty SET seq = $1::numeric WHERE child_objid = $2`,
      [seq, childObjId],
    );
    return NextResponse.json({ success: true, result: true });
  } catch (e) {
    console.error("bom seq save:", e);
    return NextResponse.json({ success: false, message: "저장 실패" }, { status: 500 });
  }
}
