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

// 부서관리 목록 조회
export async function POST(request: NextRequest) {
  const user = await getSession();
  if (!user) return NextResponse.json({ success: false }, { status: 401 });

  const sql = `
    SELECT D.dept_code AS "DEPT_CODE",
      D.dept_name AS "DEPT_NAME",
      COALESCE(D.company_name, '') AS "COMPANY_NAME",
      TO_CHAR(D.regdate, 'YYYY-MM-DD') AS "REGDATE",
      COALESCE(D.status, 'active') AS "STATUS",
      CASE WHEN COALESCE(D.status,'active')='active' THEN '활성' ELSE '비활성' END AS "STATUS_NAME"
    FROM dept_info D
    ORDER BY D.dept_code
  `;

  const rows = await queryRows(sql);
  return NextResponse.json({ RESULTLIST: rows, TOTAL_CNT: rows.length });
}
