Option Strict On
Option Explicit On

Imports System
Imports System.Collections.Generic

Namespace Nexamas.UI.Quality

    ''' <summary>
    ''' Verifies that the eight product controls have an explicit documentation,
    ''' sample, and evidence-pack declaration before any ProductionReady promotion
    ''' rule may treat documentation as complete.
    ''' </summary>
    Friend NotInheritable Class MASDocumentationSampleEvidenceGate

        Private Sub New()
        End Sub

        Friend Shared Function Passes() As Boolean
            Return EvaluateFindings().Count = 0
        End Function

        Friend Shared Function EvaluateFindings() As IReadOnlyList(Of MASCommercialReadinessFinding)
            Dim findings As New List(Of MASCommercialReadinessFinding)()
            Dim manifest As MASDocumentationSampleEvidenceManifest = Nothing
            Dim matrix As MASCommercialReadinessMatrix = Nothing

            Try
                manifest = MASDocumentationSampleEvidenceManifest.CreateDefault()
                matrix = MASCommercialReadinessMatrixBuilder.BuildProductControlsMatrix()
            Catch ex As Exception
                Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowDiagnosticOnly(ex, "MASDocumentationSampleEvidenceGate.CreateDefault")
                findings.Add(New MASCommercialReadinessFinding("documentation-samples", "Build", MASCommercialReadinessFindingSeverity.Error, "Documentation/sample evidence manifest could not be built."))
                Return findings
            End Try

            If manifest Is Nothing Then
                findings.Add(New MASCommercialReadinessFinding("documentation-samples", "Manifest", MASCommercialReadinessFindingSeverity.Error, "Documentation/sample evidence manifest is missing."))
                Return findings
            End If

            If Not manifest.HasExpectedProductControlCoverage Then
                findings.Add(New MASCommercialReadinessFinding("documentation-samples", "Coverage", MASCommercialReadinessFindingSeverity.Error, "Documentation/sample evidence must cover exactly the eight product controls."))
            End If

            Dim ids As New HashSet(Of String)(StringComparer.OrdinalIgnoreCase)
            For Each record As MASDocumentationSampleEvidenceRecord In manifest.Records
                If record Is Nothing Then
                    findings.Add(New MASCommercialReadinessFinding("documentation-samples", "Record", MASCommercialReadinessFindingSeverity.Error, "Documentation/sample evidence manifest contains a null record."))
                    Continue For
                End If

                If Not ids.Add(record.ComponentId) Then
                    findings.Add(New MASCommercialReadinessFinding(record.ComponentId, "DuplicateId", MASCommercialReadinessFindingSeverity.Error, "Documentation/sample evidence id is duplicated."))
                End If

                If Not record.IsStructurallyValid Then
                    findings.Add(New MASCommercialReadinessFinding(record.ComponentId, "Structure", MASCommercialReadinessFindingSeverity.Error, record.DisplayName & " documentation/sample evidence record is structurally invalid."))
                End If

                If Not record.DeclaresCompactPreviewBoundary Then
                    findings.Add(New MASCommercialReadinessFinding(record.ComponentId, "StageBoundary", MASCommercialReadinessFindingSeverity.Error, record.DisplayName & " documentation must explicitly preserve the CompactPreview / ProductionReady boundary."))
                End If

                If Not record.DeclaresPublicSampleBoundary Then
                    findings.Add(New MASCommercialReadinessFinding(record.ComponentId, "SampleBoundary", MASCommercialReadinessFindingSeverity.Error, record.DisplayName & " sample documentation must keep public examples separate from Friend/internal proof APIs."))
                End If

                If Not record.DeclaresEvidencePackBoundary Then
                    findings.Add(New MASCommercialReadinessFinding(record.ComponentId, "EvidenceBoundary", MASCommercialReadinessFindingSeverity.Error, record.DisplayName & " evidence pack must state that measured benchmarks remain separate."))
                End If

                Dim matrixRecord As MASCommercialReadinessRecord = If(matrix Is Nothing, Nothing, matrix.FindById(record.ComponentId))
                If matrixRecord Is Nothing Then
                    findings.Add(New MASCommercialReadinessFinding(record.ComponentId, "MatrixRecord", MASCommercialReadinessFindingSeverity.Error, record.DisplayName & " has documentation evidence but no commercial readiness matrix record."))
                ElseIf matrixRecord.Stage <> MASCommercialReadinessStage.CompactPreview AndAlso
                       matrixRecord.Stage <> MASCommercialReadinessStage.ProductionReady Then
                    findings.Add(New MASCommercialReadinessFinding(record.ComponentId, "MatrixStage", MASCommercialReadinessFindingSeverity.Error, record.DisplayName & " documentation pack supports only governed CompactPreview or explicitly promoted ProductionReady controls."))
                End If
            Next

            Return findings
        End Function

    End Class

End Namespace
