package com.zioinfo.mes.partner;

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+(PII 마스킹), 변경 Manager+.
 */
@RestController
@RequestMapping("/api/mes/partners")
@RequiredArgsConstructor
public class PartnerController {

    private final PartnerService service;

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

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

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

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

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