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.CollectionCommissionLedgerMapper;
import com.ga.core.mapper.commission.CollectionCommissionRuleMapper;
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 CollectionCommissionService {

    private final CollectionCommissionRuleMapper ruleMapper;
    private final CollectionCommissionLedgerMapper ledgerMapper;

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

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

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

    @Transactional
    public Long createRule(CollectionCommissionRuleSaveReq req) {
        CollectionCommissionRuleVO vo = new CollectionCommissionRuleVO();
        vo.setProductId(req.getProductId());
        vo.setPaymentCycle(req.getPaymentCycle());
        vo.setCommissionRate(req.getCommissionRate());
        vo.setEffectiveFrom(req.getEffectiveFrom());
        vo.setEffectiveTo(req.getEffectiveTo());
        ruleMapper.insert(vo);
        return vo.getRuleId();
    }

    @Transactional
    public void updateRule(Long ruleId, CollectionCommissionRuleSaveReq req) {
        CollectionCommissionRuleResp resp = ruleMapper.selectById(ruleId);
        if (resp == null) throw new BizException(ErrorCode.NOT_FOUND);
        CollectionCommissionRuleVO vo = new CollectionCommissionRuleVO();
        vo.setRuleId(ruleId);
        vo.setProductId(req.getProductId());
        vo.setPaymentCycle(req.getPaymentCycle());
        vo.setCommissionRate(req.getCommissionRate());
        vo.setEffectiveFrom(req.getEffectiveFrom());
        vo.setEffectiveTo(req.getEffectiveTo());
        ruleMapper.update(vo);
    }

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

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

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

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

    @Transactional
    public Long createLedger(CollectionCommissionLedgerSaveReq req) {
        CollectionCommissionLedgerVO vo = new CollectionCommissionLedgerVO();
        vo.setContractId(req.getContractId());
        vo.setAgentId(req.getAgentId());
        vo.setSettleMonth(req.getSettleMonth());
        vo.setPremiumAmount(req.getPremiumAmount());
        vo.setCommissionRate(req.getCommissionRate());
        // 수금수수료 = 보험료 × 수수료율
        vo.setCommissionAmount(req.getPremiumAmount().multiply(req.getCommissionRate()));
        vo.setPaymentCycle(req.getPaymentCycle());
        vo.setPaidAt(req.getPaidAt());
        vo.setStatus("CALCULATED");
        ledgerMapper.insert(vo);
        return vo.getLedgerId();
    }
}
