package com.ga.api.service.commission;

import com.ga.common.exception.BizException;
import com.ga.common.exception.ErrorCode;
import com.ga.common.model.PageResponse;
import com.ga.core.mapper.commission.RenewalCommissionLedgerMapper;
import com.ga.core.mapper.commission.RenewalCommissionRuleMapper;
import com.ga.core.vo.commission.*;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class RenewalCommissionService {

    private final RenewalCommissionRuleMapper ruleMapper;
    private final RenewalCommissionLedgerMapper ledgerMapper;

    // ── Rule ──────────────────────────────────────────────────────────────────

    public PageResponse<RenewalCommissionRuleResp> listRules(RenewalCommissionRuleSearchParam param) {
        param.startPage();
        return PageResponse.of(ruleMapper.selectList(param));
    }

    public RenewalCommissionRuleResp detailRule(Long ruleId) {
        RenewalCommissionRuleResp resp = ruleMapper.selectById(ruleId);
        if (resp == null) throw new BizException(ErrorCode.NOT_FOUND);
        return resp;
    }

    @Transactional
    public Long createRule(RenewalCommissionRuleSaveReq req) {
        RenewalCommissionRuleVO vo = new RenewalCommissionRuleVO();
        vo.setProductId(req.getProductId());
        vo.setPayType(req.getPayType());
        vo.setRateValue(req.getRateValue());
        vo.setFixedAmount(req.getFixedAmount());
        vo.setEffectiveFrom(req.getEffectiveFrom());
        vo.setEffectiveTo(req.getEffectiveTo());
        ruleMapper.insert(vo);
        return vo.getRuleId();
    }

    @Transactional
    public void updateRule(Long ruleId, RenewalCommissionRuleSaveReq req) {
        RenewalCommissionRuleResp resp = ruleMapper.selectById(ruleId);
        if (resp == null) throw new BizException(ErrorCode.NOT_FOUND);
        RenewalCommissionRuleVO vo = new RenewalCommissionRuleVO();
        vo.setRuleId(ruleId);
        vo.setProductId(req.getProductId());
        vo.setPayType(req.getPayType());
        vo.setRateValue(req.getRateValue());
        vo.setFixedAmount(req.getFixedAmount());
        vo.setEffectiveFrom(req.getEffectiveFrom());
        vo.setEffectiveTo(req.getEffectiveTo());
        ruleMapper.update(vo);
    }

    @Transactional
    public void deleteRule(Long ruleId) {
        RenewalCommissionRuleResp resp = ruleMapper.selectById(ruleId);
        if (resp == null) throw new BizException(ErrorCode.NOT_FOUND);
        ruleMapper.delete(ruleId);
    }

    // ── Ledger ────────────────────────────────────────────────────────────────

    public PageResponse<RenewalCommissionLedgerResp> listLedgers(RenewalCommissionLedgerSearchParam param) {
        param.startPage();
        return PageResponse.of(ledgerMapper.selectList(param));
    }

    public RenewalCommissionLedgerResp detailLedger(Long ledgerId) {
        RenewalCommissionLedgerResp resp = ledgerMapper.selectById(ledgerId);
        if (resp == null) throw new BizException(ErrorCode.NOT_FOUND);
        return resp;
    }

    @Transactional
    public Long createLedger(RenewalCommissionLedgerSaveReq req) {
        RenewalCommissionLedgerVO vo = new RenewalCommissionLedgerVO();
        vo.setContractId(req.getContractId());
        vo.setAgentId(req.getAgentId());
        vo.setRuleId(req.getRuleId());
        vo.setSettleMonth(req.getSettleMonth());
        vo.setRenewalDate(req.getRenewalDate());
        vo.setRenewalPremium(req.getRenewalPremium());
        vo.setCommissionAmount(req.getCommissionAmount());
        vo.setPayType(req.getPayType());
        vo.setStatus("CALCULATED");
        ledgerMapper.insert(vo);
        return vo.getLedgerId();
    }
}
