package com.ga.api.controller.commission;

import com.ga.api.service.commission.JointContractService;
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.commission.JointContractResp;
import com.ga.core.vo.commission.JointContractSaveReq;
import com.ga.core.vo.commission.JointContractSearchParam;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "공동계약")
@RestController
@RequestMapping("/api/joint-contracts")
@RequiredArgsConstructor
public class JointContractController {

    private final JointContractService service;

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

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

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

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

    @DeleteMapping("/{jointId}")
    @RequirePermission(menu = "JOINT_CONTRACT", perm = "DELETE")
    @DataChangeLog(menu = "JOINT_CONTRACT", table = "joint_contract")
    public ApiResponse<Void> delete(@PathVariable Long jointId) {
        service.delete(jointId);
        return ApiResponse.ok();
    }
}
