package at.procon.eventhub.processing.service;

import at.procon.eventhub.processing.model.UnifiedDriverTimelineRequest;
import at.procon.eventhub.tachographfilesession.model.ResolvedDriverTimeline;
import java.util.List;
import org.springframework.stereotype.Service;

@Service
public class UnifiedDriverTimelineService {

    private final List<UnifiedDriverTimelineSource> timelineSources;

    public UnifiedDriverTimelineService(List<UnifiedDriverTimelineSource> timelineSources) {
        this.timelineSources = List.copyOf(timelineSources);
    }

    public ResolvedDriverTimeline loadDriverTimeline(UnifiedDriverTimelineRequest request) {
        return timelineSources.stream()
                .filter(source -> source.supports(request))
                .findFirst()
                .orElseThrow(() -> unsupportedSource(request.sourceFamily().name()))
                .loadDriverTimeline(request);
    }

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