package at.procon.eventhub.tachographfilesession.dto;

import at.procon.eventhub.processing.driverworkingtime.dto.DriverWorkingTimeProcessingResultDto;
import at.procon.eventhub.processing.driverworkingtime.model.DriverWorkingTimeActivityInterval;
import at.procon.eventhub.processing.driverworkingtime.model.DriverWorkingTimeDrivingInterruptionInterval;
import at.procon.eventhub.processing.driverworkingtime.model.DriverWorkingTimeGeoEvidenceEvent;
import at.procon.eventhub.processing.driverworkingtime.model.DriverWorkingTimePotentialInVehicleTripInterval;
import at.procon.eventhub.processing.driverworkingtime.model.DriverWorkingTimeRestCoverageInterval;
import at.procon.eventhub.processing.driverworkingtime.model.DriverWorkingTimeSupportGeoEvent;
import at.procon.eventhub.processing.driverworkingtime.model.DriverWorkingTimeVehicleUsageInterval;
import at.procon.eventhub.processing.driverworkingtime.model.DriverWorkingTimeVuCardAbsentInterval;
import at.procon.eventhub.tachographfilesession.model.TachographEsperActivityIntervalEvent;
import at.procon.eventhub.tachographfilesession.model.TachographEsperDailyWeeklyRestCandidateCoverageIntervalEvent;
import at.procon.eventhub.tachographfilesession.model.TachographEsperDrivingInterruptionIntervalEvent;
import at.procon.eventhub.tachographfilesession.model.TachographEsperGeoEvidenceEvent;
import at.procon.eventhub.tachographfilesession.model.TachographEsperPotentialHomeOvernightStayIntervalEvent;
import at.procon.eventhub.tachographfilesession.model.TachographEsperPotentialInVehicleOvernightStayIntervalEvent;
import at.procon.eventhub.tachographfilesession.model.TachographEsperPotentialInVehicleTripIntervalEvent;
import at.procon.eventhub.tachographfilesession.model.TachographEsperSupportGeoEvent;
import at.procon.eventhub.tachographfilesession.model.TachographEsperVehicleUsageIntervalEvent;
import at.procon.eventhub.tachographfilesession.model.TachographEsperVuCardAbsentIntervalEvent;
import java.time.OffsetDateTime;
import java.util.List;
import java.util.UUID;

