package at.procon.eventhub.yellowfox.service;

import at.procon.eventhub.config.EventHubProperties;
import at.procon.eventhub.dto.DataPackageType;
import at.procon.eventhub.dto.EventHubEventBatchDto;
import at.procon.eventhub.dto.EventHubPackageRequest;
import at.procon.eventhub.dto.EventHubPackageResult;
import at.procon.eventhub.dto.EventSourceDto;
import at.procon.eventhub.dto.ImportScopeDto;
import at.procon.eventhub.service.EventHubIngestionService;
import java.time.OffsetDateTime;
import java.util.List;
import org.apache.camel.ProducerTemplate;
import org.junit.jupiter.api.Test;
import org.springframework.core.io.DefaultResourceLoader;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

class JdbcYellowFoxD8BookingExtractionBatchExecutorIngestModeTest {

    @Test
    void usesDirectIngestionServiceInSyncDirectMode() {
        EventHubIngestionService ingestionService = mock(EventHubIngestionService.class);
        ProducerTemplate producerTemplate = mock(ProducerTemplate.class);
        JdbcYellowFoxD8BookingExtractionBatchExecutor executor = executor(
                EventHubProperties.JdbcExtractionIngestMode.SYNC_DIRECT,
                ingestionService,
                producerTemplate
        );
        EventHubEventBatchDto batch = batch();
        EventHubPackageResult result = new EventHubPackageResult(null, batch.packageKey(), 2, 2);

        when(ingestionService.ingest(batch)).thenReturn(result);

        assertThat(executor.persistBatch(batch)).isEqualTo(result);

        verify(ingestionService).ingest(batch);
    }

    @Test
    void usesCamelBatchPersistRouteInCamelMode() {
        EventHubIngestionService ingestionService = mock(EventHubIngestionService.class);
        ProducerTemplate producerTemplate = mock(ProducerTemplate.class);
        JdbcYellowFoxD8BookingExtractionBatchExecutor executor = executor(
                EventHubProperties.JdbcExtractionIngestMode.CAMEL_ROUTE,
                ingestionService,
                producerTemplate
        );
        EventHubEventBatchDto batch = batch();
        EventHubPackageResult result = new EventHubPackageResult(null, batch.packageKey(), 2, 2);

        when(producerTemplate.requestBody(
                eq("direct:eventhub-batch-persist-input"),
                eq(batch),
                eq(EventHubPackageResult.class)
        )).thenReturn(result);

        assertThat(executor.persistBatch(batch)).isEqualTo(result);

        verify(producerTemplate).requestBody(
                "direct:eventhub-batch-persist-input",
                batch,
                EventHubPackageResult.class
        );
    }

    private JdbcYellowFoxD8BookingExtractionBatchExecutor executor(
            EventHubProperties.JdbcExtractionIngestMode ingestMode,
            EventHubIngestionService ingestionService,
            ProducerTemplate producerTemplate
    ) {
        EventHubProperties properties = new EventHubProperties();
        properties.getYellowFox().setJdbcExtractionIngestMode(ingestMode);
        return new JdbcYellowFoxD8BookingExtractionBatchExecutor(
                null,
                ingestionService,
                producerTemplate,
                new DefaultResourceLoader(),
                null,
                properties,
                null,
                null,
                null
        );
    }

    private EventHubEventBatchDto batch() {
        return new EventHubEventBatchDto(
                "pkg-1",
                new EventHubPackageRequest(
                        "tenant-1",
                        new EventSourceDto("YELLOWFOX", "TELEMATICS_PLATFORM", "YELLOWFOX_D8", "instance-1", "setting-1", "7"),
                        null,
                        ImportScopeDto.tenantAll(
                                OffsetDateTime.parse("2026-04-01T00:00:00+02:00"),
                                OffsetDateTime.parse("2026-04-02T00:00:00+02:00")
                        ),
                        "DRIVER_ACTIVITY",
                        null,
                        "external-1"
                ),
                DataPackageType.DB_EXTRACT,
                null,
                null,
                List.of(),
                null
        );
    }
}
