package com.ga.api.service.rule;

import com.ga.common.exception.BizException;
import com.ga.common.exception.ErrorCode;
import com.ga.core.mapper.rule.RuleMapper;
import com.ga.core.vo.rule.*;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class RuleService {

    private final RuleMapper mapper;

    // === commission_rate ===

    @Transactional(readOnly = true)
    public List<CommissionRateVO> listCommissionRates(Long productId, Integer year) {
        return mapper.selectCommissionRates(productId, year);
    }

    @Transactional
    public Long createCommissionRate(CommissionRateVO vo) {
        mapper.insertCommissionRate(vo);
        return vo.getRateId();
    }

    @Transactional
    public void updateCommissionRate(Long rateId, CommissionRateVO vo) {
        vo.setRateId(rateId);
        mapper.updateCommissionRate(vo);
    }

    @Transactional
    public void deleteCommissionRate(Long rateId) {
        mapper.deleteCommissionRate(rateId);
    }

    // === payout_rule ===

    @Transactional(readOnly = true)
    public List<PayoutRuleVO> listPayoutRules(Long gradeId, String insuranceType) {
        return mapper.selectPayoutRules(gradeId, insuranceType);
    }

    @Transactional
    public Long createPayoutRule(PayoutRuleVO vo) {
        mapper.insertPayoutRule(vo);
        return vo.getRuleId();
    }

    @Transactional
    public void updatePayoutRule(Long ruleId, PayoutRuleVO vo) {
        vo.setRuleId(ruleId);
        mapper.updatePayoutRule(vo);
    }

    @Transactional
    public void deletePayoutRule(Long ruleId) {
        mapper.deletePayoutRule(ruleId);
    }

    // === override_rule ===

    @Transactional(readOnly = true)
    public List<OverrideRuleVO> listOverrideRules() {
        return mapper.selectOverrideRules();
    }

    @Transactional
    public Long createOverrideRule(OverrideRuleVO vo) {
        mapper.insertOverrideRule(vo);
        return vo.getOverrideId();
    }

    @Transactional
    public void updateOverrideRule(Long overrideId, OverrideRuleVO vo) {
        vo.setOverrideId(overrideId);
        mapper.updateOverrideRule(vo);
    }

    @Transactional
    public void deleteOverrideRule(Long overrideId) {
        mapper.deleteOverrideRule(overrideId);
    }

    // === chargeback_rule ===

    @Transactional(readOnly = true)
    public List<ChargebackRuleVO> listChargebackRules(String companyCode) {
        return mapper.selectChargebackRules(companyCode);
    }

    @Transactional
    public Long createChargebackRule(ChargebackRuleVO vo) {
        mapper.insertChargebackRule(vo);
        return vo.getCbRuleId();
    }

    @Transactional
    public void updateChargebackRule(Long cbRuleId, ChargebackRuleVO vo) {
        vo.setCbRuleId(cbRuleId);
        mapper.updateChargebackRule(vo);
    }

    @Transactional
    public void deleteChargebackRule(Long cbRuleId) {
        mapper.deleteChargebackRule(cbRuleId);
    }

    // === exception_type_code ===

    @Transactional(readOnly = true)
    public List<ExceptionTypeCodeVO> listExceptionCodes() {
        return mapper.selectExceptionCodes();
    }

    @Transactional
    public void createExceptionCode(ExceptionTypeCodeVO vo) {
        if (mapper.selectExceptionCode(vo.getExceptionCode()) != null) {
            throw new BizException(ErrorCode.DUPLICATE_DATA);
        }
        mapper.insertExceptionCode(vo);
    }

    @Transactional
    public void updateExceptionCode(String exceptionCode, ExceptionTypeCodeVO vo) {
        vo.setExceptionCode(exceptionCode);
        mapper.updateExceptionCode(vo);
    }
}
