package at.procon.eventhub.esperpoc.dto;

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

public record ActivityIntervalDto(
        UUID driverEntityId,
        UUID vehicleId,
        UUID vehicleRegistrationId,
        String activityType,
        String cardSlot,
        String cardStatus,
        String drivingStatus,
        String sourceKind,
        OffsetDateTime startedAt,
        OffsetDateTime endedAt,
        long durationSeconds,
        String sourceRowId,
        List<String> sourceRowIds,
        boolean clippedToRequestedPeriod,
        String level
) {
    public static ActivityIntervalDto raw(
            UUID driverEntityId,
            UUID vehicleId,
            UUID vehicleRegistrationId,
            String activityType,
            String cardSlot,
            String cardStatus,
            String drivingStatus,
            String sourceKind,
            OffsetDateTime startedAt,
            OffsetDateTime endedAt,
            String sourceRowId
    ) {
        return new ActivityIntervalDto(
                driverEntityId,
                vehicleId,
                vehicleRegistrationId,
                activityType,
                cardSlot,
                cardStatus,
                drivingStatus,
                sourceKind,
                startedAt,
                endedAt,
                Duration.between(startedAt, endedAt).getSeconds(),
                sourceRowId,
                sourceRowId == null ? List.of() : List.of(sourceRowId),
                false,
                "RAW_INTERVAL"
        );
    }

    public ActivityIntervalDto withTime(OffsetDateTime newStartedAt, OffsetDateTime newEndedAt, boolean clipped) {
        return new ActivityIntervalDto(
                driverEntityId,
                vehicleId,
                vehicleRegistrationId,
                activityType,
                cardSlot,
                cardStatus,
                drivingStatus,
                sourceKind,
                newStartedAt,
                newEndedAt,
                Duration.between(newStartedAt, newEndedAt).getSeconds(),
                sourceRowId,
                sourceRowIds,
                clipped,
                level
        );
    }

    public ActivityIntervalDto asMerged(List<String> mergedSourceRowIds) {
        return new ActivityIntervalDto(
                driverEntityId,
                vehicleId,
                vehicleRegistrationId,
                activityType,
                cardSlot,
                cardStatus,
                drivingStatus,
                sourceKind,
                startedAt,
                endedAt,
                durationSeconds,
                sourceRowId,
                mergedSourceRowIds,
                clippedToRequestedPeriod,
                "MERGED_ACTIVITY"
        );
    }
}
