package com.ga.api.controller.rule;

import com.ga.api.service.rule.RuleService;
import com.ga.common.annotation.DataChangeLog;
import com.ga.common.annotation.RequirePermission;
import com.ga.common.model.ApiResponse;
import com.ga.core.vo.rule.*;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "수수료 규정")
@RestController
@RequestMapping("/api/rules")
@RequiredArgsConstructor
public class RuleController {

    private final RuleService ruleService;

    // ========== commission_rate ==========
    @GetMapping("/commission-rates")
    @RequirePermission(menu = "RULE_COMMISSION", perm = "READ")
    public ApiResponse<List<CommissionRateVO>> listCommissionRates(@RequestParam(required = false) Long productId,
                                                                    @RequestParam(required = false) Integer year) {
        return ApiResponse.ok(ruleService.listCommissionRates(productId, year));
    }

    @PostMapping("/commission-rates")
    @RequirePermission(menu = "RULE_COMMISSION", perm = "CREATE")
    @DataChangeLog(menu = "RULE_COMMISSION", table = "commission_rate")
    public ApiResponse<Long> createCommissionRate(@RequestBody CommissionRateVO vo) {
        return ApiResponse.ok(ruleService.createCommissionRate(vo));
    }

    @PutMapping("/commission-rates/{rateId}")
    @RequirePermission(menu = "RULE_COMMISSION", perm = "UPDATE")
    @DataChangeLog(menu = "RULE_COMMISSION", table = "commission_rate")
    public ApiResponse<Void> updateCommissionRate(@PathVariable Long rateId, @RequestBody CommissionRateVO vo) {
        ruleService.updateCommissionRate(rateId, vo);
        return ApiResponse.ok();
    }

    @DeleteMapping("/commission-rates/{rateId}")
    @RequirePermission(menu = "RULE_COMMISSION", perm = "DELETE")
    @DataChangeLog(menu = "RULE_COMMISSION", table = "commission_rate")
    public ApiResponse<Void> deleteCommissionRate(@PathVariable Long rateId) {
        ruleService.deleteCommissionRate(rateId);
        return ApiResponse.ok();
    }

    // ========== payout_rule ==========
    @GetMapping("/payout")
    @RequirePermission(menu = "RULE_PAYOUT", perm = "READ")
    public ApiResponse<List<PayoutRuleVO>> listPayoutRules(@RequestParam(required = false) Long gradeId,
                                                            @RequestParam(required = false) String insuranceType) {
        return ApiResponse.ok(ruleService.listPayoutRules(gradeId, insuranceType));
    }

    @PostMapping("/payout")
    @RequirePermission(menu = "RULE_PAYOUT", perm = "CREATE")
    @DataChangeLog(menu = "RULE_PAYOUT", table = "payout_rule")
    public ApiResponse<Long> createPayoutRule(@RequestBody PayoutRuleVO vo) {
        return ApiResponse.ok(ruleService.createPayoutRule(vo));
    }

    @PutMapping("/payout/{ruleId}")
    @RequirePermission(menu = "RULE_PAYOUT", perm = "UPDATE")
    @DataChangeLog(menu = "RULE_PAYOUT", table = "payout_rule")
    public ApiResponse<Void> updatePayoutRule(@PathVariable Long ruleId, @RequestBody PayoutRuleVO vo) {
        ruleService.updatePayoutRule(ruleId, vo);
        return ApiResponse.ok();
    }

    @DeleteMapping("/payout/{ruleId}")
    @RequirePermission(menu = "RULE_PAYOUT", perm = "DELETE")
    @DataChangeLog(menu = "RULE_PAYOUT", table = "payout_rule")
    public ApiResponse<Void> deletePayoutRule(@PathVariable Long ruleId) {
        ruleService.deletePayoutRule(ruleId);
        return ApiResponse.ok();
    }

    // ========== override_rule ==========
    @GetMapping("/override")
    @RequirePermission(menu = "RULE_OVERRIDE", perm = "READ")
    public ApiResponse<List<OverrideRuleVO>> listOverrideRules() {
        return ApiResponse.ok(ruleService.listOverrideRules());
    }

