/**
 * wace_plm 데이터 import 컨트롤러 — SUPER_ADMIN 만 사용 가능
 */
import { Response } from "express";
import { AuthenticatedRequest } from "../types/auth";
import { WacePlmDataImportService } from "../services/wacePlmDataImportService";

function requireSuperAdmin(req: AuthenticatedRequest, res: Response): boolean {
  if (req.user?.userType !== "SUPER_ADMIN") {
    res.status(403).json({ success: false, message: "SUPER_ADMIN 권한이 필요합니다." });
    return false;
  }
  return true;
}

export class WacePlmDataImportController {
  static async importAll(req: AuthenticatedRequest, res: Response) {
    if (!requireSuperAdmin(req, res)) return;
    try {
      const data = await WacePlmDataImportService.importAll();
      return res.json({ success: true, data, message: "wace_plm 전체 데이터 import 완료" });
    } catch (e: any) {
      return res.status(500).json({ success: false, message: "import 실패", error: e?.message });
    }
  }
  static async importEcr(req: AuthenticatedRequest, res: Response) {
    if (!requireSuperAdmin(req, res)) return;
    try { return res.json({ success: true, data: await WacePlmDataImportService.importEcr() }); }
    catch (e: any) { return res.status(500).json({ success: false, message: e?.message }); }
  }
  static async importCs(req: AuthenticatedRequest, res: Response) {
    if (!requireSuperAdmin(req, res)) return;
    try { return res.json({ success: true, data: await WacePlmDataImportService.importCustomerMng() }); }
    catch (e: any) { return res.status(500).json({ success: false, message: e?.message }); }
  }
  static async importMasters(req: AuthenticatedRequest, res: Response) {
    if (!requireSuperAdmin(req, res)) return;
    try {
      const commCode = await WacePlmDataImportService.importCommCode();
      const userInfo = await WacePlmDataImportService.importUserInfo();
      const product  = await WacePlmDataImportService.importProduct();
      const part     = await WacePlmDataImportService.importPart();
      return res.json({ success: true, data: { commCode, userInfo, product, part } });
    } catch (e: any) { return res.status(500).json({ success: false, message: e?.message }); }
  }
  static async importApproval(req: AuthenticatedRequest, res: Response) {
    if (!requireSuperAdmin(req, res)) return;
    try { return res.json({ success: true, data: await WacePlmDataImportService.importApprovalAll() }); }
    catch (e: any) { return res.status(500).json({ success: false, message: e?.message }); }
  }
  static async importCommCode(req: AuthenticatedRequest, res: Response) {
    if (!requireSuperAdmin(req, res)) return;
    try { return res.json({ success: true, data: await WacePlmDataImportService.importCommCode() }); }
    catch (e: any) { return res.status(500).json({ success: false, message: e?.message }); }
  }
  static async importUserInfo(req: AuthenticatedRequest, res: Response) {
    if (!requireSuperAdmin(req, res)) return;
    try { return res.json({ success: true, data: await WacePlmDataImportService.importUserInfo() }); }
    catch (e: any) { return res.status(500).json({ success: false, message: e?.message }); }
  }

  /** wace_plm 의 comm_code 733 행 SQL 시드를 재실행 (부팅 시 자동 호출되지만 재시작 없이 즉시 반영용) */
  static async seedCommCodeFromFile(req: AuthenticatedRequest, res: Response) {
    if (!requireSuperAdmin(req, res)) return;
    try {
      const { ensureWaceCommCodeSeed } = await import("../services/waceCommCodeSeedService");
      await ensureWaceCommCodeSeed();
      return res.json({ success: true, message: "wace_plm comm_code 시드 적재 완료" });
    } catch (e: any) {
      return res.status(500).json({ success: false, message: e?.message });
    }
  }
}
