package com.axelor.apps.account.web;

import org.junit.Before;
import org.junit.Test;

import com.axelor.apps.account.db.CashInventory;
import com.axelor.apps.base.service.ConvertNumberToFrenchWordsService;
import com.axelor.inject.Beans;
import com.axelor.rpc.ActionRequest;
import com.axelor.rpc.ActionResponse;
import static org.mockito.Mockito.*;
import org.mockito.ArgumentCaptor;
import static org.junit.Assert.assertEquals;
import java.math.BigDecimal;

public class CashInventoryControllerTest {

    private CashInventoryController controller;
    private ActionRequest request;
    private ActionResponse response;
    private CashInventory cashInventory;
    private ConvertNumberToFrenchWordsService convertService;

    @Before
    public void setUp() {
        request = mock(ActionRequest.class);
        response = mock(ActionResponse.class);
        cashInventory = mock(CashInventory.class);
        convertService = mock(ConvertNumberToFrenchWordsService.class);

        controller = new CashInventoryController(convertService); // ✅ No Guice needed

        // Mock request.getContext().asType(CashInventory.class)
        com.axelor.rpc.Context context = mock(com.axelor.rpc.Context.class);
        when(request.getContext()).thenReturn(context);
        when(context.asType(CashInventory.class)).thenReturn(cashInventory);
    }

    @Test
    public void testConvert_withDecimalSolde() {
        // Given
        when(cashInventory.getTheoricalSolde()).thenReturn(new BigDecimal("1234.56"));
        when(convertService.convert(1234L)).thenReturn("mille deux cent trente-quatre");
        when(convertService.convert(56L)).thenReturn("cinquante-six");

    }

    @Test
    public void testConvert_withIntegerSolde() {
        // Given
        when(cashInventory.getTheoricalSolde()).thenReturn(new BigDecimal("1000.00"));
        when(convertService.convert(1000L)).thenReturn("mille");
        when(convertService.convert(0L)).thenReturn("zéro");


        // Then
        ArgumentCaptor<String> keyCaptor = ArgumentCaptor.forClass(String.class);
        ArgumentCaptor<String> valueCaptor = ArgumentCaptor.forClass(String.class);
        verify(response).setValue(keyCaptor.capture(), valueCaptor.capture());
        assertEquals("theoricalSoldeInWords", keyCaptor.getValue());
        assertEquals("mille dinars algériens et zéro Cts", valueCaptor.getValue());
    }

    // @Test
    // public void testConvert_withNoDecimalPart() {
    //     // Given
    //     when(cashInventory.getTheoricalSolde()).thenReturn(new BigDecimal("42"));
    //     when(convertService.convert(42L)).thenReturn("quarante-deux");
    //     when(convertService.convert(0L)).thenReturn("zéro");


    //     // Then
    //     ArgumentCaptor<String> keyCaptor = ArgumentCaptor.forClass(String.class);
    //     ArgumentCaptor<String> valueCaptor = ArgumentCaptor.forClass(String.class);
    //     verify(response).setValue(keyCaptor.capture(), valueCaptor.capture());
    //     assertEquals("theoricalSoldeInWords", keyCaptor.getValue());
    //     assertEquals("quarante-deux dinars algériens et zéro Cts",
    //             valueCaptor.getValue());
    // }
}