package com.zioinfo.mes.oee;

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

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

/**
 * OEE API — 기록 입력 + 산식 + 비가동 파레토. 조회 Viewer+, 입력 Worker+.
 */
@RestController
@RequestMapping("/api/mes/oee")
@RequiredArgsConstructor
public class OeeController {

    private final OeeService service;

    @GetMapping
    public ApiResponse<List<MesOeeRecord>> list(@RequestParam(required = false) String equipmentCode,
                                                @RequestParam(defaultValue = "100") int limit) {
        return ApiResponse.ok(service.list(equipmentCode, limit));
    }

    @PostMapping
    public ApiResponse<MesOeeRecord> create(@RequestBody MesOeeRecord r, Authentication auth) {
        return ApiResponse.ok(service.create(r, AuthSupport.actor(auth)));
    }

    /** 입력값으로 즉시 OEE 계산(저장 없음). */
    @PostMapping("/compute")
    public ApiResponse<Map<String, Object>> compute(@RequestBody MesOeeRecord r) {
        return ApiResponse.ok(service.compute(r));
    }

    @GetMapping("/{id}/oee")
    public ApiResponse<Map<String, Object>> computeById(@PathVariable Long id) {
        return ApiResponse.ok(service.computeById(id));
    }

    @GetMapping("/downtime-pareto")
    public ApiResponse<List<Map<String, Object>>> downtimePareto(@RequestParam(defaultValue = "30") int days) {
        return ApiResponse.ok(service.downtimePareto(days));
    }
}
