package at.procon.eventhub.service;

import at.procon.eventhub.dto.EventHubEventDto;
import java.util.Comparator;
import java.util.List;
import org.springframework.stereotype.Component;

@Component
public class EventHubEventSorter {

    public List<EventHubEventDto> sort(List<EventHubEventDto> events) {
        return events.stream()
                .sorted(Comparator
                        .comparing(EventHubEventDto::occurredAt)
                        .thenComparing(EventHubEventDto::externalSourceEventId, Comparator.nullsLast(String::compareTo))
                        .thenComparing(event -> event.eventType().name()))
                .toList();
    }
}
