package com.zioinfo.mes.integration;

import com.zioinfo.mes.common.ApiResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * GUARDiA 연계 상태/조회 API. 모든 응답은 자격증명 새니타이즈됨.
 *
 * <p>RBAC: 조회 Viewer+.
 */
@RestController
@RequestMapping("/api/mes/integration")
@RequiredArgsConstructor
public class IntegrationController {

    private final ErpClient erp;
    private final ItsmClient itsm;
    private final OcrClient ocr;
    private final BiClient bi;

    /** 연계 솔루션 헬스 요약. */
    @GetMapping("/status")
    public ApiResponse<Map<String, Object>> status() {
        Map<String, Object> m = new LinkedHashMap<>();
        m.put("erp", erp.available());
        m.put("itsm", itsm.available());
        m.put("ocr", ocr.available());
        m.put("bi", bi.available());
        return ApiResponse.ok(m);
    }

    /** ERP 생산계획 조회(연계). */
    @GetMapping("/erp/plans")
    public ApiResponse<List<Object>> erpPlans() {
        return ApiResponse.ok(erp.fetchProductionPlans());
    }

    /** OCR 문서 파싱 결과 조회(연계). */
    @GetMapping("/ocr/{docId}")
    public ApiResponse<Map<String, Object>> ocrResult(@PathVariable String docId) {
        return ApiResponse.ok(ocr.fetchParsed(docId));
    }
}
