package io.delphiplatform.api.v3.bigtable;

import com.google.cloud.bigtable.data.v2.BigtableDataClient;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.time.LocalDate;
import java.util.List;
import java.util.concurrent.CompletableFuture;

import javax.transaction.NotSupportedException;

import io.delphiplatform.api.exception.InvalidParameterException;
import io.delphiplatform.api.v3.bigtable.processing.PaginationService;
import io.delphiplatform.api.v3.bigtable.processing.StreamsAggregator;
import io.delphiplatform.api.v3.bigtable.processing.StreamsConverter;
import io.delphiplatform.api.v3.bigtable.processing.StreamsSortingService;
import io.delphiplatform.api.v3.constant.DspConstants;
import io.delphiplatform.api.v3.view.util.Params;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class BigtableStreamsServiceTest {

    private BigtableStreamsService service;

    @Mock
    private BigtableQueryExecutor queryExecutor;

    @Mock
    private BigtableRowReader rowReader;
    @Mock
    private StreamsConverter streamsConverter;
    @Mock
    private StreamsAggregator streamsAggregator;
    @Mock
    private StreamsSortingService streamsSortingService;
    @Mock
    private PaginationService paginationService;

    private BigtableDataClient client = mock(BigtableDataClient.class);

    @BeforeEach
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        service = new BigtableStreamsService(queryExecutor, rowReader, streamsConverter, streamsAggregator,
            streamsSortingService, paginationService);
    }

    @Test
    void getRows_checkForDspFailed() {
        Params params = Params.builder().dsp(List.of(DspConstants.AMAZON, "aaa", "bbb")).build();

        Throwable exception = Assertions
            .assertThrows(InvalidParameterException.class, () -> service.getRows(params));

        Assertions.assertEquals(
            "dsp should be at least one from [[amazon, apple, spotify]] : unexpected value 'aaa,bbb'",
            exception.getMessage());

    }

    @Test
    void getRows_checkForDspPassed() throws NotSupportedException {
        when(queryExecutor.executePartitioned(any(), any(), any()))
            .thenReturn(CompletableFuture.completedFuture(null));

        Params params = Params.builder()
            .startDate(LocalDate.of(2021, 01, 01))
            .endDate(LocalDate.of(2021, 01, 02))
            .artistId("1")
            .dsp(List.of(DspConstants.AMAZON, DspConstants.APPLE)).build();

        service.getRows(params);

    }

}
