package at.procon.eventhub.yellowfox.dto;

import at.procon.eventhub.dto.AcquisitionStrategy;
import at.procon.eventhub.dto.EventFamily;
import at.procon.eventhub.dto.EventSourceDto;
import at.procon.eventhub.dto.ImportMode;
import at.procon.eventhub.dto.ImportScopeDto;
import at.procon.eventhub.dto.SourceGroupRefDto;
import at.procon.eventhub.importing.ImportRunRequest;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import java.util.EnumSet;
import java.util.Set;

public record YellowFoxD8ImportRequest(
        @NotBlank String tenantKey,
        @Valid @NotNull EventSourceDto eventSource,
        @Valid SourceGroupRefDto sourceGroup,
        @Valid @NotNull ImportScopeDto importScope,
        Set<EventFamily> eventFamilies,
        ImportMode mode,
        boolean refreshMasterDataFirst,
        AcquisitionStrategy acquisitionStrategy
) implements ImportRunRequest {
    public YellowFoxD8ImportRequest {
        tenantKey = tenantKey == null ? null : tenantKey.trim();
        if (eventSource == null) {
            eventSource = new EventSourceDto("YELLOWFOX", "TELEMATICS_PLATFORM", "YELLOWFOX_D8", null, null, null);
        }
        if (importScope == null) {
            importScope = ImportScopeDto.tenantAll(null, null);
        }
        if (eventFamilies == null || eventFamilies.isEmpty()) {
            eventFamilies = EnumSet.of(EventFamily.DRIVER_ACTIVITY, EventFamily.DRIVER_CARD);
        } else {
            eventFamilies = EnumSet.copyOf(eventFamilies);
        }
        if (mode == null) {
            mode = ImportMode.INITIAL_BACKFILL;
        }
        if (acquisitionStrategy == null || acquisitionStrategy == AcquisitionStrategy.SOURCE_PACKAGE_WATERMARK) {
            acquisitionStrategy = mode == ImportMode.INCREMENTAL_UPDATE
                    ? AcquisitionStrategy.SOURCE_ROW_WATERMARK
                    : AcquisitionStrategy.OCCURRED_AT_WINDOW_WITH_OVERLAP;
        }
    }
}
