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.PersistencyBonusMapper;
import com.ga.core.mapper.commission.PersistencyBonusRuleMapper;
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 PersistencyBonusService {

    private final PersistencyBonusRuleMapper ruleMapper;
    private final PersistencyBonusMapper bonusMapper;

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

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

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

    @Transactional
    public Long createRule(PersistencyBonusRuleSaveReq req) {
        PersistencyBonusRuleVO vo = new PersistencyBonusRuleVO();
        vo.setGradeId(req.getGradeId());
        vo.setRetentionMonth(req.getRetentionMonth());
        vo.setRetentionRateThreshold(req.getRetentionRateThreshold());
        vo.setBonusType(req.getBonusType());
        vo.setBonusValue(req.getBonusValue());
        vo.setEffectiveFrom(req.getEffectiveFrom());
        vo.setEffectiveTo(req.getEffectiveTo());
        ruleMapper.insert(vo);
        return vo.getRuleId();
    }

    @Transactional
    public void updateRule(Long ruleId, PersistencyBonusRuleSaveReq req) {
        PersistencyBonusRuleResp resp = ruleMapper.selectById(ruleId);
        if (resp == null) throw new BizException(ErrorCode.NOT_FOUND);
        PersistencyBonusRuleVO vo = new PersistencyBonusRuleVO();
        vo.setRuleId(ruleId);
        vo.setGradeId(req.getGradeId());
        vo.setRetentionMonth(req.getRetentionMonth());
        vo.setRetentionRateThreshold(req.getRetentionRateThreshold());
        vo.setBonusType(req.getBonusType());
        vo.setBonusValue(req.getBonusValue());
        vo.setEffectiveFrom(req.getEffectiveFrom());
        vo.setEffectiveTo(req.getEffectiveTo());
        ruleMapper.update(vo);
    }

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

    // ── Bonus ─────────────────────────────────────────────────────────────────

    public PageResponse<PersistencyBonusResp> listBonuses(PersistencyBonusSearchParam param) {
        param.startPage();
        return PageResponse.of(bonusMapper.selectList(param));
    }

    public PersistencyBonusResp detailBonus(Long bonusId) {
        PersistencyBonusResp resp = bonusMapper.selectById(bonusId);
        if (resp == null) throw new BizException(ErrorCode.NOT_FOUND);
        return resp;
    }

    /**
     * 수동 지급 등록/갱신 — UNIQUE(agent_id, base_month, retention_month) 기반 upsert.
     */
    @Transactional
    public void upsertBonus(PersistencyBonusSaveReq req) {
        PersistencyBonusVO vo = new PersistencyBonusVO();
        vo.setAgentId(req.getAgentId());
        vo.setRuleId(req.getRuleId());
        vo.setBaseMonth(req.getBaseMonth());
        vo.setRetentionMonth(req.getRetentionMonth());
        vo.setSettleMonth(req.getSettleMonth());
        vo.setRetentionRate(req.getRetentionRate());
        vo.setBonusAmount(req.getBonusAmount());
        vo.setStatus("CALCULATED");
        bonusMapper.upsert(vo);
    }
}
