package com.zioinfo.mes.production;

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;

/**
 * 생산실적 API. 조회 Viewer+, 실적 보고 Worker+.
 */
@RestController
@RequestMapping("/api/mes/production")
@RequiredArgsConstructor
public class ProductionController {

    private final ProductionService service;

    @GetMapping
    public ApiResponse<List<MesProduction>> list(@RequestParam(required = false) String woNo,
                                                 @RequestParam(required = false) String itemCode,
                                                 @RequestParam(defaultValue = "200") int limit) {
        return ApiResponse.ok(service.list(woNo, itemCode, limit));
    }

    @GetMapping("/{id}")
    public ApiResponse<MesProduction> get(@PathVariable Long id) {
        return ApiResponse.ok(service.get(id));
    }

    @GetMapping("/daily-output")
    public ApiResponse<List<Map<String, Object>>> dailyOutput(@RequestParam(defaultValue = "14") int days) {
        return ApiResponse.ok(service.dailyOutput(days));
    }

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

    /** 생산실적 보고 — LOT 생성·자재 투입·산출 입고·작업지시 누적. Worker+. */
    @PostMapping("/report")
    public ApiResponse<MesProduction> report(@RequestBody MesProduction p, Authentication auth) {
        return ApiResponse.ok(service.report(p, AuthSupport.actor(auth)));
    }
}
