package com.ga.api.controller.product;

import com.ga.api.service.product.ContractService;
import com.ga.common.annotation.DataChangeLog;
import com.ga.common.annotation.RequirePermission;
import com.ga.common.model.ApiResponse;
import com.ga.common.model.PageResponse;
import com.ga.core.vo.product.ContractResp;
import com.ga.core.vo.product.ContractSaveReq;
import com.ga.core.vo.product.ContractSearchParam;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "계약 관리")
@RestController
@RequestMapping("/api/contracts")
@RequiredArgsConstructor
public class ContractController {

    private final ContractService service;

    @GetMapping
    @RequirePermission(menu = "CONTRACT_LIST", perm = "READ")
    public ApiResponse<PageResponse<ContractResp>> list(@ModelAttribute ContractSearchParam param) {
        return ApiResponse.ok(service.list(param));
    }

    @GetMapping("/{contractId}")
    @RequirePermission(menu = "CONTRACT_LIST", perm = "READ")
    public ApiResponse<ContractResp> detail(@PathVariable Long contractId) {
        return ApiResponse.ok(service.detail(contractId));
    }

    @PostMapping
    @RequirePermission(menu = "CONTRACT_LIST", perm = "CREATE")
    @DataChangeLog(menu = "CONTRACT_LIST", table = "contract")
    public ApiResponse<Long> create(@Valid @RequestBody ContractSaveReq req) {
        return ApiResponse.ok(service.create(req));
    }

    @PutMapping("/{contractId}")
    @RequirePermission(menu = "CONTRACT_LIST", perm = "UPDATE")
    @DataChangeLog(menu = "CONTRACT_LIST", table = "contract")
    public ApiResponse<Void> update(@PathVariable Long contractId, @Valid @RequestBody ContractSaveReq req) {
        service.update(contractId, req);
        return ApiResponse.ok();
    }

    @PutMapping("/{contractId}/status")
    @RequirePermission(menu = "CONTRACT_LIST", perm = "UPDATE")
    @DataChangeLog(menu = "CONTRACT_LIST", table = "contract")
    public ApiResponse<Void> changeStatus(@PathVariable Long contractId, @RequestBody Map<String, String> body) {
        service.changeStatus(contractId, body.get("status"));
        return ApiResponse.ok();
    }
}
