package com.ga.api.service.commission;

import com.ga.core.mapper.commission.LifeSupportLedgerMapper;
import com.ga.core.mapper.commission.LifeSupportRuleMapper;
import com.ga.core.vo.commission.LifeSupportLedgerSaveReq;
import com.ga.core.vo.commission.LifeSupportLedgerVO;
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 LifeSupportServiceTest {

    @Mock LifeSupportRuleMapper ruleMapper;
    @Mock LifeSupportLedgerMapper ledgerMapper;

    @InjectMocks LifeSupportService service;

    @Test
    @DisplayName("createLedger: support = base×rate, net = support - clawback (1,000,000×0.3=300,000, 300,000-20,000=280,000)")
    void createLedger_calculation() {
        LifeSupportLedgerSaveReq req = new LifeSupportLedgerSaveReq();
        req.setAgentId(1L);
        req.setSettleMonth("202506");
        req.setBaseAmount(new BigDecimal("1000000"));
        req.setSupportRate(new BigDecimal("0.3"));
        req.setClawbackAmount(new BigDecimal("20000"));

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

        service.createLedger(req);

        LifeSupportLedgerVO saved = captor.getValue();
        assertThat(saved.getSupportAmount()).isEqualByComparingTo("300000.00");
        assertThat(saved.getNetAmount()).isEqualByComparingTo("280000.00");
        assertThat(saved.getStatus()).isEqualTo("CALCULATED");
    }

    @Test
    @DisplayName("createLedger: clawback null이면 0으로 처리 → net = support")
    void createLedger_noClawback() {
        LifeSupportLedgerSaveReq req = new LifeSupportLedgerSaveReq();
        req.setAgentId(1L);
        req.setSettleMonth("202506");
        req.setBaseAmount(new BigDecimal("500000"));
        req.setSupportRate(new BigDecimal("0.2"));
        req.setClawbackAmount(null);

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

        service.createLedger(req);

        LifeSupportLedgerVO saved = captor.getValue();
        assertThat(saved.getSupportAmount()).isEqualByComparingTo("100000.00");
        assertThat(saved.getNetAmount()).isEqualByComparingTo("100000.00");
    }
}
