/**
 * BOM 이력/버전 관리 컨트롤러
 */

import { Request, Response } from "express";
import { logger } from "../utils/logger";
import * as bomService from "../services/bomService";

// ─── 이력 (History) ─────────────────────────────

export async function getBomHistory(req: Request, res: Response) {
  try {
    const { bomId } = req.params;
    const companyCode = (req as any).user?.companyCode || "*";
    const tableName = (req.query.tableName as string) || undefined;

    const data = await bomService.getBomHistory(bomId, companyCode, tableName);
    res.json({ success: true, data });
  } catch (error: any) {
    logger.error("BOM 이력 조회 실패", { error: error.message });
    res.status(500).json({ success: false, message: error.message });
  }
}

export async function addBomHistory(req: Request, res: Response) {
  try {
    const { bomId } = req.params;
    const companyCode = (req as any).user?.companyCode || "*";
    const changedBy = (req as any).user?.userName || (req as any).user?.userId || "";

    const { change_type, change_description, revision, version, tableName } = req.body;
    if (!change_type) {
      res.status(400).json({ success: false, message: "change_type은 필수입니다" });
      return;
    }

    const result = await bomService.addBomHistory(bomId, companyCode, {
      change_type,
      change_description,
      revision,
      version,
      changed_by: changedBy,
    }, tableName);
    res.json({ success: true, data: result });
  } catch (error: any) {
    logger.error("BOM 이력 등록 실패", { error: error.message });
    res.status(500).json({ success: false, message: error.message });
  }
}

// ─── BOM 헤더 조회 (entity join 포함) ─────────────────────────

export async function getBomHeader(req: Request, res: Response) {
  try {
    const { bomId } = req.params;
    const tableName = (req.query.tableName as string) || undefined;

    const data = await bomService.getBomHeader(bomId, tableName);
    if (!data) {
      res.status(404).json({ success: false, message: "BOM을 찾을 수 없습니다" });
      return;
    }
    res.json({ success: true, data });
  } catch (error: any) {
    logger.error("BOM 헤더 조회 실패", { error: error.message });
    res.status(500).json({ success: false, message: error.message });
  }
}

// ─── 버전 (Version) ─────────────────────────────

export async function getBomVersions(req: Request, res: Response) {
  try {
    const { bomId } = req.params;
    const companyCode = (req as any).user?.companyCode || "*";
    const tableName = (req.query.tableName as string) || undefined;

    const result = await bomService.getBomVersions(bomId, companyCode, tableName);
    res.json({
      success: true,
      data: result.versions,
      currentVersionId: result.currentVersionId,
    });
  } catch (error: any) {
    logger.error("BOM 버전 목록 조회 실패", { error: error.message });
    res.status(500).json({ success: false, message: error.message });
  }
}

export async function createBomVersion(req: Request, res: Response) {
  try {
    const { bomId } = req.params;
    const companyCode = (req as any).user?.companyCode || "*";
    const createdBy = (req as any).user?.userName || (req as any).user?.userId || "";
    const { tableName, detailTable, versionName } = req.body || {};

    const result = await bomService.createBomVersion(bomId, companyCode, createdBy, tableName, detailTable, versionName);
    res.json({ success: true, data: result });
  } catch (error: any) {
    logger.error("BOM 버전 생성 실패", { error: error.message });
    res.status(500).json({ success: false, message: error.message });
  }
}

export async function loadBomVersion(req: Request, res: Response) {
  try {
    const { bomId, versionId } = req.params;
    const companyCode = (req as any).user?.companyCode || "*";
    const { tableName, detailTable } = req.body || {};

    const result = await bomService.loadBomVersion(bomId, versionId, companyCode, tableName, detailTable);
    res.json({ success: true, data: result });
  } catch (error: any) {
    logger.error("BOM 버전 불러오기 실패", { error: error.message });
    res.status(500).json({ success: false, message: error.message });
  }
}

