package at.procon.dip.classification.service;

import at.procon.dip.classification.spi.DetectionResult;
import at.procon.dip.classification.spi.DocumentTypeDetector;
import at.procon.dip.ingestion.spi.SourceDescriptor;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * Selects the first matching detector for a source descriptor.
 */
@Service
@RequiredArgsConstructor
public class DocumentClassificationService {

    private final List<DocumentTypeDetector> detectors;

    public DetectionResult detect(SourceDescriptor sourceDescriptor) {
        return detectors.stream()
                .filter(detector -> detector.supports(sourceDescriptor))
                .findFirst()
                .orElseThrow(() -> new IllegalStateException("No document type detector available"))
                .detect(sourceDescriptor);
    }
}
