package com.ga.api.controller.commission;

import com.ga.api.service.commission.GuaranteeInsuranceService;
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.AgentGuaranteeInsuranceResp;
import com.ga.core.vo.commission.AgentGuaranteeInsuranceSaveReq;
import com.ga.core.vo.commission.AgentGuaranteeInsuranceSearchParam;
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/guarantee-insurances")
@RequiredArgsConstructor
public class GuaranteeInsuranceController {

    private final GuaranteeInsuranceService service;

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

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

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

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

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