package com.ga.api.controller.commission;

import com.ga.api.service.commission.LapsedReinstatementService;
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.LapsedReinstatementRecoveryResp;
import com.ga.core.vo.commission.LapsedReinstatementRecoverySaveReq;
import com.ga.core.vo.commission.LapsedReinstatementRecoverySearchParam;
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/lapsed-reinstatements")
@RequiredArgsConstructor
public class LapsedReinstatementController {

    private final LapsedReinstatementService service;

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

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

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

    @PostMapping("/{recoveryId}/repay")
    @RequirePermission(menu = "LAPSED_REINSTATEMENT", perm = "UPDATE")
    @DataChangeLog(menu = "LAPSED_REINSTATEMENT", table = "lapsed_reinstatement_recovery")
    public ApiResponse<Void> repay(@PathVariable Long recoveryId) {
        service.repay(recoveryId);
        return ApiResponse.ok();
    }

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