package com.correx.core.toolintent

import com.correx.core.events.risk.RiskAction

/** Open, config-driven registry of plane-2 rules. New rules plug in here. */
class ToolCallAssessor(private val rules: List<ToolCallRule>) {

    fun assess(input: ToolCallAssessmentInput): ToolCallAssessment {
        val results = rules.filter { it.appliesTo(input.capabilities) }.map { it.assess(input) }
        return ToolCallAssessment(
            issues = results.flatMap { it.issues },
            observations = results.flatMap { it.observations },
            disposition = results.fold(RiskAction.PROCEED) { acc, r -> maxAction(acc, r.disposition) },
        )
    }
}

internal fun maxAction(a: RiskAction, b: RiskAction): RiskAction =
    if (a.severityRank() >= b.severityRank()) a else b

private fun RiskAction.severityRank(): Int = when (this) {
    RiskAction.PROCEED -> 0
    RiskAction.PROMPT_USER -> 1
    RiskAction.BLOCK -> 2
}
