package com.correx.core.toolintent

import com.correx.core.events.events.AssessedIssue
import com.correx.core.events.risk.RiskAction
import com.correx.core.events.risk.RiskLevel
import com.correx.core.events.risk.RiskSignal
import com.correx.core.events.risk.RiskSummary
import com.correx.core.validation.model.ValidationSeverity

fun ToolCallAssessment.toRiskSummary(): RiskSummary {
    val errorCount = issues.count { it.severity == ValidationSeverity.ERROR }
    val level = when (disposition) {
        RiskAction.BLOCK -> RiskLevel.CRITICAL
        RiskAction.PROMPT_USER -> RiskLevel.MEDIUM
        RiskAction.PROCEED -> RiskLevel.LOW
    }
    return RiskSummary(
        level = level,
        signals = if (errorCount > 0) listOf(RiskSignal.ValidationErrors(errorCount)) else emptyList(),
        recommendedAction = disposition,
        rationale = issues.map { "[${it.code}] ${it.message}" },
    )
}

fun ToolCallAssessment.toAssessedIssues(): List<AssessedIssue> =
    issues.map { AssessedIssue(it.code, it.message, it.severity.name) }
