package com.zioinfo.mes.ai;

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

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

/**
 * GUARDiA MES AI 도구 API — 8개 제조 AI(전부 Ollama + Java 폴백).
 *
 * <p>RBAC: 조회/분석 Viewer+ (GET), 분석 트리거 Worker+ (POST).
 */
@RestController
@RequestMapping("/api/mes/ai")
@RequiredArgsConstructor
public class AiController {

    private final AiService ai;

    @GetMapping("/status")
    public ApiResponse<Map<String, Object>> status() {
        return ApiResponse.ok(Map.of("ollamaAvailable", ai.ollamaAvailable()));
    }

    @PostMapping("/defect-root-cause")
    public ApiResponse<Map<String, Object>> defectRootCause(@RequestBody DefectRequest req) {
        return ApiResponse.ok(ai.defectRootCause(req.defectCode(), req.context()));
    }

    @PostMapping("/forecast")
    public ApiResponse<Map<String, Object>> forecast(@RequestBody ForecastRequest req) {
        return ApiResponse.ok(ai.forecast(req.series(), req.horizon()));
    }

    @PostMapping("/predictive-maintenance")
    public ApiResponse<Map<String, Object>> pdm(@RequestBody PdmRequest req) {
        return ApiResponse.ok(ai.predictiveMaintenance(
            req.equipmentCode(), req.availability(), req.downtimeCount(), req.mtbfHours()));
    }

    @PostMapping("/spc-anomaly")
    public ApiResponse<Map<String, Object>> spc(@RequestBody SpcRequest req) {
        return ApiResponse.ok(ai.spcAnomaly(req.values(), req.ucl(), req.lcl(), req.cl()));
    }

    @PostMapping("/parse-query")
    public ApiResponse<Map<String, Object>> parseQuery(@RequestBody QueryRequest req) {
        return ApiResponse.ok(ai.parseQuery(req.query()));
    }

    @PostMapping("/inspection-judge")
    public ApiResponse<Map<String, Object>> inspectionJudge(@RequestBody JudgeRequest req) {
        return ApiResponse.ok(ai.inspectionJudge(req.measured(), req.lsl(), req.usl(), req.target()));
    }

    @PostMapping("/safety-stock")
    public ApiResponse<Map<String, Object>> safetyStock(@RequestBody SafetyStockRequest req) {
        return ApiResponse.ok(ai.safetyStock(
            req.avgDailyDemand(), req.demandStdDev(), req.leadTimeDays(), req.serviceZ()));
    }

    @PostMapping("/schedule-optimize")
    public ApiResponse<List<Map<String, Object>>> scheduleOptimize(@RequestBody ScheduleRequest req) {
        return ApiResponse.ok(ai.scheduleOptimize(req.orders()));
    }

    // ── request DTO ──
    record DefectRequest(String defectCode, List<Map<String, Object>> context) {}
    record ForecastRequest(List<Double> series, int horizon) {}
    record PdmRequest(String equipmentCode, double availability, int downtimeCount, double mtbfHours) {}
    record SpcRequest(List<Double> values, double ucl, double lcl, double cl) {}
    record QueryRequest(String query) {}
    record JudgeRequest(double measured, Double lsl, Double usl, Double target) {}
    record SafetyStockRequest(double avgDailyDemand, double demandStdDev, double leadTimeDays, double serviceZ) {}
    record ScheduleRequest(List<Map<String, Object>> orders) {}
}
