package at.procon.eventhub.tachographfilesession.service;

import at.procon.eventhub.dto.EventHubEventDto;
import at.procon.eventhub.tachographfilesession.model.DriverExtractionSession;
import at.procon.eventhub.tachographfilesession.model.ResolvedDriverTimeline;
import at.procon.eventhub.tachographfilesession.model.TachographFileSession;
import java.time.OffsetDateTime;
import java.util.List;
import java.util.UUID;

public record TachographEsperProcessingInput(
        UUID sessionId,
        String driverKey,
        TachographFileSession session,
        DriverExtractionSession driverSession,
        ResolvedDriverTimeline timeline,
        List<EventHubEventDto> eventInputEvents,
        OffsetDateTime requestedFrom,
        OffsetDateTime requestedTo,
        int significantDrivingMinutes,
        int minimumRestPeriodMinutes,
        boolean forceEventInput,
        List<String> notes
) {
    public TachographEsperProcessingInput {
        eventInputEvents = eventInputEvents == null ? List.of() : List.copyOf(eventInputEvents);
        significantDrivingMinutes = Math.max(1, significantDrivingMinutes);
        minimumRestPeriodMinutes = Math.max(1, minimumRestPeriodMinutes);
        notes = notes == null ? List.of() : List.copyOf(notes);
    }

    public static TachographEsperProcessingInput fromFileSession(
            TachographFileSession session,
            DriverExtractionSession driverSession,
            ResolvedDriverTimeline timeline,
            OffsetDateTime requestedFrom,
            OffsetDateTime requestedTo,
            int significantDrivingMinutes,
            int minimumRestPeriodMinutes,
            List<String> notes
    ) {
        return new TachographEsperProcessingInput(
                session == null ? null : session.sessionId(),
                driverSession == null ? null : driverSession.driverKey(),
                session,
                driverSession,
                timeline,
                List.of(),
                requestedFrom,
                requestedTo,
                significantDrivingMinutes,
                minimumRestPeriodMinutes,
                false,
                notes
        );
    }

    public static TachographEsperProcessingInput fromEvents(
            UUID sessionId,
            String driverKey,
            ResolvedDriverTimeline timeline,
            List<EventHubEventDto> eventInputEvents,
            OffsetDateTime requestedFrom,
            OffsetDateTime requestedTo,
            int significantDrivingMinutes,
            int minimumRestPeriodMinutes,
            List<String> notes
    ) {
        return new TachographEsperProcessingInput(
                sessionId,
                driverKey,
                null,
                null,
                timeline,
                eventInputEvents,
                requestedFrom,
                requestedTo,
                significantDrivingMinutes,
                minimumRestPeriodMinutes,
                true,
                notes
        );
    }

    public boolean hasEventInputEvents() {
        return !eventInputEvents.isEmpty();
    }
}