@Deprecated(forRemoval = false)
public record TachographEsperDriverProcessingResultDto(
        UUID sessionId,
        String driverKey,
        String sourceKind,
        OffsetDateTime loadedFrom,
        OffsetDateTime loadedTo,
        OffsetDateTime requestedFrom,
        OffsetDateTime requestedTo,
        int activityIntervalCount,
        int drivingIntervalCount,
        int drivingInterruptionIntervalCount,
        int drivingInterruptionVehicleChangeIntervalCount,
        int dailyWeeklyRestCandidateIntervalCount,
        int dailyWeeklyRestCandidateCoverageIntervalCount,
        int unclassifiedDailyWeeklyRestCandidateCoverageIntervalCount,
        int potentialHomeOvernightStayIntervalCount,
        int potentialInVehicleOvernightStayIntervalCount,
        int potentialInVehicleTripIntervalCount,
        int vehicleUsageIntervalCount,
        int vuCardAbsentIntervalCount,
        int supportGeoEventCount,
        List<TachographEsperActivityIntervalEvent> activityIntervals,
        List<TachographEsperActivityIntervalEvent> drivingIntervals,
        List<TachographEsperDrivingInterruptionIntervalEvent> drivingInterruptionIntervals,
        List<TachographEsperDrivingInterruptionIntervalEvent> drivingInterruptionVehicleChangeIntervals,
        List<TachographEsperDrivingInterruptionIntervalEvent> dailyWeeklyRestCandidateIntervals,
        List<TachographEsperDailyWeeklyRestCandidateCoverageIntervalEvent> dailyWeeklyRestCandidateCoverageIntervals,
        List<TachographEsperDailyWeeklyRestCandidateCoverageIntervalEvent> unclassifiedDailyWeeklyRestCandidateCoverageIntervals,
        List<TachographEsperPotentialHomeOvernightStayIntervalEvent> potentialHomeOvernightStayIntervals,
        List<TachographEsperPotentialInVehicleOvernightStayIntervalEvent> potentialInVehicleOvernightStayIntervals,
        List<TachographEsperPotentialInVehicleTripIntervalEvent> potentialInVehicleTripIntervals,
        List<TachographEsperVehicleUsageIntervalEvent> vehicleUsageIntervals,
        List<TachographEsperVuCardAbsentIntervalEvent> vuCardAbsentIntervals,
        List<TachographEsperSupportGeoEvent> supportGeoEvents,
        List<String> notes
) {
    public static TachographEsperDriverProcessingResultDto fromDriverWorkingTime(
            DriverWorkingTimeProcessingResultDto result
    ) {
        if (result == null) {
            return null;
        }
        return new TachographEsperDriverProcessingResultDto(
                result.sessionId(),
                result.driverKey(),
                result.sourceKind(),
                result.loadedFrom(),
                result.loadedTo(),
                result.requestedFrom(),
                result.requestedTo(),
                result.activityIntervalCount(),
                result.drivingIntervalCount(),
                result.drivingInterruptionIntervalCount(),
                result.drivingInterruptionVehicleChangeIntervalCount(),
                result.dailyWeeklyRestCandidateIntervalCount(),
                result.dailyWeeklyRestCandidateCoverageIntervalCount(),
                result.unclassifiedDailyWeeklyRestCandidateCoverageIntervalCount(),
                result.potentialHomeOvernightStayIntervalCount(),
                result.potentialInVehicleOvernightStayIntervalCount(),
                result.potentialInVehicleTripIntervalCount(),
                result.vehicleUsageIntervalCount(),
                result.vuCardAbsentIntervalCount(),
                result.supportGeoEventCount(),
                mapActivityIntervalsToLegacy(result.activityIntervals()),
                mapActivityIntervalsToLegacy(result.drivingIntervals()),
                mapDrivingInterruptionsToLegacy(result.drivingInterruptionIntervals()),
                mapDrivingInterruptionsToLegacy(result.drivingInterruptionVehicleChangeIntervals()),
                mapDrivingInterruptionsToLegacy(result.dailyWeeklyRestCandidateIntervals()),
                mapCoverageIntervalsToLegacy(result.dailyWeeklyRestCandidateCoverageIntervals()),
                mapCoverageIntervalsToLegacy(result.unclassifiedDailyWeeklyRestCandidateCoverageIntervals()),
                mapPotentialHomeIntervalsToLegacy(result.potentialHomeOvernightStayIntervals()),
                mapPotentialInVehicleIntervalsToLegacy(result.potentialInVehicleOvernightStayIntervals()),
                mapPotentialTripsToLegacy(result.potentialInVehicleTripIntervals()),
                mapVehicleUsageIntervalsToLegacy(result.vehicleUsageIntervals()),
                mapVuCardAbsentIntervalsToLegacy(result.vuCardAbsentIntervals()),
                mapSupportGeoEventsToLegacy(result.supportGeoEvents()),
                result.notes()
        );
    }

    public DriverWorkingTimeProcessingResultDto toDriverWorkingTime() {
        return new DriverWorkingTimeProcessingResultDto(
                sessionId,
                driverKey,
                sourceKind,
                loadedFrom,
                loadedTo,
                requestedFrom,
                requestedTo,
                activityIntervalCount,
                drivingIntervalCount,
                drivingInterruptionIntervalCount,
                drivingInterruptionVehicleChangeIntervalCount,
                dailyWeeklyRestCandidateIntervalCount,
                dailyWeeklyRestCandidateCoverageIntervalCount,
                unclassifiedDailyWeeklyRestCandidateCoverageIntervalCount,
                potentialHomeOvernightStayIntervalCount,
                potentialInVehicleOvernightStayIntervalCount,
                potentialInVehicleTripIntervalCount,
                vehicleUsageIntervalCount,
                vuCardAbsentIntervalCount,
                supportGeoEventCount,
                mapActivityIntervalsFromLegacy(activityIntervals),
                mapActivityIntervalsFromLegacy(drivingIntervals),
                mapDrivingInterruptionsFromLegacy(drivingInterruptionIntervals),
                mapDrivingInterruptionsFromLegacy(drivingInterruptionVehicleChangeIntervals),
                mapDrivingInterruptionsFromLegacy(dailyWeeklyRestCandidateIntervals),
                mapCoverageIntervalsFromLegacy(dailyWeeklyRestCandidateCoverageIntervals),
                mapCoverageIntervalsFromLegacy(unclassifiedDailyWeeklyRestCandidateCoverageIntervals),
                mapPotentialHomeIntervalsFromLegacy(potentialHomeOvernightStayIntervals),
                mapPotentialInVehicleIntervalsFromLegacy(potentialInVehicleOvernightStayIntervals),
                mapPotentialTripsFromLegacy(potentialInVehicleTripIntervals),
                mapVehicleUsageIntervalsFromLegacy(vehicleUsageIntervals),
                mapVuCardAbsentIntervalsFromLegacy(vuCardAbsentIntervals),
                mapSupportGeoEventsFromLegacy(supportGeoEvents),
                notes
        );
    }

    private static List<TachographEsperActivityIntervalEvent> mapActivityIntervalsToLegacy(
            List<DriverWorkingTimeActivityInterval> values
    ) {
        return safe(values).stream().map(TachographEsperDriverProcessingResultDto::toLegacy).toList();
    }

    private static List<DriverWorkingTimeActivityInterval> mapActivityIntervalsFromLegacy(
            List<TachographEsperActivityIntervalEvent> values
    ) {
        return safe(values).stream().map(TachographEsperDriverProcessingResultDto::toCommon).toList();
    }

    private static List<TachographEsperDrivingInterruptionIntervalEvent> mapDrivingInterruptionsToLegacy(
            List<DriverWorkingTimeDrivingInterruptionInterval> values
    ) {
        return safe(values).stream().map(TachographEsperDriverProcessingResultDto::toLegacy).toList();
    }

    private static List<DriverWorkingTimeDrivingInterruptionInterval> mapDrivingInterruptionsFromLegacy(
            List<TachographEsperDrivingInterruptionIntervalEvent> values
    ) {
        return safe(values).stream().map(TachographEsperDriverProcessingResultDto::toCommon).toList();
    }

    private static List<TachographEsperDailyWeeklyRestCandidateCoverageIntervalEvent> mapCoverageIntervalsToLegacy(
            List<DriverWorkingTimeRestCoverageInterval> values
    ) {
        return safe(values).stream().map(TachographEsperDriverProcessingResultDto::toLegacyCoverage).toList();
    }

    private static List<DriverWorkingTimeRestCoverageInterval> mapCoverageIntervalsFromLegacy(
            List<TachographEsperDailyWeeklyRestCandidateCoverageIntervalEvent> values
    ) {
        return safe(values).stream().map(TachographEsperDriverProcessingResultDto::toCommon).toList();
    }

    private static List<TachographEsperPotentialHomeOvernightStayIntervalEvent> mapPotentialHomeIntervalsToLegacy(
            List<DriverWorkingTimeRestCoverageInterval> values
    ) {
        return safe(values).stream().map(TachographEsperDriverProcessingResultDto::toLegacyPotentialHome).toList();
    }

    private static List<DriverWorkingTimeRestCoverageInterval> mapPotentialHomeIntervalsFromLegacy(
            List<TachographEsperPotentialHomeOvernightStayIntervalEvent> values
    ) {
        return safe(values).stream().map(TachographEsperDriverProcessingResultDto::toCommon).toList();
    }

    private static List<TachographEsperPotentialInVehicleOvernightStayIntervalEvent> mapPotentialInVehicleIntervalsToLegacy(
            List<DriverWorkingTimeRestCoverageInterval> values
    ) {
        return safe(values).stream().map(TachographEsperDriverProcessingResultDto::toLegacyPotentialInVehicle).toList();
    }

    private static List<DriverWorkingTimeRestCoverageInterval> mapPotentialInVehicleIntervalsFromLegacy(
            List<TachographEsperPotentialInVehicleOvernightStayIntervalEvent> values
    ) {
        return safe(values).stream().map(TachographEsperDriverProcessingResultDto::toCommon).toList();
    }

    private static List<TachographEsperPotentialInVehicleTripIntervalEvent> mapPotentialTripsToLegacy(
            List<DriverWorkingTimePotentialInVehicleTripInterval> values
    ) {
        return safe(values).stream().map(TachographEsperDriverProcessingResultDto::toLegacy).toList();
    }

    private static List<DriverWorkingTimePotentialInVehicleTripInterval> mapPotentialTripsFromLegacy(
            List<TachographEsperPotentialInVehicleTripIntervalEvent> values
    ) {
        return safe(values).stream().map(TachographEsperDriverProcessingResultDto::toCommon).toList();
    }

    private static List<TachographEsperVehicleUsageIntervalEvent> mapVehicleUsageIntervalsToLegacy(
            List<DriverWorkingTimeVehicleUsageInterval> values
    ) {
        return safe(values).stream().map(TachographEsperDriverProcessingResultDto::toLegacy).toList();
    }

    private static List<DriverWorkingTimeVehicleUsageInterval> mapVehicleUsageIntervalsFromLegacy(
            List<TachographEsperVehicleUsageIntervalEvent> values
    ) {
        return safe(values).stream().map(TachographEsperDriverProcessingResultDto::toCommon).toList();
    }

    private static List<TachographEsperVuCardAbsentIntervalEvent> mapVuCardAbsentIntervalsToLegacy(
            List<DriverWorkingTimeVuCardAbsentInterval> values
    ) {
        return safe(values).stream().map(TachographEsperDriverProcessingResultDto::toLegacy).toList();
    }

    private static List<DriverWorkingTimeVuCardAbsentInterval> mapVuCardAbsentIntervalsFromLegacy(
            List<TachographEsperVuCardAbsentIntervalEvent> values
    ) {
        return safe(values).stream().map(TachographEsperDriverProcessingResultDto::toCommon).toList();
    }

    private static List<TachographEsperSupportGeoEvent> mapSupportGeoEventsToLegacy(
            List<DriverWorkingTimeSupportGeoEvent> values
    ) {
        return safe(values).stream().map(TachographEsperDriverProcessingResultDto::toLegacy).toList();
    }

    private static List<DriverWorkingTimeSupportGeoEvent> mapSupportGeoEventsFromLegacy(
            List<TachographEsperSupportGeoEvent> values
    ) {
        return safe(values).stream().map(TachographEsperDriverProcessingResultDto::toCommon).toList();
    }

    private static DriverWorkingTimeActivityInterval toCommon(TachographEsperActivityIntervalEvent value) {
        if (value == null) {
            return null;
        }
        return new DriverWorkingTimeActivityInterval(
                value.sessionId(),
                value.driverKey(),
                value.intervalId(),
                value.activityType(),
                value.cardSlot(),
                value.cardStatus(),
                value.drivingStatus(),
                value.registrationKey(),
                value.vehicleKey(),
                value.sourceKind(),
                firstSourceIntervalId(value.intervalId(), value.sourceIntervalIds()),
                lastSourceIntervalId(value.intervalId(), value.sourceIntervalIds()),
                value.startedAt(),
                value.endedAt(),
                epochSecond(value.startedAt()),
                epochSecond(value.endedAt()),
                value.durationSeconds(),
                value.sourceIntervalIds(),
                value.synthetic(),
                value.clippedToRequestedPeriod(),
                value.level()
        );
    }

    private static TachographEsperActivityIntervalEvent toLegacy(DriverWorkingTimeActivityInterval value) {
        return new TachographEsperActivityIntervalEvent(
                value.sessionId(),
                value.driverKey(),
                value.intervalId(),
                value.activityType(),
                value.cardSlot(),
                value.cardStatus(),
                value.drivingStatus(),
                value.registrationKey(),
                value.vehicleKey(),
                value.sourceKind(),
                value.startedAt(),
                value.endedAt(),
                value.durationSeconds(),
                value.sourceIntervalIds(),
                value.synthetic(),
                value.clippedToRequestedPeriod(),
                value.level()
        );
    }

    private static DriverWorkingTimeDrivingInterruptionInterval toCommon(
            TachographEsperDrivingInterruptionIntervalEvent value
    ) {
        if (value == null) {
            return null;
        }
        return new DriverWorkingTimeDrivingInterruptionInterval(
                value.sessionId(),
                value.driverKey(),
                value.startedAt(),
                value.endedAt(),
                value.durationSeconds(),
                value.previousDrivingSourceIntervalId(),
                value.nextDrivingSourceIntervalId(),
                value.previousRegistrationKey(),
                value.nextRegistrationKey(),
                value.previousVehicleKey(),
                value.nextVehicleKey()
        );
    }

    private static TachographEsperDrivingInterruptionIntervalEvent toLegacy(
            DriverWorkingTimeDrivingInterruptionInterval value
    ) {
        return new TachographEsperDrivingInterruptionIntervalEvent(
                value.sessionId(),
                value.driverKey(),
                value.startedAt(),
                value.endedAt(),
                value.durationSeconds(),
                value.previousDrivingSourceIntervalId(),
                value.nextDrivingSourceIntervalId(),
                value.previousRegistrationKey(),
                value.nextRegistrationKey(),
                value.previousVehicleKey(),
                value.nextVehicleKey()
        );
    }

    private static DriverWorkingTimeRestCoverageInterval toCommon(
            TachographEsperDailyWeeklyRestCandidateCoverageIntervalEvent value
    ) {
        if (value == null) {
            return null;
        }
        return new DriverWorkingTimeRestCoverageInterval(
                value.sessionId(),
                value.driverKey(),
                value.startedAt(),
                value.endedAt(),
                value.durationSeconds(),
                value.cardAbsentDurationSeconds(),
                value.cardAbsentCoveragePercent(),
                value.previousDrivingSourceIntervalId(),
                value.nextDrivingSourceIntervalId(),
                value.previousRegistrationKey(),
                value.nextRegistrationKey(),
                value.previousVehicleKey(),
                value.nextVehicleKey(),
                value.beginBoundaryOdometerKm(),
                value.endBoundaryOdometerKm(),
                value.beginGeoEventId(),
                value.beginGeoEventDomain(),
                value.beginGeoOccurredAt(),
                value.beginLatitude(),
                value.beginLongitude(),
                value.beginGeoDistanceSeconds(),
                value.beginGeoOdometerKm(),
                value.endGeoEventId(),
                value.endGeoEventDomain(),
                value.endGeoOccurredAt(),
                value.endLatitude(),
                value.endLongitude(),
                value.endGeoDistanceSeconds(),
                value.endGeoOdometerKm(),
                value.geoEvidenceMovementMeters(),
                value.geoEvidenceMovementCategory(),
                toCommon(value.beginGeoEvent()),
                toCommon(value.endGeoEvent())
        );
    }

    private static DriverWorkingTimeRestCoverageInterval toCommon(
            TachographEsperPotentialHomeOvernightStayIntervalEvent value
    ) {
        if (value == null) {
            return null;
        }
        return new DriverWorkingTimeRestCoverageInterval(
                value.sessionId(),
                value.driverKey(),
                value.startedAt(),
                value.endedAt(),
                value.durationSeconds(),
                value.cardAbsentDurationSeconds(),
                value.cardAbsentCoveragePercent(),
                value.previousDrivingSourceIntervalId(),
                value.nextDrivingSourceIntervalId(),
                value.previousRegistrationKey(),
                value.nextRegistrationKey(),
                value.previousVehicleKey(),
                value.nextVehicleKey(),
                value.beginBoundaryOdometerKm(),
                value.endBoundaryOdometerKm(),
                value.beginGeoEventId(),
                value.beginGeoEventDomain(),
                value.beginGeoOccurredAt(),
                value.beginLatitude(),
                value.beginLongitude(),
                value.beginGeoDistanceSeconds(),
                value.beginGeoOdometerKm(),
                value.endGeoEventId(),
                value.endGeoEventDomain(),
                value.endGeoOccurredAt(),
                value.endLatitude(),
                value.endLongitude(),
                value.endGeoDistanceSeconds(),
                value.endGeoOdometerKm(),
                value.geoEvidenceMovementMeters(),
                value.geoEvidenceMovementCategory(),
                toCommon(value.beginGeoEvent()),
                toCommon(value.endGeoEvent())
        );
    }

    private static DriverWorkingTimeRestCoverageInterval toCommon(
            TachographEsperPotentialInVehicleOvernightStayIntervalEvent value
    ) {
        if (value == null) {
            return null;
        }
        return new DriverWorkingTimeRestCoverageInterval(
                value.sessionId(),
                value.driverKey(),
                value.startedAt(),
                value.endedAt(),
                value.durationSeconds(),
                value.cardAbsentDurationSeconds(),
                value.cardAbsentCoveragePercent(),
                value.previousDrivingSourceIntervalId(),
                value.nextDrivingSourceIntervalId(),
                value.previousRegistrationKey(),
                value.nextRegistrationKey(),
                value.previousVehicleKey(),
                value.nextVehicleKey(),
                value.beginBoundaryOdometerKm(),
                value.endBoundaryOdometerKm(),
                value.beginGeoEventId(),
                value.beginGeoEventDomain(),
                value.beginGeoOccurredAt(),
                value.beginLatitude(),
                value.beginLongitude(),
                value.beginGeoDistanceSeconds(),
                value.beginGeoOdometerKm(),
                value.endGeoEventId(),
                value.endGeoEventDomain(),
                value.endGeoOccurredAt(),
                value.endLatitude(),
                value.endLongitude(),
                value.endGeoDistanceSeconds(),
                value.endGeoOdometerKm(),
                value.geoEvidenceMovementMeters(),
                value.geoEvidenceMovementCategory(),
                toCommon(value.beginGeoEvent()),
                toCommon(value.endGeoEvent())
        );
    }

    private static TachographEsperDailyWeeklyRestCandidateCoverageIntervalEvent toLegacyCoverage(
            DriverWorkingTimeRestCoverageInterval value
    ) {
        return new TachographEsperDailyWeeklyRestCandidateCoverageIntervalEvent(
                value.sessionId(),
                value.driverKey(),
                value.startedAt(),
                value.endedAt(),
                value.durationSeconds(),
                value.cardAbsentDurationSeconds(),
                value.cardAbsentCoveragePercent(),
                value.previousDrivingSourceIntervalId(),
                value.nextDrivingSourceIntervalId(),
                value.previousRegistrationKey(),
                value.nextRegistrationKey(),
                value.previousVehicleKey(),
                value.nextVehicleKey(),
                value.beginBoundaryOdometerKm(),
                value.endBoundaryOdometerKm(),
                value.beginGeoEventId(),
                value.beginGeoEventDomain(),
                value.beginGeoOccurredAt(),
                value.beginLatitude(),
                value.beginLongitude(),
                value.beginGeoDistanceSeconds(),
                value.beginGeoOdometerKm(),
                value.endGeoEventId(),
                value.endGeoEventDomain(),
                value.endGeoOccurredAt(),
                value.endLatitude(),
                value.endLongitude(),
                value.endGeoDistanceSeconds(),
                value.endGeoOdometerKm(),
                value.geoEvidenceMovementMeters(),
                value.geoEvidenceMovementCategory(),
                toLegacy(value.beginGeoEvent()),
                toLegacy(value.endGeoEvent())
        );
    }

    private static TachographEsperPotentialHomeOvernightStayIntervalEvent toLegacyPotentialHome(
            DriverWorkingTimeRestCoverageInterval value
    ) {
        return new TachographEsperPotentialHomeOvernightStayIntervalEvent(
                value.sessionId(),
                value.driverKey(),
                value.startedAt(),
                value.endedAt(),
                value.durationSeconds(),
                value.cardAbsentDurationSeconds(),
                value.cardAbsentCoveragePercent(),
                value.previousDrivingSourceIntervalId(),
                value.nextDrivingSourceIntervalId(),
                value.previousRegistrationKey(),
                value.nextRegistrationKey(),
                value.previousVehicleKey(),
                value.nextVehicleKey(),
                value.beginBoundaryOdometerKm(),
                value.endBoundaryOdometerKm(),
                value.beginGeoEventId(),
                value.beginGeoEventDomain(),
                value.beginGeoOccurredAt(),
                value.beginLatitude(),
                value.beginLongitude(),
                value.beginGeoDistanceSeconds(),
                value.beginGeoOdometerKm(),
                value.endGeoEventId(),
                value.endGeoEventDomain(),
                value.endGeoOccurredAt(),
                value.endLatitude(),
                value.endLongitude(),
                value.endGeoDistanceSeconds(),
                value.endGeoOdometerKm(),
                value.geoEvidenceMovementMeters(),
                value.geoEvidenceMovementCategory(),
                toLegacy(value.beginGeoEvent()),
                toLegacy(value.endGeoEvent())
        );
    }

    private static TachographEsperPotentialInVehicleOvernightStayIntervalEvent toLegacyPotentialInVehicle(
            DriverWorkingTimeRestCoverageInterval value
    ) {
        return new TachographEsperPotentialInVehicleOvernightStayIntervalEvent(
                value.sessionId(),
                value.driverKey(),
                value.startedAt(),
                value.endedAt(),
                value.durationSeconds(),
                value.cardAbsentDurationSeconds(),
                value.cardAbsentCoveragePercent(),
                value.previousDrivingSourceIntervalId(),
                value.nextDrivingSourceIntervalId(),
                value.previousRegistrationKey(),
                value.nextRegistrationKey(),
                value.previousVehicleKey(),
                value.nextVehicleKey(),
                value.beginBoundaryOdometerKm(),
                value.endBoundaryOdometerKm(),
                value.beginGeoEventId(),
                value.beginGeoEventDomain(),
                value.beginGeoOccurredAt(),
                value.beginLatitude(),
                value.beginLongitude(),
                value.beginGeoDistanceSeconds(),
                value.beginGeoOdometerKm(),
                value.endGeoEventId(),
                value.endGeoEventDomain(),
                value.endGeoOccurredAt(),
                value.endLatitude(),
                value.endLongitude(),
                value.endGeoDistanceSeconds(),
                value.endGeoOdometerKm(),
                value.geoEvidenceMovementMeters(),
                value.geoEvidenceMovementCategory(),
                toLegacy(value.beginGeoEvent()),
                toLegacy(value.endGeoEvent())
        );
    }

    private static DriverWorkingTimePotentialInVehicleTripInterval toCommon(
            TachographEsperPotentialInVehicleTripIntervalEvent value
    ) {
        if (value == null) {
            return null;
        }
        return new DriverWorkingTimePotentialInVehicleTripInterval(
                value.sessionId(),
                value.driverKey(),
                value.startedAt(),
                value.endedAt(),
                value.durationSeconds(),
                value.registrationKey(),
                value.vehicleKey(),
                value.containedPotentialInVehicleOvernightStayIntervalCount(),
                value.containedPotentialInVehicleOvernightStayDurationSeconds(),
                value.containedCardAbsentDurationSeconds(),
                value.firstPotentialInVehicleOvernightStayStartedAt(),
                value.lastPotentialInVehicleOvernightStayEndedAt(),
                value.firstPreviousDrivingSourceIntervalId(),
                value.lastNextDrivingSourceIntervalId(),
                safe(value.potentialInVehicleOvernightStayIntervals()).stream()
                        .map(TachographEsperDriverProcessingResultDto::toCommon)
                        .toList()
        );
    }

    private static TachographEsperPotentialInVehicleTripIntervalEvent toLegacy(
            DriverWorkingTimePotentialInVehicleTripInterval value
    ) {
        return new TachographEsperPotentialInVehicleTripIntervalEvent(
                value.sessionId(),
                value.driverKey(),
                value.startedAt(),
                value.endedAt(),
                value.durationSeconds(),
                value.registrationKey(),
                value.vehicleKey(),
                value.containedPotentialInVehicleOvernightStayIntervalCount(),
                value.containedPotentialInVehicleOvernightStayDurationSeconds(),
                value.containedCardAbsentDurationSeconds(),
                value.firstPotentialInVehicleOvernightStayStartedAt(),
                value.lastPotentialInVehicleOvernightStayEndedAt(),
                value.firstPreviousDrivingSourceIntervalId(),
                value.lastNextDrivingSourceIntervalId(),
                safe(value.potentialInVehicleOvernightStayIntervals()).stream()
                        .map(TachographEsperDriverProcessingResultDto::toLegacyPotentialInVehicle)
                        .toList()
        );
    }

    private static DriverWorkingTimeVehicleUsageInterval toCommon(TachographEsperVehicleUsageIntervalEvent value) {
        if (value == null) {
            return null;
        }
        return new DriverWorkingTimeVehicleUsageInterval(
                value.sessionId(),
                value.driverKey(),
                value.intervalId(),
                firstSourceIntervalId(value.intervalId(), value.sourceIntervalIds()),
                lastSourceIntervalId(value.intervalId(), value.sourceIntervalIds()),
                value.startedAt(),
                value.endedAt(),
                epochSecond(value.startedAt()),
                value.endedAt() == null ? null : value.endedAt().toEpochSecond(),
                value.durationSeconds(),
                value.odometerBeginKm(),
                value.odometerEndKm(),
                value.registrationKey(),
                value.vehicleKey(),
                value.sourceKind(),
                value.sourceIntervalIds()
        );
    }

    private static TachographEsperVehicleUsageIntervalEvent toLegacy(DriverWorkingTimeVehicleUsageInterval value) {
        return new TachographEsperVehicleUsageIntervalEvent(
                value.sessionId(),
                value.driverKey(),
                value.intervalId(),
                value.startedAt(),
                value.endedAt(),
                value.durationSeconds(),
                value.odometerBeginKm(),
                value.odometerEndKm(),
                value.registrationKey(),
                value.vehicleKey(),
                value.sourceKind(),
                value.sourceIntervalIds()
        );
    }

    private static DriverWorkingTimeVuCardAbsentInterval toCommon(TachographEsperVuCardAbsentIntervalEvent value) {
        if (value == null) {
            return null;
        }
        return new DriverWorkingTimeVuCardAbsentInterval(
                value.sessionId(),
                value.driverKey(),
                value.startedAt(),
                value.endedAt(),
                value.durationSeconds(),
                value.previousUsageIntervalId(),
                value.nextUsageIntervalId(),
                value.previousRegistrationKey(),
                value.nextRegistrationKey(),
                value.previousVehicleKey(),
                value.nextVehicleKey()
        );
    }

    private static TachographEsperVuCardAbsentIntervalEvent toLegacy(DriverWorkingTimeVuCardAbsentInterval value) {
        return new TachographEsperVuCardAbsentIntervalEvent(
                value.sessionId(),
                value.driverKey(),
                value.startedAt(),
                value.endedAt(),
                value.durationSeconds(),
                value.previousUsageIntervalId(),
                value.nextUsageIntervalId(),
                value.previousRegistrationKey(),
                value.nextRegistrationKey(),
                value.previousVehicleKey(),
                value.nextVehicleKey()
        );
    }

    private static DriverWorkingTimeSupportGeoEvent toCommon(TachographEsperSupportGeoEvent value) {
        if (value == null) {
            return null;
        }
        return new DriverWorkingTimeSupportGeoEvent(
                value.eventId(),
                value.driverKey(),
                value.occurredAt(),
                value.eventDomain(),
                value.eventType(),
                value.eventLifecycle(),
                value.registrationKey(),
                value.vehicleKey(),
                value.country(),
                value.region(),
                value.countryFrom(),
                value.countryTo(),
                value.operation(),
                value.latitude(),
                value.longitude(),
                value.odometerKm(),
                value.rawRecordPath()
        );
    }

    private static TachographEsperSupportGeoEvent toLegacy(DriverWorkingTimeSupportGeoEvent value) {
        return new TachographEsperSupportGeoEvent(
                value.eventId(),
                value.driverKey(),
                value.occurredAt(),
                value.eventDomain(),
                value.eventType(),
                value.eventLifecycle(),
                value.registrationKey(),
                value.vehicleKey(),
                value.country(),
                value.region(),
                value.countryFrom(),
                value.countryTo(),
                value.operation(),
                value.latitude(),
                value.longitude(),
                value.odometerKm(),
                value.rawRecordPath()
        );
    }

    private static DriverWorkingTimeGeoEvidenceEvent toCommon(TachographEsperGeoEvidenceEvent value) {
        if (value == null) {
            return null;
        }
        return new DriverWorkingTimeGeoEvidenceEvent(
                value.eventId(),
                value.eventDomain(),
                value.occurredAt(),
                value.latitude(),
                value.longitude(),
                value.distanceSeconds(),
                value.odometerKm()
        );
    }

    private static TachographEsperGeoEvidenceEvent toLegacy(DriverWorkingTimeGeoEvidenceEvent value) {
        if (value == null) {
            return null;
        }
        return new TachographEsperGeoEvidenceEvent(
                value.eventId(),
                value.eventDomain(),
                value.occurredAt(),
                value.latitude(),
                value.longitude(),
                value.distanceSeconds(),
                value.odometerKm()
        );
    }

    private static String firstSourceIntervalId(String intervalId, List<String> sourceIntervalIds) {
        return sourceIntervalIds == null || sourceIntervalIds.isEmpty() ? intervalId : sourceIntervalIds.getFirst();
    }

    private static String lastSourceIntervalId(String intervalId, List<String> sourceIntervalIds) {
        return sourceIntervalIds == null || sourceIntervalIds.isEmpty()
                ? intervalId
                : sourceIntervalIds.getLast();
    }

    private static long epochSecond(OffsetDateTime value) {
        return value == null ? 0L : value.toEpochSecond();
    }

    private static <T> List<T> safe(List<T> values) {
        return values == null ? List.of() : values;
    }
}
