package com.zioinfo.mes.shipping;

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 (WMS) — 출하지시·피킹/패킹·출하검사·출하실적·납품서/거래명세서.
 *
 * <p>RBAC: 조회 Viewer+, 등록/피킹/패킹/검사/출하 Worker+, 삭제 Manager+.
 */
@RestController
@RequestMapping("/api/mes/shipping")
@RequiredArgsConstructor
public class ShippingController {

    private final ShippingService service;

    @GetMapping
    public ApiResponse<List<MesShipping>> list(@RequestParam(required = false) String status,
                                              @RequestParam(required = false) String partnerCode,
                                              @RequestParam(required = false) String keyword) {
        return ApiResponse.ok(service.list(status, partnerCode, keyword));
    }

    @GetMapping("/board")
    public ApiResponse<List<Map<String, Object>>> board() {
        return ApiResponse.ok(service.statusCounts());
    }

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

    /** 출하지시 생성. */
    @PostMapping
    public ApiResponse<MesShipping> create(@RequestBody MesShipping s, Authentication auth) {
        return ApiResponse.ok(service.create(s, AuthSupport.actor(auth)));
    }

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

    /** 피킹. */
    @PostMapping("/{id}/pick")
    public ApiResponse<MesShipping> pick(@PathVariable Long id, @RequestBody QtyRequest req, Authentication auth) {
        return ApiResponse.ok(service.pick(id, req.qty(), AuthSupport.actor(auth)));
    }

    /** 패킹. */
    @PostMapping("/{id}/pack")
    public ApiResponse<MesShipping> pack(@PathVariable Long id, @RequestBody QtyRequest req, Authentication auth) {
        return ApiResponse.ok(service.pack(id, req.qty(), AuthSupport.actor(auth)));
    }

    /** 출하검사(OQC). */
    @PostMapping("/{id}/inspect")
    public ApiResponse<MesShipping> inspect(@PathVariable Long id, @RequestBody InspectRequest req, Authentication auth) {
        return ApiResponse.ok(service.inspect(id, req.result(), AuthSupport.actor(auth)));
    }

    /** 출하확정 — 재고 감소 + 납품서/거래명세서 발번. */
    @PostMapping("/{id}/ship")
    public ApiResponse<MesShipping> ship(@PathVariable Long id, Authentication auth) {
        return ApiResponse.ok(service.ship(id, AuthSupport.actor(auth)));
    }

    @PostMapping("/{id}/cancel")
    public ApiResponse<MesShipping> cancel(@PathVariable Long id, Authentication auth) {
        return ApiResponse.ok(service.cancel(id, AuthSupport.actor(auth)));
    }

    record QtyRequest(double qty) {}
    record InspectRequest(String result) {}
}
