package com.ga.api.service.commission;

import com.ga.core.mapper.commission.BranchMgrAllowanceLedgerMapper;
import com.ga.core.mapper.commission.BranchMgrAllowanceRuleMapper;
import com.ga.core.vo.commission.BranchMgrAllowanceLedgerSaveReq;
import com.ga.core.vo.commission.BranchMgrAllowanceLedgerVO;
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.Mockito.when;

@ExtendWith(MockitoExtension.class)
class BranchAllowanceServiceTest {

    @Mock BranchMgrAllowanceRuleMapper ruleMapper;
    @Mock BranchMgrAllowanceLedgerMapper ledgerMapper;

    @InjectMocks BranchAllowanceService service;

    @Test
    @DisplayName("createLedger: allowance = base × allowance_rate (2,000,000 × 0.05 = 100,000)")
    void createLedger_allowanceAmount() {
        BranchMgrAllowanceLedgerSaveReq req = new BranchMgrAllowanceLedgerSaveReq();
        req.setManagerAgentId(1L);
        req.setOrgId(10L);
        req.setSettleMonth("202506");
        req.setBaseAmount(new BigDecimal("2000000"));
        req.setAllowanceRate(new BigDecimal("0.05"));
        req.setClawbackAmount(null);

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

        service.createLedger(req);

        BranchMgrAllowanceLedgerVO saved = captor.getValue();
        assertThat(saved.getAllowanceAmount()).isEqualByComparingTo("100000.00");
        assertThat(saved.getStatus()).isEqualTo("CALCULATED");
    }
}
