package com.ga.api.controller.succession;

import com.ga.api.service.succession.ContractSuccessionService;
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.succession.ContractSuccessionResp;
import com.ga.core.vo.succession.ContractSuccessionSaveReq;
import com.ga.core.vo.succession.ContractSuccessionSearchParam;
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-successions")
@RequiredArgsConstructor
public class ContractSuccessionController {

    private final ContractSuccessionService service;

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

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

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

    @PostMapping("/{successionId}/cancel")
    @RequirePermission(menu = "CONTRACT_SUCCESSION", perm = "UPDATE")
    @DataChangeLog(menu = "CONTRACT_SUCCESSION", table = "contract_succession")
    public ApiResponse<Void> cancel(@PathVariable Long successionId) {
        service.cancel(successionId);
        return ApiResponse.ok();
    }

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