Option Strict On
Option Explicit On

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

Namespace Nexamas.UI.Virtualization

    ''' <summary>
    ''' Immutable internal final-closure manifest for VirtualizationSystem commercial scale governance.
    ''' </summary>
    ''' <remarks>
    ''' This contract does not create controls, mutate scroll adapters, render items, allocate visual containers,
    ''' run benchmarks, or expose public telemetry. It only records closure facts over an already-built
    ''' MASVirtualizationCommercialProofReport.
    ''' </remarks>
    Friend NotInheritable Class MASVirtualizationGovernanceClosureManifest
        Private ReadOnly _requiredGateNames As IReadOnlyList(Of String)
        Private ReadOnly _evidenceReportNames As IReadOnlyList(Of String)
        Private ReadOnly _ownershipContracts As IReadOnlyList(Of String)

        Friend Sub New(status As MASVirtualizationGovernanceClosureStatus,
                       commercialProofReport As MASVirtualizationCommercialProofReport,
                       closureName As String,
                       closureId As String,
                       requiredGateNames As IEnumerable(Of String),
                       evidenceReportNames As IEnumerable(Of String),
                       ownershipContracts As IEnumerable(Of String),
                       summary As String)
            If commercialProofReport Is Nothing Then Throw New ArgumentNullException(NameOf(commercialProofReport))

            Me.Status = status
            Me.CommercialProofReport = commercialProofReport
            Me.ClosureName = NormalizeText(closureName)
            Me.ClosureId = NormalizeText(closureId)
            _requiredGateNames = New ReadOnlyCollection(Of String)(NormalizeList(requiredGateNames))
            _evidenceReportNames = New ReadOnlyCollection(Of String)(NormalizeList(evidenceReportNames))
            _ownershipContracts = New ReadOnlyCollection(Of String)(NormalizeList(ownershipContracts))
            Me.Summary = NormalizeText(summary)
        End Sub

        Friend ReadOnly Property Status As MASVirtualizationGovernanceClosureStatus
        Friend ReadOnly Property CommercialProofReport As MASVirtualizationCommercialProofReport
        Friend ReadOnly Property ClosureName As String
        Friend ReadOnly Property ClosureId As String
        Friend ReadOnly Property Summary As String

        Friend ReadOnly Property RequiredGateNames As IReadOnlyList(Of String)
            Get
                Return _requiredGateNames
            End Get
        End Property

        Friend ReadOnly Property EvidenceReportNames As IReadOnlyList(Of String)
            Get
                Return _evidenceReportNames
            End Get
        End Property

        Friend ReadOnly Property OwnershipContracts As IReadOnlyList(Of String)
            Get
                Return _ownershipContracts
            End Get
        End Property

        Friend ReadOnly Property HasCompleteScaleGovernanceChain As Boolean
            Get
                Return Not String.IsNullOrWhiteSpace(ClosureName) AndAlso
                       Not String.IsNullOrWhiteSpace(ClosureId) AndAlso
                       RequiredGateNames.Count >= 3 AndAlso
                       EvidenceReportNames.Count >= 18 AndAlso
                       OwnershipContracts.Count >= 14 AndAlso
                       CommercialProofReport.IsReadyForCommercialLockdown
            End Get
        End Property

        Friend ReadOnly Property IsClosed As Boolean
            Get
                Return Status = MASVirtualizationGovernanceClosureStatus.Closed AndAlso HasCompleteScaleGovernanceChain
            End Get
        End Property

        Friend ReadOnly Property HasClosureBlocker As Boolean
            Get
                Return Status <> MASVirtualizationGovernanceClosureStatus.Closed OrElse Not HasCompleteScaleGovernanceChain
            End Get
        End Property

        Friend Shared Function CreateFromCommercialProofReport(commercialProofReport As MASVirtualizationCommercialProofReport,
                                                               closureName As String) As MASVirtualizationGovernanceClosureManifest
            If commercialProofReport Is Nothing Then Throw New ArgumentNullException(NameOf(commercialProofReport))

            Dim status As MASVirtualizationGovernanceClosureStatus = ResolveStatus(commercialProofReport)
            Dim name As String = If(String.IsNullOrWhiteSpace(closureName),
                                    "Nexamas UI Virtualization Final Closure",
                                    closureName.Trim())
            Dim closureId As String = "MAS-VIRTUALIZATION-FINAL-CLOSURE-2026-06-18"

            Return New MASVirtualizationGovernanceClosureManifest(
                status,
                commercialProofReport,
                name,
                closureId,
                CreateRequiredGateNames(),
                CreateEvidenceReportNames(),
                CreateOwnershipContracts(),
                ResolveSummary(status, commercialProofReport))
        End Function

        Private Shared Function ResolveStatus(commercialProofReport As MASVirtualizationCommercialProofReport) As MASVirtualizationGovernanceClosureStatus
            If commercialProofReport.BenchmarkSummary Is Nothing OrElse
               commercialProofReport.IntegrationReport Is Nothing OrElse
               commercialProofReport.ProfessionalReadinessReport Is Nothing OrElse
               commercialProofReport.ConsumerBehaviorReport Is Nothing OrElse
               commercialProofReport.ListViewBoundedMeasurementReport Is Nothing OrElse
               commercialProofReport.TileBoxSinglePlanningReport Is Nothing OrElse
               commercialProofReport.ConsumerLifecycleReport Is Nothing OrElse
               commercialProofReport.LargeDataWindowingReport Is Nothing OrElse
               commercialProofReport.LargeDataWindowingCatalogCoverageReport Is Nothing OrElse
               commercialProofReport.ConsumerDiagnosticsReport Is Nothing OrElse
               commercialProofReport.ProjectionScopeReport Is Nothing OrElse
               commercialProofReport.ProjectionScaleReport Is Nothing OrElse
               commercialProofReport.AccessibleRangeReport Is Nothing OrElse
               commercialProofReport.DataGridRenderInvalidationCoalescingReport Is Nothing Then
                Return MASVirtualizationGovernanceClosureStatus.IncompleteGovernance
            End If

            If Not commercialProofReport.ConsumerIntegrationReady OrElse Not commercialProofReport.AllBindingsRequireInvalidationAndBoundaryAudit Then
                Return MASVirtualizationGovernanceClosureStatus.BlockedByConsumerBypass
            End If

            If Not commercialProofReport.IsReadyForCommercialLockdown Then
                Return MASVirtualizationGovernanceClosureStatus.BlockedByCommercialProof
            End If

            Return MASVirtualizationGovernanceClosureStatus.Closed
        End Function

        Private Shared Function ResolveSummary(status As MASVirtualizationGovernanceClosureStatus,
                                               commercialProofReport As MASVirtualizationCommercialProofReport) As String
            Select Case status
                Case MASVirtualizationGovernanceClosureStatus.Closed
                    Return "Virtualization is closed for commercial scale: benchmarks, executed consumer behavior proofs, MASListView 100k variable-height bounded measurement invalidation, MASTileBox 100k single-planning proof, unified consumer dispatcher/lifecycle ownership, shared consumers, domain-specific large-data windowing surfaces, compiled catalog coverage for all component WindowingGate routes, structured diagnostic-code consumer faults, strict consumer diagnostics, explicit projection-scope ownership, 100k source/projection scale proof, accessible virtual-range navigation, DataGrid render/layout invalidation coalescing, invalidation, boundary audit, measurement cache, recycle pool, and scroll-to-index ownership are all governed."
                Case MASVirtualizationGovernanceClosureStatus.BlockedByConsumerBypass
                    Return "Virtualization closure is blocked because one or more heavy consumers bypass shared-controller, invalidation, or boundary-audit governance."
                Case MASVirtualizationGovernanceClosureStatus.BlockedByCommercialProof
                    Return "Virtualization closure is blocked because the commercial proof report is not ready for lockdown."
                Case Else
                    Return "Virtualization closure is incomplete because one or more required proof reports are missing."
            End Select
        End Function

        Private Shared Function CreateRequiredGateNames() As IReadOnlyList(Of String)
            Return New ReadOnlyCollection(Of String)(New List(Of String) From {
                "tools/quality/Run-NexamasUIQuality.ps1",
                "tools/quality/Run-NexamasUIQuality.ps1",
                "tools/quality/Run-NexamasUIQuality.ps1"
            })
        End Function

        Private Shared Function CreateEvidenceReportNames() As IReadOnlyList(Of String)
            Return New ReadOnlyCollection(Of String)(New List(Of String) From {
                "MAS_VIRTUALIZED_ITEMS_SYSTEM_PHASE2.md",
                "MAS_VIRTUALIZATION_SYSTEM.md",
                "MAS_POST_PHASE2_FOUNDATION_COHESION_AUDIT.md",
                "MAS_POST_PHASE2_VIRTUALIZATION_INTEGRITY_COHESION_AUDIT_2026_06_14.md",
                "MAS_POST_PHASE2_VIRTUALIZATION_PRODUCTION_HARDENING_PASS_2026_06_14.md",
                "MAS_POST_PHASE2_VIRTUALIZATION_CONSUMER_BOUNDARY_HARDENING_PASS_2026_06_14.md",
                "MAS_POST_PHASE6_VIRTUALIZATION_PROFESSIONAL_HARDENING_PASS.md",
                "MAS_POST_PHASE6_VIRTUALIZATION_CONSUMER_INTEGRATION_DIAGNOSTICS_LOCKDOWN_PASS1.md",
                "MAS_POST_PHASE6_VIRTUALIZATION_LINEAR_CONSUMERS_LOCKDOWN_PASS2.md",
                "MAS_POST_PHASE6_VIRTUALIZATION_LISTVIEW_CONSUMER_LOCKDOWN_PASS3.md",
                "MAS_POST_PHASE6_VIRTUALIZATION_TREEVIEW_CONSUMER_LOCKDOWN_PASS4.md",
                "MAS_POST_PHASE6_VIRTUALIZATION_TILEGRID_LARGEICON_LOCKDOWN_PASS5.md",
                "MAS_POST_PHASE6_VIRTUALIZATION_FILEEXPLORER_COMPOSED_CONSUMER_LOCKDOWN_PASS6.md",
                "MAS_POST_PHASE6_VIRTUALIZATION_FINAL_LOCKDOWN_COMMERCIAL_PROOF_PASS7.md",
                "MAS_VIRTUALIZATION_FINAL_CLOSURE_COMMERCIAL_SCALE_GOVERNANCE_2026_06_18.md",
                "MAS_VIRTUALIZATION_CONSUMER_BEHAVIOR_PROOF.md",
                "MAS_VIRTUALIZATION_LISTVIEW_BOUNDED_MEASUREMENT_PROOF.md",
                "MAS_VIRTUALIZATION_TILEBOX_SINGLE_PLANNING_PROOF.md",
                "MAS_VIRTUALIZATION_CONSUMER_LIFECYCLE_DISPATCHER_PROOF.md",
                "MAS_VIRTUALIZATION_LARGE_DATA_WINDOWING_CATALOG_PROOF.md",
                "MAS_VIRTUALIZATION_LARGE_DATA_WINDOWING_CATALOG_COVERAGE_PROOF.md",
                "MAS_VIRTUALIZATION_CONSUMER_DIAGNOSTICS_STRICT_MODE_PROOF.md",
                "MAS_VIRTUALIZATION_PROJECTION_SCOPE_CONTRACT_PROOF.md",
                "MAS_VIRTUALIZATION_PROJECTION_SCALE_PROOF.md",
                "MAS_VIRTUALIZATION_ACCESSIBLE_RANGE_PROOF.md",
                "MAS_VIRTUALIZATION_DATAGRID_RENDER_INVALIDATION_PROOF.md"
            })
        End Function

        Private Shared Function CreateOwnershipContracts() As IReadOnlyList(Of String)
            Return New ReadOnlyCollection(Of String)(New List(Of String) From {
                "MASVirtualizedItemsController",
                "MASVirtualizationMeasurementCache",
                "MASVirtualizationRecyclePool",
                "MASVirtualizationRealizationPlan",
                "MASVirtualizationScrollRequest",
                "MASVirtualizationAutoInvalidationCoordinator",
                "MASVirtualizationBenchmarkSuite",
                "MASVirtualizationBenchmarkSummary",
                "MASVirtualizationConsumerCatalog",
                "MASVirtualizationIntegrationGate",
                "MASVirtualizationConsumerBehaviorGate",
                "MASListViewBoundedMeasurementGate",
                "MASListViewBoundedMeasurementReport",
                "MASTileBoxSinglePlanningGate",
                "MASTileBoxSinglePlanningReport",
                "MASVirtualizationConsumerLifecycleGate",
                "MASLinearVirtualizationConsumerCoordinator",
                "MASTileGridVirtualizationConsumerCoordinator",
                "MASVirtualizationConsumerBehaviorReport",
                "MASVirtualizationConsumerLifecycleReport",
                "MASLargeDataWindowingCatalog",
                "MASLargeDataWindowingGate",
                "MASLargeDataWindowingReport",
                "MASLargeDataWindowingCatalogCoverageGate",
                "MASLargeDataWindowingCatalogCoverageReport",
                "MASVirtualizationConsumerDiagnosticContexts",
                "MASVirtualizationConsumerDiagnosticCode",
                "MASVirtualizationConsumerFaultDiagnostics",
                "MASVirtualizationConsumerFaultBoundary",
                "MASVirtualizationConsumerFaultDiagnosticReport",
                "MASVirtualizationConsumerDiagnosticsGate",
                "MASVirtualizationConsumerDiagnosticsReport",
                "MASVirtualizationProjectionScopeCatalog",
                "MASVirtualizationProjectionScopeGate",
                "MASVirtualizationProjectionScopeReport",
                "MASVirtualizationProjectionScaleGate",
                "MASVirtualizationProjectionScaleReport",
                "MASVirtualizationAccessibleRangeBinding",
                "MASVirtualizationAccessibleRangeGate",
                "MASVirtualizationAccessibleRangeReport",
                "MASDataGridRenderInvalidationCoalescingGate",
                "MASDataGridRenderInvalidationCoalescingReport",
                "MASVirtualizationCommercialProofReport",
                "MASVirtualizationCommercialProofGate"
            })
        End Function

        Private Shared Function NormalizeText(value As String) As String
            Return If(value, String.Empty).Trim()
        End Function

        Private Shared Function NormalizeList(values As IEnumerable(Of String)) As List(Of String)
            Dim result As New List(Of String)()
            If values Is Nothing Then Return result

            For Each value As String In values
                Dim normalized As String = NormalizeText(value)
                If normalized.Length > 0 AndAlso Not result.Contains(normalized) Then
                    result.Add(normalized)
                End If
            Next

            Return result
        End Function

    End Class

End Namespace
