package at.procon.eventhub.tachograph.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;

/**
 * Request for acquiring tachograph DB data into EventHub. This request describes
 * the import job/scope; individual SQL extraction routes then create acquisition
 * packages and EventHubEventDto records.
 */
public record TachographImportRequest(
        @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 TachographImportRequest {
        tenantKey = tenantKey == null ? null : tenantKey.trim();
        if (importScope == null) {
            importScope = ImportScopeDto.tenantAll(null, null);
        }
        if (eventFamilies == null || eventFamilies.isEmpty()) {
            eventFamilies = EnumSet.allOf(EventFamily.class);
        } else {
            eventFamilies = EnumSet.copyOf(eventFamilies);
        }
        if (mode == null) {
            mode = ImportMode.INITIAL_BACKFILL;
        }
        if (acquisitionStrategy == null) {
            acquisitionStrategy = mode == ImportMode.INCREMENTAL_UPDATE
                    ? AcquisitionStrategy.SOURCE_PACKAGE_WATERMARK
                    : AcquisitionStrategy.OCCURRED_AT_WINDOW_WITH_OVERLAP;
        }
    }
}
