package com.ga.api.controller.commission;

import com.ga.api.service.commission.PersistencyBonusService;
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.*;
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/persistency-bonus")
@RequiredArgsConstructor
public class PersistencyBonusController {

    private final PersistencyBonusService service;

    @GetMapping("/rules")
    @RequirePermission(menu = "PERSIST_BONUS", perm = "READ")
    public ApiResponse<PageResponse<PersistencyBonusRuleResp>> listRules(
            @ModelAttribute PersistencyBonusRuleSearchParam param) {
        return ApiResponse.ok(service.listRules(param));
    }

    @GetMapping("/rules/{ruleId}")
    @RequirePermission(menu = "PERSIST_BONUS", perm = "READ")
    public ApiResponse<PersistencyBonusRuleResp> detailRule(@PathVariable Long ruleId) {
        return ApiResponse.ok(service.detailRule(ruleId));
    }

    @PostMapping("/rules")
    @RequirePermission(menu = "PERSIST_BONUS", perm = "CREATE")
    @DataChangeLog(menu = "PERSIST_BONUS", table = "persistency_bonus_rule")
    public ApiResponse<Long> createRule(@Valid @RequestBody PersistencyBonusRuleSaveReq req) {
        return ApiResponse.ok(service.createRule(req));
    }

    @PutMapping("/rules/{ruleId}")
    @RequirePermission(menu = "PERSIST_BONUS", perm = "UPDATE")
    @DataChangeLog(menu = "PERSIST_BONUS", table = "persistency_bonus_rule")
    public ApiResponse<Void> updateRule(@PathVariable Long ruleId,
                                        @Valid @RequestBody PersistencyBonusRuleSaveReq req) {
        service.updateRule(ruleId, req);
        return ApiResponse.ok();
    }

    @DeleteMapping("/rules/{ruleId}")
    @RequirePermission(menu = "PERSIST_BONUS", perm = "DELETE")
    @DataChangeLog(menu = "PERSIST_BONUS", table = "persistency_bonus_rule")
    public ApiResponse<Void> deleteRule(@PathVariable Long ruleId) {
        service.deleteRule(ruleId);
        return ApiResponse.ok();
    }

    @GetMapping("/bonuses")
    @RequirePermission(menu = "PERSIST_BONUS", perm = "READ")
    public ApiResponse<PageResponse<PersistencyBonusResp>> listBonuses(
            @ModelAttribute PersistencyBonusSearchParam param) {
        return ApiResponse.ok(service.listBonuses(param));
    }

    @GetMapping("/bonuses/{bonusId}")
    @RequirePermission(menu = "PERSIST_BONUS", perm = "READ")
    public ApiResponse<PersistencyBonusResp> detailBonus(@PathVariable Long bonusId) {
        return ApiResponse.ok(service.detailBonus(bonusId));
    }

    @PostMapping("/bonuses")
    @RequirePermission(menu = "PERSIST_BONUS", perm = "CREATE")
    @DataChangeLog(menu = "PERSIST_BONUS", table = "persistency_bonus")
    public ApiResponse<Void> upsertBonus(@Valid @RequestBody PersistencyBonusSaveReq req) {
        service.upsertBonus(req);
        return ApiResponse.ok();
    }
}
