package com.correx.infrastructure.inference

import com.correx.core.events.types.StageId
import com.correx.core.inference.InferenceProvider
import com.correx.core.inference.ModelCapability
import com.correx.core.inference.NoEligibleProviderException
import com.correx.core.inference.RoutingStrategy

/**
 * Selects the first [InferenceProvider] from [candidates] whose declared capabilities
 * cover all [requiredCapabilities].
 *
 * "First" means the list order as supplied by [com.correx.core.inference.ProviderRegistry.resolve] — callers that
 * want score-ordered selection should pass a score-sorted list.
 *
 * @throws NoEligibleProviderException if no candidate satisfies all required capabilities.
 */
class FirstAvailableRoutingStrategy : RoutingStrategy {
    override fun select(
        candidates: List<InferenceProvider>,
        requiredCapabilities: Set<ModelCapability>,
    ): InferenceProvider {
        val declared = candidates.firstOrNull { provider ->
            val providerCapabilities = provider.capabilities().map { it.capability }.toSet()
            providerCapabilities.containsAll(requiredCapabilities)
        }
        return declared ?: throw NoEligibleProviderException(
            StageId("routing"),
            requiredCapabilities,
        )
    }
}
