package com.zioinfo.mes.integration;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;

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

/**
 * GUARDiA ERP 연계 — 생산계획·BOM·MRP 자재소요·생산실적→정산.
 *
 * <p>연계 실패 시 빈 결과(폴백은 호출 측). 응답은 새니타이즈됨.
 */
@Component
@RequiredArgsConstructor
public class ErpClient {

    @Value("${guardia.erp-url:http://localhost:8003}")
    private String erpUrl;

    private final GuardiaHttpClient http;

    /** ERP 생산계획 조회(연계 실패 시 빈 리스트 → MES 자체 plan 사용). */
    public List<Object> fetchProductionPlans() {
        return http.callList(erpUrl, "/api/erp/production/plans", HttpMethod.GET, null);
    }

    /** 생산실적 → ERP 정산 피드(연계 실패 시 무시). */
    public Map<String, Object> pushProductionResult(Map<String, Object> result) {
        return http.callMap(erpUrl, "/api/erp/production/result", HttpMethod.POST, result);
    }

    public boolean available() {
        return !http.callMap(erpUrl, "/actuator/health", HttpMethod.GET, null).isEmpty();
    }
}
