package com.zioinfo.mes.item;

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. 조회 Viewer+, 생성/수정/삭제 Manager+(기준정보 가드).
 */
@RestController
@RequestMapping("/api/mes/items")
@RequiredArgsConstructor
public class ItemController {

    private final ItemService service;

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

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

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

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

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