    @PostMapping("/override")
    @RequirePermission(menu = "RULE_OVERRIDE", perm = "CREATE")
    @DataChangeLog(menu = "RULE_OVERRIDE", table = "override_rule")
    public ApiResponse<Long> createOverrideRule(@RequestBody OverrideRuleVO vo) {
        return ApiResponse.ok(ruleService.createOverrideRule(vo));
    }

    @PutMapping("/override/{overrideId}")
    @RequirePermission(menu = "RULE_OVERRIDE", perm = "UPDATE")
    @DataChangeLog(menu = "RULE_OVERRIDE", table = "override_rule")
    public ApiResponse<Void> updateOverrideRule(@PathVariable Long overrideId, @RequestBody OverrideRuleVO vo) {
        ruleService.updateOverrideRule(overrideId, vo);
        return ApiResponse.ok();
    }

    @DeleteMapping("/override/{overrideId}")
    @RequirePermission(menu = "RULE_OVERRIDE", perm = "DELETE")
    @DataChangeLog(menu = "RULE_OVERRIDE", table = "override_rule")
    public ApiResponse<Void> deleteOverrideRule(@PathVariable Long overrideId) {
        ruleService.deleteOverrideRule(overrideId);
        return ApiResponse.ok();
    }

    // ========== chargeback_rule ==========
    @GetMapping("/chargeback")
    @RequirePermission(menu = "RULE_CHARGEBACK", perm = "READ")
    public ApiResponse<List<ChargebackRuleVO>> listChargebackRules(@RequestParam(required = false) String companyCode) {
        return ApiResponse.ok(ruleService.listChargebackRules(companyCode));
    }

    @PostMapping("/chargeback")
    @RequirePermission(menu = "RULE_CHARGEBACK", perm = "CREATE")
    @DataChangeLog(menu = "RULE_CHARGEBACK", table = "chargeback_rule")
    public ApiResponse<Long> createChargebackRule(@RequestBody ChargebackRuleVO vo) {
        return ApiResponse.ok(ruleService.createChargebackRule(vo));
    }

    @PutMapping("/chargeback/{cbRuleId}")
    @RequirePermission(menu = "RULE_CHARGEBACK", perm = "UPDATE")
    @DataChangeLog(menu = "RULE_CHARGEBACK", table = "chargeback_rule")
    public ApiResponse<Void> updateChargebackRule(@PathVariable Long cbRuleId, @RequestBody ChargebackRuleVO vo) {
        ruleService.updateChargebackRule(cbRuleId, vo);
        return ApiResponse.ok();
    }

    @DeleteMapping("/chargeback/{cbRuleId}")
    @RequirePermission(menu = "RULE_CHARGEBACK", perm = "DELETE")
    @DataChangeLog(menu = "RULE_CHARGEBACK", table = "chargeback_rule")
    public ApiResponse<Void> deleteChargebackRule(@PathVariable Long cbRuleId) {
        ruleService.deleteChargebackRule(cbRuleId);
        return ApiResponse.ok();
    }

    // ========== exception_type_code ==========
    @GetMapping("/exception-codes")
    @RequirePermission(menu = "RULE_EXCEPTION", perm = "READ")
    public ApiResponse<List<ExceptionTypeCodeVO>> listExceptionCodes() {
        return ApiResponse.ok(ruleService.listExceptionCodes());
    }

    @PostMapping("/exception-codes")
    @RequirePermission(menu = "RULE_EXCEPTION", perm = "CREATE")
    @DataChangeLog(menu = "RULE_EXCEPTION", table = "exception_type_code")
    public ApiResponse<Void> createExceptionCode(@RequestBody ExceptionTypeCodeVO vo) {
        ruleService.createExceptionCode(vo);
        return ApiResponse.ok();
    }

    @PutMapping("/exception-codes/{exceptionCode}")
    @RequirePermission(menu = "RULE_EXCEPTION", perm = "UPDATE")
    @DataChangeLog(menu = "RULE_EXCEPTION", table = "exception_type_code")
    public ApiResponse<Void> updateExceptionCode(@PathVariable String exceptionCode,
                                                 @RequestBody ExceptionTypeCodeVO vo) {
        ruleService.updateExceptionCode(exceptionCode, vo);
        return ApiResponse.ok();
    }
}
