package at.procon.eventhub.processing.service;

import at.procon.eventhub.dto.EventHubEventDto;
import at.procon.eventhub.processing.model.UnifiedDriverEventsRequest;
import java.util.List;
import org.springframework.stereotype.Service;

@Service
public class UnifiedDriverEventSourceService {

    private final List<UnifiedDriverEventSource> eventSources;

    public UnifiedDriverEventSourceService(List<UnifiedDriverEventSource> eventSources) {
        this.eventSources = List.copyOf(eventSources);
    }

    public List<EventHubEventDto> loadDriverEvents(UnifiedDriverEventsRequest request) {
        return eventSources.stream()
                .filter(source -> source.supports(request))
                .findFirst()
                .orElseThrow(() -> unsupportedSource(request.sourceFamily().name()))
                .loadDriverEvents(request);
    }

    private IllegalArgumentException unsupportedSource(String sourceFamily) {
        return new IllegalArgumentException("No unified driver event source is registered for source family " + sourceFamily + ".");
    }
}
