package com.ga.api.service.commission;

import com.ga.core.mapper.commission.ReferralCommissionLedgerMapper;
import com.ga.core.mapper.commission.ReferralCommissionRuleMapper;
import com.ga.core.vo.commission.ReferralCommissionLedgerSaveReq;
import com.ga.core.vo.commission.ReferralCommissionLedgerVO;
import com.ga.core.vo.commission.ReferralCommissionRuleVO;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.math.BigDecimal;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class ReferralCommissionServiceTest {

    @Mock ReferralCommissionRuleMapper ruleMapper;
    @Mock ReferralCommissionLedgerMapper ledgerMapper;

    @InjectMocks ReferralCommissionService service;

    @Test
    @DisplayName("createLedger RATE: commission_amount = base × rate (500,000 × 0.1 = 50,000)")
    void createLedger_rate() {
        ReferralCommissionLedgerSaveReq req = new ReferralCommissionLedgerSaveReq();
        req.setContractId(1L);
        req.setReferrerAgentId(2L);
        req.setSettleMonth("202506");
        req.setReferralType("REFERRAL");
        req.setBaseAmount(new BigDecimal("500000"));
        req.setCommissionRate(new BigDecimal("0.1"));

        ArgumentCaptor<ReferralCommissionLedgerVO> captor = ArgumentCaptor.forClass(ReferralCommissionLedgerVO.class);
        when(ledgerMapper.insert(captor.capture())).thenReturn(1);

        service.createLedger(req);

        ReferralCommissionLedgerVO saved = captor.getValue();
        assertThat(saved.getCommissionAmount()).isEqualByComparingTo("50000.00");
        assertThat(saved.getStatus()).isEqualTo("CALCULATED");
    }

    @Test
    @DisplayName("createLedger FIXED: commission_amount = rule.fixedAmount (30,000)")
    void createLedger_fixed() {
        ReferralCommissionLedgerSaveReq req = new ReferralCommissionLedgerSaveReq();
        req.setContractId(1L);
        req.setReferrerAgentId(2L);
        req.setSettleMonth("202506");
        req.setReferralType("TRANSFER");
        req.setBaseAmount(new BigDecimal("500000"));
        req.setCommissionRate(null); // FIXED 분기

        ReferralCommissionRuleVO rule = new ReferralCommissionRuleVO();
        rule.setCalcType("FIXED");
        rule.setFixedAmount(new BigDecimal("30000"));
        when(ruleMapper.selectActiveRule(any(), any())).thenReturn(rule);

        ArgumentCaptor<ReferralCommissionLedgerVO> captor = ArgumentCaptor.forClass(ReferralCommissionLedgerVO.class);
        when(ledgerMapper.insert(captor.capture())).thenReturn(1);

        service.createLedger(req);

        ReferralCommissionLedgerVO saved = captor.getValue();
        assertThat(saved.getCommissionAmount()).isEqualByComparingTo("30000.00");
    }
}
