package at.procon.eventhub.tachographfilesession.model;

import java.time.Duration;
import java.time.OffsetDateTime;
import java.util.List;

public record ResolvedActivityInterval(
        String intervalId,
        OffsetDateTime from,
        OffsetDateTime to,
        long durationSeconds,
        String activityType,
        String slot,
        String cardStatus,
        String drivingStatus,
        String registrationKey,
        String vehicleKey,
        String sourceKind,
        List<String> sourceIntervalIds,
        boolean synthetic,
        boolean clippedToRequestedPeriod,
        String level
) {
    public static ResolvedActivityInterval raw(
            String intervalId,
            OffsetDateTime from,
            OffsetDateTime to,
            String activityType,
            String slot,
            String cardStatus,
            String drivingStatus,
            String registrationKey,
            String vehicleKey,
            String sourceKind,
            List<String> sourceIntervalIds
    ) {
        return new ResolvedActivityInterval(
                intervalId,
                from,
                to,
                Duration.between(from, to).getSeconds(),
                activityType,
                slot,
                cardStatus,
                drivingStatus,
                registrationKey,
                vehicleKey,
                sourceKind,
                sourceIntervalIds == null ? List.of() : List.copyOf(sourceIntervalIds),
                false,
                false,
                "RAW_INTERVAL"
        );
    }

    public ResolvedActivityInterval withTime(OffsetDateTime newFrom, OffsetDateTime newTo, boolean clipped) {
        return new ResolvedActivityInterval(
                intervalId,
                newFrom,
                newTo,
                Duration.between(newFrom, newTo).getSeconds(),
                activityType,
                slot,
                cardStatus,
                drivingStatus,
                registrationKey,
                vehicleKey,
                sourceKind,
                sourceIntervalIds,
                synthetic,
                clipped,
                level
        );
    }

    public ResolvedActivityInterval asMerged(
            String mergedIntervalId,
            OffsetDateTime newTo,
            List<String> mergedSourceIntervalIds
    ) {
        return new ResolvedActivityInterval(
                mergedIntervalId,
                from,
                newTo,
                Duration.between(from, newTo).getSeconds(),
                activityType,
                slot,
                cardStatus,
                drivingStatus,
                registrationKey,
                vehicleKey,
                sourceKind,
                mergedSourceIntervalIds == null ? List.of() : List.copyOf(mergedSourceIntervalIds),
                synthetic,
                clippedToRequestedPeriod,
                "MERGED_ACTIVITY"
        );
    }
}
