package at.procon.eventhub.yellowfox.service;

import at.procon.eventhub.config.EventHubProperties;
import at.procon.eventhub.dto.AcquisitionStrategy;
import at.procon.eventhub.dto.EventFamily;
import at.procon.eventhub.dto.EventSourceDto;
import at.procon.eventhub.dto.ImportCursorStateDto;
import at.procon.eventhub.dto.ImportMode;
import at.procon.eventhub.dto.ImportScopeDto;
import at.procon.eventhub.dto.SourceGroupRefDto;
import at.procon.eventhub.dto.SourceGroupType;
import at.procon.eventhub.importing.ImportPlanItemDto;
import at.procon.eventhub.importing.persistence.ImportCursorRepository;
import at.procon.eventhub.yellowfox.dto.YellowFoxD8ImportRequest;
import java.time.OffsetDateTime;
import java.util.EnumSet;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.core.io.DefaultResourceLoader;

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

class JdbcYellowFoxD8BookingExtractionBatchExecutorCursorTest {

    @Test
    void bootstrapsScheduledWatermarkCursorFromLatestExistingCursorWhenExactCursorIsMissing() {
        ImportCursorRepository repository = mock(ImportCursorRepository.class);
        JdbcYellowFoxD8BookingExtractionBatchExecutor executor = executor(repository);
        YellowFoxD8ImportRequest request = request();
        ImportPlanItemDto planItem = new ImportPlanItemDto(
                EventFamily.DRIVER_ACTIVITY,
                "TELEMATICS_PLATFORM",
                "YELLOWFOX_D8_BOOKING",
                List.of("data.d8_booking"),
                "BOTH",
                "YellowFox bookings",
                AcquisitionStrategy.SOURCE_ROW_WATERMARK
        );
        ImportCursorStateDto fallbackCursor = new ImportCursorStateDto(
                null,
                "4711",
                null,
                OffsetDateTime.parse("2026-04-09T23:59:59+02:00")
        );

        when(repository.findCursor(
                "tenant-1",
                17,
                request.importScope().stableKey(),
                EventFamily.DRIVER_ACTIVITY,
                "TELEMATICS_PLATFORM",
                AcquisitionStrategy.SOURCE_ROW_WATERMARK
        )).thenReturn(null);
        when(repository.findLatestCursor(
                "tenant-1",
                17,
                request.importScope().stableKey(),
                EventFamily.DRIVER_ACTIVITY,
                "TELEMATICS_PLATFORM"
        )).thenReturn(fallbackCursor);

        assertThat(executor.findCursor(17, request, planItem)).isEqualTo(fallbackCursor);

        verify(repository).findLatestCursor(
                "tenant-1",
                17,
                request.importScope().stableKey(),
                EventFamily.DRIVER_ACTIVITY,
                "TELEMATICS_PLATFORM"
        );
    }

    private JdbcYellowFoxD8BookingExtractionBatchExecutor executor(ImportCursorRepository repository) {
        EventHubProperties properties = new EventHubProperties();
        return new JdbcYellowFoxD8BookingExtractionBatchExecutor(
                null,
                null,
                null,
                new DefaultResourceLoader(),
                repository,
                properties,
                null,
                null,
                null
        );
    }

    private YellowFoxD8ImportRequest request() {
        return new YellowFoxD8ImportRequest(
                "tenant-1",
                new EventSourceDto("YELLOWFOX", "TELEMATICS_PLATFORM", "YELLOWFOX_D8", "instance-1", "setting-1", null),
                new SourceGroupRefDto(SourceGroupType.FLEET, "7", null, "Fleet 7"),
                ImportScopeDto.tenantAll(
                        OffsetDateTime.parse("2026-04-01T00:00:00+02:00"),
                        OffsetDateTime.parse("2026-04-10T00:00:00+02:00")
                ),
                EnumSet.of(EventFamily.DRIVER_ACTIVITY),
                ImportMode.INCREMENTAL_UPDATE,
                false,
                AcquisitionStrategy.SOURCE_ROW_WATERMARK
        );
    }
}
