package com.ga.api.service.commission;

import com.ga.core.mapper.commission.ProductConversionRateMapper;
import com.ga.core.mapper.commission.RecognizedPerformanceLedgerMapper;
import com.ga.core.vo.commission.RecognizedPerformanceLedgerSaveReq;
import com.ga.core.vo.commission.RecognizedPerformanceLedgerVO;
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 RecognizedPerformanceServiceTest {

    @Mock ProductConversionRateMapper conversionRateMapper;
    @Mock RecognizedPerformanceLedgerMapper ledgerMapper;

    @InjectMocks RecognizedPerformanceService service;

    @Test
    @DisplayName("createLedger: converted=premium×conversion_rate, recognized=converted×recognition_rate (1,000,000×0.5×0.8=400,000)")
    void createLedger_twoStepCalc() {
        RecognizedPerformanceLedgerSaveReq req = new RecognizedPerformanceLedgerSaveReq();
        req.setAgentId(1L);
        req.setContractId(2L);
        req.setSettleMonth("202506");
        req.setPremiumAmount(new BigDecimal("1000000"));
        req.setConversionRate(new BigDecimal("0.5"));
        req.setRecognitionRate(new BigDecimal("0.8"));

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

        service.createLedger(req);

        RecognizedPerformanceLedgerVO saved = captor.getValue();
        assertThat(saved.getConvertedPremium()).isEqualByComparingTo("500000.00");
        assertThat(saved.getRecognizedAmount()).isEqualByComparingTo("400000.00");
        assertThat(saved.getStatus()).isEqualTo("CALCULATED");
    }
}
