package com.correx.core.validation.pipeline

import com.correx.core.validation.approval.ApprovalTrigger
import com.correx.core.validation.model.ValidationContext
import com.correx.core.validation.model.ValidationReport
import com.correx.core.validation.model.ValidationSection
import com.correx.core.validation.model.ValidationSeverity

class ValidationPipeline(
    private val validators: List<Validator>,
    private val approvalTrigger: ApprovalTrigger? = null
) {

    suspend fun validate(context: ValidationContext): ValidationOutcome {
        val sections = mutableListOf<ValidationSection>()
        for (validator in validators) {
            val section = validator.validate(context)
            sections += section
            if (section.issues.any { it.severity == ValidationSeverity.ERROR }) {
                // Structural validation errors are not retryable — the graph must be fixed.
                return ValidationOutcome.Rejected(ValidationReport(sections), retryable = false)
            }
        }
        val report = ValidationReport(sections)
        val approvalRequest = approvalTrigger?.evaluate(report)
        return if (approvalRequest != null) {
            ValidationOutcome.NeedsApproval(approvalRequest)
        } else {
            ValidationOutcome.Passed(report)
        }
    }
}
