/**
 * Amaranth 전자결재 프록시 컨트롤러
 *  - 자바 ApprovalController 의 결재 관련 endpoint 들을 Node 로 포팅
 *  - 모든 요청은 amaranthApprovalClient 를 거쳐 외부 커넥션 정보로 호출됨
 */
import { Response } from "express";
import { AuthenticatedRequest } from "../types/auth";
import * as approval from "../services/amaranthApprovalClient";

export class AmaranthApprovalController {
  /** GET /api/amaranth-approval/box?empSeq=&compSeq=&deptSeq= — 결재함 조회 */
  static async box(req: AuthenticatedRequest, res: Response) {
    try {
      const data = await approval.getApprovalBoxList(
        String(req.query.empSeq || ""),
        String(req.query.compSeq || ""),
        req.query.deptSeq ? String(req.query.deptSeq) : undefined,
      );
      return res.json({ success: true, data });
    } catch (e: any) {
      return res.status(500).json({ success: false, message: e?.message });
    }
  }

  /** GET /api/amaranth-approval/docs?empSeq=&compSeq=&menuId=...&page=1 */
  static async docList(req: AuthenticatedRequest, res: Response) {
    try {
      const data = await approval.getApprovalDocList({
        empSeq: String(req.query.empSeq || ""),
        compSeq: String(req.query.compSeq || ""),
        menuId: String(req.query.menuId || ""),
        fromDt: req.query.fromDt ? String(req.query.fromDt) : undefined,
        toDt: req.query.toDt ? String(req.query.toDt) : undefined,
        keyWord: req.query.keyWord ? String(req.query.keyWord) : undefined,
        page: req.query.page ? String(req.query.page) : undefined,
        pageSize: req.query.pageSize ? String(req.query.pageSize) : undefined,
      });
      return res.json({ success: true, data });
    } catch (e: any) {
      return res.status(500).json({ success: false, message: e?.message });
    }
  }

  /** GET /api/amaranth-approval/docs/:docId?empSeq=&compSeq=&deptSeq= */
  static async docDetail(req: AuthenticatedRequest, res: Response) {
    try {
      const data = await approval.getApprovalDocDetail({
        empSeq: String(req.query.empSeq || ""),
        compSeq: String(req.query.compSeq || ""),
        deptSeq: String(req.query.deptSeq || ""),
        docId: req.params.docId,
      });
      return res.json({ success: true, data });
    } catch (e: any) {
      return res.status(500).json({ success: false, message: e?.message });
    }
  }

  /** POST /api/amaranth-approval/sso — body: {empSeq, approKey, mod, outProcessCode|formId, ...} */
  static async ssoUrl(req: AuthenticatedRequest, res: Response) {
    try {
      const data = await approval.getSsoUrl(req.body);
      return res.json({ success: true, data });
    } catch (e: any) {
      return res.status(500).json({ success: false, message: e?.message });
    }
  }

  /** POST /api/amaranth-approval/submit — body: {empSeq, payload} */
  static async submit(req: AuthenticatedRequest, res: Response) {
    try {
      const { empSeq, payload } = req.body;
      const data = await approval.submitApprovalDoc(empSeq, payload);
      return res.json({ success: true, data });
    } catch (e: any) {
      return res.status(500).json({ success: false, message: e?.message });
    }
  }

  /** POST /api/amaranth-approval/auth-token — body: {empSeq} (디버깅/테스트용) */
  static async authToken(req: AuthenticatedRequest, res: Response) {
    try {
      const data = await approval.getAuthToken(String(req.body.empSeq || ""));
      return res.json({ success: true, data });
    } catch (e: any) {
      return res.status(500).json({ success: false, message: e?.message });
    }
  }
}
