package com.zioinfo.mes.job;

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 — 작업/작업자/설비 배정·할당·조회·진행·이력.
 *
 * <p>RBAC: 조회 Viewer+, 생성/배정/진행/실적 Worker+, 삭제 Manager+.
 */
@RestController
@RequestMapping("/api/mes/jobs")
@RequiredArgsConstructor
public class JobController {

    private final JobService service;

    @GetMapping
    public ApiResponse<List<MesJob>> list(@RequestParam(required = false) String status,
                                         @RequestParam(required = false) String workerId,
                                         @RequestParam(required = false) String woNo,
                                         @RequestParam(required = false) String equipmentCode) {
        return ApiResponse.ok(service.list(status, workerId, woNo, equipmentCode));
    }

    /** 작업 현황 보드(상태별 카운트). */
    @GetMapping("/board")
    public ApiResponse<List<Map<String, Object>>> board() {
        return ApiResponse.ok(service.statusBoard());
    }

    /** 작업자별 부하(진행중 작업/잔여수량). */
    @GetMapping("/workload")
    public ApiResponse<List<Map<String, Object>>> workload() {
        return ApiResponse.ok(service.workload());
    }

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

    /** 작업 생성(작업지시 공정 할당). */
    @PostMapping
    public ApiResponse<MesJob> create(@RequestBody MesJob job, Authentication auth) {
        return ApiResponse.ok(service.create(job, AuthSupport.actor(auth)));
    }

    @PutMapping("/{id}")
    public ApiResponse<MesJob> update(@PathVariable Long id, @RequestBody MesJob job, Authentication auth) {
        return ApiResponse.ok(service.update(id, job, AuthSupport.actor(auth)));
    }

    /** 작업자/설비 배정. */
    @PostMapping("/{id}/assign")
    public ApiResponse<MesJob> assign(@PathVariable Long id, @RequestBody AssignRequest req, Authentication auth) {
        return ApiResponse.ok(service.assign(id, req.workerId(), req.equipmentCode(), AuthSupport.actor(auth)));
    }

    /** 진행 상태 전이(START/PAUSE/RESUME/DONE/CANCEL). */
    @PostMapping("/{id}/transition")
    public ApiResponse<MesJob> transition(@PathVariable Long id, @RequestBody TransitionRequest req, Authentication auth) {
        return ApiResponse.ok(service.transition(id, req.action(), AuthSupport.actor(auth)));
    }

    /** 작업 완료 수량 보고. */
    @PostMapping("/{id}/report")
    public ApiResponse<MesJob> report(@PathVariable Long id, @RequestBody QtyRequest req, Authentication auth) {
        return ApiResponse.ok(service.reportDone(id, req.qty(), AuthSupport.actor(auth)));
    }

    @DeleteMapping("/{id}")
    public ApiResponse<Void> delete(@PathVariable Long id, Authentication auth) {
        AuthSupport.requireManager(auth);
        service.delete(id);
        return ApiResponse.ok(null);
    }

    record AssignRequest(String workerId, String equipmentCode) {}
    record TransitionRequest(String action) {}
    record QtyRequest(double qty) {}
}
