package com.zioinfo.mes.stocktake;

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;

/**
 * 재고실사 API (WMS). 조회 Viewer+, 실사 카운트 Worker+, 차이 조정(재고보정) Manager+.
 */
@RestController
@RequestMapping("/api/mes/stocktake")
@RequiredArgsConstructor
public class StocktakeController {

    private final StocktakeService service;

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

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

    /** 실사 카운트 기록(차이 계산). */
    @PostMapping("/count")
    public ApiResponse<MesStocktake> count(@RequestBody MesStocktake s, Authentication auth) {
        return ApiResponse.ok(service.count(s, AuthSupport.actor(auth)));
    }

    /** 차이 조정 — 재고 보정(Manager+). */
    @PostMapping("/{id}/adjust")
    public ApiResponse<MesStocktake> adjust(@PathVariable Long id, Authentication auth) {
        AuthSupport.requireManager(auth);
        return ApiResponse.ok(service.adjust(id, AuthSupport.actor(auth)));
    }
}
