// ============================================================
// 개발관리 설계변경 리스트 (M5) 컨트롤러 — read-only.
//   GET /api/development/eo-history/list
//   GET /api/development/eo-history/:objid
// ============================================================

import { Response } from "express";
import { AuthenticatedRequest } from "../types/auth";
import * as svc from "../services/devEoHistoryService";
import { logger } from "../utils/logger";

function parseListFilter(q: Record<string, any>): svc.EoHistoryListFilter {
  const filter: svc.EoHistoryListFilter = { ...q };
  if (q.page) filter.page = Number(q.page);
  if (q.page_size) filter.page_size = Number(q.page_size);
  return filter;
}

export async function getList(req: AuthenticatedRequest, res: Response) {
  try {
    const data = await svc.list(parseListFilter(req.query as Record<string, any>));
    return res.json({ success: true, data });
  } catch (e: any) {
    logger.error("설계변경 리스트 실패", { error: e.message });
    return res.status(500).json({ success: false, message: e.message });
  }
}

export async function getByObjid(req: AuthenticatedRequest, res: Response) {
  try {
    const { objid } = req.params;
    const row = await svc.getByObjid(objid);
    if (!row) return res.status(404).json({ success: false, message: "not_found" });
    return res.json({ success: true, data: row });
  } catch (e: any) {
    logger.error("설계변경 상세 실패", { error: e.message });
    return res.status(500).json({ success: false, message: e.message });
  }
}
