package at.procon.dip.ingestion.service;

import at.procon.dip.ingestion.spi.DocumentIngestionAdapter;
import at.procon.dip.ingestion.spi.IngestionResult;
import at.procon.dip.ingestion.spi.SourceDescriptor;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class DocumentIngestionGateway {

    private final List<DocumentIngestionAdapter> adapters;

    public IngestionResult ingest(SourceDescriptor sourceDescriptor) {
        return adapters.stream()
                .filter(adapter -> adapter.supports(sourceDescriptor))
                .findFirst()
                .orElseThrow(() -> new IllegalArgumentException(
                        "No ingestion adapter registered for source type " + sourceDescriptor.sourceType()))
                .ingest(sourceDescriptor);
    }
}
