package at.procon.eventhub.processing.service;

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

@Service
public class UnifiedVehicleEventSourceService {

    private final List<UnifiedVehicleEventSource> eventSources;

    public UnifiedVehicleEventSourceService(List<UnifiedVehicleEventSource> eventSources) {
        this.eventSources = List.copyOf(eventSources);
    }

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

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