export async function activateBomVersion(req: Request, res: Response) {
  try {
    const { bomId, versionId } = req.params;
    const { tableName } = req.body || {};

    const result = await bomService.activateBomVersion(bomId, versionId, tableName);
    res.json({ success: true, data: result });
  } catch (error: any) {
    logger.error("BOM 버전 사용 확정 실패", { error: error.message });
    res.status(500).json({ success: false, message: error.message });
  }
}

export async function initializeBomVersion(req: Request, res: Response) {
  try {
    const { bomId } = req.params;
    const companyCode = (req as any).user?.companyCode || "*";
    const createdBy = (req as any).user?.userName || (req as any).user?.userId || "";

    const result = await bomService.initializeBomVersion(bomId, companyCode, createdBy);
    res.json({ success: true, data: result });
  } catch (error: any) {
    logger.error("BOM 초기 버전 생성 실패", { error: error.message });
    res.status(500).json({ success: false, message: error.message });
  }
}

// ─── BOM 엑셀 업로드/다운로드 ─────────────────────────

export async function createBomFromExcel(req: Request, res: Response) {
  try {
    const companyCode = (req as any).user?.companyCode || "*";
    const userId = (req as any).user?.userName || (req as any).user?.userId || "";
    const { rows } = req.body;

    if (!rows || !Array.isArray(rows) || rows.length === 0) {
      res.status(400).json({ success: false, message: "업로드할 데이터가 없습니다" });
      return;
    }

    const result = await bomService.createBomFromExcel(companyCode, userId, rows);
    if (!result.success) {
      res.status(400).json({ success: false, message: result.errors.join(", "), data: result });
      return;
    }

    res.json({ success: true, data: result });
  } catch (error: any) {
    logger.error("BOM 엑셀 업로드 실패", { error: error.message });
    res.status(500).json({ success: false, message: error.message });
  }
}

export async function createBomVersionFromExcel(req: Request, res: Response) {
  try {
    const { bomId } = req.params;
    const companyCode = (req as any).user?.companyCode || "*";
    const userId = (req as any).user?.userName || (req as any).user?.userId || "";
    const { rows, versionName } = req.body;

    if (!rows || !Array.isArray(rows) || rows.length === 0) {
      res.status(400).json({ success: false, message: "업로드할 데이터가 없습니다" });
      return;
    }

    const result = await bomService.createBomVersionFromExcel(bomId, companyCode, userId, rows, versionName);
    if (!result.success) {
      res.status(400).json({ success: false, message: result.errors.join(", "), data: result });
      return;
    }

    res.json({ success: true, data: result });
  } catch (error: any) {
    logger.error("BOM 버전 엑셀 업로드 실패", { error: error.message });
    res.status(500).json({ success: false, message: error.message });
  }
}

export async function downloadBomExcelData(req: Request, res: Response) {
  try {
    const { bomId } = req.params;
    const companyCode = (req as any).user?.companyCode || "*";

    const data = await bomService.downloadBomExcelData(bomId, companyCode);
    res.json({ success: true, data });
  } catch (error: any) {
    logger.error("BOM 엑셀 다운로드 데이터 조회 실패", { error: error.message });
    res.status(500).json({ success: false, message: error.message });
  }
}

export async function deleteBomVersion(req: Request, res: Response) {
  try {
    const { bomId, versionId } = req.params;
    const tableName = (req.query.tableName as string) || undefined;
    const detailTable = (req.query.detailTable as string) || undefined;

    const deleted = await bomService.deleteBomVersion(bomId, versionId, tableName, detailTable);
    if (!deleted) {
      res.status(404).json({ success: false, message: "버전을 찾을 수 없습니다" });
      return;
    }
    res.json({ success: true });
  } catch (error: any) {
    logger.error("BOM 버전 삭제 실패", { error: error.message });
    res.status(500).json({ success: false, message: error.message });
  }
}
