package com.correx.core.validation.semantic.rules

import com.correx.core.validation.model.ValidationContext
import com.correx.core.validation.model.ValidationIssue
import com.correx.core.validation.model.ValidationLocation
import com.correx.core.validation.model.ValidationSeverity
import com.correx.core.validation.semantic.SemanticRule

class MissingToolRule : SemanticRule {

    override fun validate(context: ValidationContext): List<ValidationIssue> {
        val available = context.availableTools ?: return emptyList()

        val issues = mutableListOf<ValidationIssue>()

        for (stageId in context.graph.stageIds.sortedBy { it.value }) {
            val stageConfig = context.graph.stages[stageId] ?: continue
            for (toolName in stageConfig.allowedTools.sorted()) {
                if (toolName !in available) {
                    issues.add(
                        ValidationIssue(
                            code = "MISSING_TOOL",
                            message = "Stage '${stageId.value}' allows unregistered tool '$toolName'",
                            severity = ValidationSeverity.WARNING,
                            location = ValidationLocation.Graph(stageId = stageId)
                        )
                    )
                }
            }
        }

        return issues
    }
}
