package at.procon.eventhub.processing.eventprocessing.validation;

import java.time.OffsetDateTime;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

public record RuntimeTachographParityValidationApiRequest(
        UUID sessionId,
        List<UUID> sessionIds,
        UUID compositeSessionId,
        String driverKey,
        Set<String> driverKeys,
        Boolean includeAllDrivers,
        OffsetDateTime occurredFrom,
        OffsetDateTime occurredTo,
        Boolean expandVehicleEvents,
        Integer vehicleExpansionPaddingMinutes,
        Integer significantDrivingMinutes,
        Integer minimumRestPeriodMinutes,
        Boolean includeDebug
) {
    public RuntimeTachographParityValidationApiRequest {
        sessionIds = sessionIds == null ? List.of() : List.copyOf(sessionIds);
        driverKeys = driverKeys == null ? Set.of() : Set.copyOf(driverKeys);
        vehicleExpansionPaddingMinutes = vehicleExpansionPaddingMinutes == null
                ? null
                : Math.max(0, vehicleExpansionPaddingMinutes);
        significantDrivingMinutes = significantDrivingMinutes == null ? null : Math.max(1, significantDrivingMinutes);
        minimumRestPeriodMinutes = minimumRestPeriodMinutes == null ? null : Math.max(1, minimumRestPeriodMinutes);
    }

    public Set<String> requestedDriverKeys() {
        LinkedHashSet<String> result = new LinkedHashSet<>();
        if (driverKey != null && !driverKey.isBlank()) {
            result.add(driverKey.trim());
        }
        driverKeys.stream()
                .filter(value -> value != null && !value.isBlank())
                .map(String::trim)
                .forEach(result::add);
        return Set.copyOf(result);
    }

    public boolean includeAllDriversOrDefault() {
        return includeAllDrivers == null ? requestedDriverKeys().isEmpty() : includeAllDrivers;
    }

    public boolean expandVehicleEventsOrDefault() {
        return expandVehicleEvents == null || expandVehicleEvents;
    }

    public boolean includeDebugOrDefault() {
        return includeDebug != null && includeDebug;
    }

    public int vehicleExpansionPaddingMinutesOrDefault() {
        return vehicleExpansionPaddingMinutes == null ? 0 : Math.max(0, vehicleExpansionPaddingMinutes);
    }
}
