package com.ga.api.controller.endorse;

import com.ga.api.service.endorse.ContractEndorsementService;
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.endorse.ContractEndorsementResp;
import com.ga.core.vo.endorse.ContractEndorsementSaveReq;
import com.ga.core.vo.endorse.ContractEndorsementSearchParam;
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/contract-endorsements")
@RequiredArgsConstructor
public class ContractEndorsementController {

    private final ContractEndorsementService service;

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

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

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

    @PostMapping("/{endorsementId}/process")
    @RequirePermission(menu = "ENDORSE", perm = "UPDATE")
    @DataChangeLog(menu = "ENDORSE", table = "contract_endorsement")
    public ApiResponse<Void> process(@PathVariable Long endorsementId) {
        service.process(endorsementId);
        return ApiResponse.ok();
    }

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