Option Strict On
Option Explicit On

Imports System.Collections.Generic
Imports System.Collections.ObjectModel

Namespace Nexamas.UI.Quality

    ''' <summary>
    ''' Central release-freeze evidence manifest for the product-controls hardening plan.
    ''' </summary>
    Friend NotInheritable Class MASReleaseFreezeEvidenceManifest
        Private Const ExpectedEvidenceCount As Integer = 6
        Private ReadOnly _records As IReadOnlyList(Of MASReleaseFreezeEvidenceRecord)

        Private Sub New(records As IEnumerable(Of MASReleaseFreezeEvidenceRecord))
            Dim normalized As New List(Of MASReleaseFreezeEvidenceRecord)()
            If records IsNot Nothing Then
                For Each record As MASReleaseFreezeEvidenceRecord In records
                    If record IsNot Nothing Then normalized.Add(record)
                Next
            End If
            _records = New ReadOnlyCollection(Of MASReleaseFreezeEvidenceRecord)(normalized.ToArray())
        End Sub

        Friend ReadOnly Property Records As IReadOnlyList(Of MASReleaseFreezeEvidenceRecord)
            Get
                Return _records
            End Get
        End Property

        Friend ReadOnly Property Count As Integer
            Get
                Return _records.Count
            End Get
        End Property

        Friend ReadOnly Property HasExpectedCoverage As Boolean
            Get
                Return Count = ExpectedEvidenceCount AndAlso
                       FindByKey("ProductControls.EvidencePack") IsNot Nothing AndAlso
                       FindByKey("ProductControls.MeasuredRuntimeBenchmarks") IsNot Nothing AndAlso
                       FindByKey("ProductControls.BenchmarkBaselineThresholdPolicy") IsNot Nothing AndAlso
                       FindByKey("ProductControls.KeyboardAccessibilityStress") IsNot Nothing AndAlso
                       FindByKey("ProductControls.FinalCommercialReadinessClosure") IsNot Nothing AndAlso
                       FindByKey("ProductControls.ReleaseFreezeEvidenceLockdown") IsNot Nothing
            End Get
        End Property

        Friend ReadOnly Property HasNoDuplicateEvidenceKeys As Boolean
            Get
                Dim seen As New HashSet(Of String)(System.StringComparer.OrdinalIgnoreCase)
                For Each record As MASReleaseFreezeEvidenceRecord In _records
                    If record Is Nothing Then Continue For
                    If Not seen.Add(record.EvidenceKey) Then Return False
                Next
                Return True
            End Get
        End Property

        Friend ReadOnly Property AllRecordsStructurallyValid As Boolean
            Get
                If Count = 0 Then Return False
                For Each record As MASReleaseFreezeEvidenceRecord In _records
                    If record Is Nothing OrElse Not record.IsStructurallyValid Then Return False
                Next
                Return True
            End Get
        End Property

        Friend ReadOnly Property AllRecordsHaveCertificationLabels As Boolean
            Get
                If Count = 0 Then Return False
                For Each record As MASReleaseFreezeEvidenceRecord In _records
                    If record Is Nothing Then Return False
                    If String.IsNullOrWhiteSpace(record.CertificationLabel) Then Return False
                    If record.CertificationKind <> MASQualityEvidenceCertificationKind.GateCertified AndAlso
                       record.CertificationKind <> MASQualityEvidenceCertificationKind.DeclaredOnly Then Return False
                Next
                Return True
            End Get
        End Property

        Friend ReadOnly Property ReadyForReleaseFreezeEvidence As Boolean
            Get
                Return HasExpectedCoverage AndAlso
                       HasNoDuplicateEvidenceKeys AndAlso
                       AllRecordsStructurallyValid AndAlso
                       AllRecordsHaveCertificationLabels
            End Get
        End Property

        Friend Function FindByKey(evidenceKey As String) As MASReleaseFreezeEvidenceRecord
            Dim normalized As String = If(evidenceKey, String.Empty).Trim()
            If normalized.Length = 0 Then Return Nothing

            For Each record As MASReleaseFreezeEvidenceRecord In _records
                If record IsNot Nothing AndAlso String.Equals(record.EvidenceKey, normalized, System.StringComparison.OrdinalIgnoreCase) Then Return record
            Next
            Return Nothing
        End Function

        Friend Shared Function CreateDefault() As MASReleaseFreezeEvidenceManifest
            Dim records As New List(Of MASReleaseFreezeEvidenceRecord)()
            Const boundary As String = "Release-freeze evidence preserves CompactPreview / ProductionReady stage boundaries and blocks unapproved ProductionReady promotion in release evidence."

            records.Add(CreateRecord("ProductControls.EvidencePack", "Product controls evidence pack", "docs/quality-proof/product-controls-evidence-pack.md", "release-docs/NexamasUI_ProductControls_Evidence_Pack.md", boundary))
            records.Add(CreateRecord("ProductControls.MeasuredRuntimeBenchmarks", "Measured runtime benchmark evidence", "docs/quality-proof/product-controls-measured-runtime-benchmarks.md", "release-docs/NexamasUI_ProductControls_Evidence_Pack.md", boundary))
            records.Add(CreateRecord("ProductControls.BenchmarkBaselineThresholdPolicy", "Benchmark baseline threshold policy", "docs/quality-proof/product-controls-benchmark-baseline-thresholds.md", "release-docs/NexamasUI_ProductControls_Evidence_Pack.md", boundary))
            records.Add(CreateRecord("ProductControls.KeyboardAccessibilityStress", "Keyboard accessibility stress evidence", "docs/quality-proof/product-controls-keyboard-accessibility-stress.md", "release-docs/NexamasUI_ProductControls_Evidence_Pack.md", boundary))
            records.Add(CreateRecord("ProductControls.FinalCommercialReadinessClosure", "Final commercial readiness closure", "docs/quality-proof/product-controls-final-commercial-readiness-closure.md", "release-docs/NexamasUI_ProductControls_Final_Closure.md", boundary))
            records.Add(CreateRecord("ProductControls.ReleaseFreezeEvidenceLockdown", "Release freeze evidence lockdown", "docs/quality-proof/product-controls-release-freeze-lockdown.md", "release-docs/NexamasUI_ProductControls_Final_Closure.md", boundary))

            Return New MASReleaseFreezeEvidenceManifest(records)
        End Function

        Private Shared Function CreateRecord(evidenceKey As String,
                                             title As String,
                                             documentationPath As String,
                                             releasePath As String,
                                             freezeBoundary As String) As MASReleaseFreezeEvidenceRecord
            Return New MASReleaseFreezeEvidenceRecord(evidenceKey, title, documentationPath, releasePath, freezeBoundary)
        End Function

    End Class

End Namespace
