Option Strict On
Option Explicit On

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

Namespace Nexamas.UI.Certification

    ''' <summary>
    ''' Immutable internal manifest proving the current Nexamas UI architecture is closed against dead paths.
    ''' </summary>
    ''' <remarks>
    ''' This manifest records already-owned architecture facts only. It does not inspect source files,
    ''' execute gates, read reports, create controls, run captures, run performance scenarios, render,
    ''' allocate bitmaps, expose telemetry, or mutate platform state.
    ''' </remarks>
    Friend NotInheritable Class NexamasUIFinalArchitectureClosureManifest
        Private ReadOnly _requiredGateNames As IReadOnlyList(Of String)
        Private ReadOnly _evidenceReportNames As IReadOnlyList(Of String)
        Private ReadOnly _ownershipContracts As IReadOnlyList(Of String)
        Private ReadOnly _closedSystemAreas As IReadOnlyList(Of String)

        Friend Sub New(status As NexamasUIFinalArchitectureClosureStatus,
                       closureName As String,
                       closureId As String,
                       hasVisualSurfaceClosure As Boolean,
                       hasThemeBoundaryClosure As Boolean,
                       hasApplicationOwnershipClosure As Boolean,
                       hasSizeLayoutCompositionBoundaryClosure As Boolean,
                       hasRenderVerificationEvidenceBoundary As Boolean,
                       hasPerformanceEvidenceBoundary As Boolean,
                       hasVirtualizationOwnershipBoundary As Boolean,
                       hasDemoDisplayOnlyBoundary As Boolean,
                       hasPublicApiLockdownBoundary As Boolean,
                       hasNoDeadPaths As Boolean,
                       hasNoWrappersAdaptersBridges As Boolean,
                       hasNoRuntimeInGovernance As Boolean,
                       hasNoPublicTelemetryApi As Boolean,
                       hasMonitoredDocumentationDebt As Boolean,
                       requiredGateNames As IEnumerable(Of String),
                       evidenceReportNames As IEnumerable(Of String),
                       ownershipContracts As IEnumerable(Of String),
                       closedSystemAreas As IEnumerable(Of String),
                       summary As String)
            Me.Status = status
            Me.ClosureName = NormalizeText(closureName)
            Me.ClosureId = NormalizeText(closureId)
            Me.HasVisualSurfaceClosure = hasVisualSurfaceClosure
            Me.HasThemeBoundaryClosure = hasThemeBoundaryClosure
            Me.HasApplicationOwnershipClosure = hasApplicationOwnershipClosure
            Me.HasSizeLayoutCompositionBoundaryClosure = hasSizeLayoutCompositionBoundaryClosure
            Me.HasRenderVerificationEvidenceBoundary = hasRenderVerificationEvidenceBoundary
            Me.HasPerformanceEvidenceBoundary = hasPerformanceEvidenceBoundary
            Me.HasVirtualizationOwnershipBoundary = hasVirtualizationOwnershipBoundary
            Me.HasDemoDisplayOnlyBoundary = hasDemoDisplayOnlyBoundary
            Me.HasPublicApiLockdownBoundary = hasPublicApiLockdownBoundary
            Me.HasNoDeadPaths = hasNoDeadPaths
            Me.HasNoWrappersAdaptersBridges = hasNoWrappersAdaptersBridges
            Me.HasNoRuntimeInGovernance = hasNoRuntimeInGovernance
            Me.HasNoPublicTelemetryApi = hasNoPublicTelemetryApi
            Me.HasMonitoredDocumentationDebt = hasMonitoredDocumentationDebt
            _requiredGateNames = New ReadOnlyCollection(Of String)(NormalizeList(requiredGateNames))
            _evidenceReportNames = New ReadOnlyCollection(Of String)(NormalizeList(evidenceReportNames))
            _ownershipContracts = New ReadOnlyCollection(Of String)(NormalizeList(ownershipContracts))
            _closedSystemAreas = New ReadOnlyCollection(Of String)(NormalizeList(closedSystemAreas))
            Me.Summary = NormalizeText(summary)
        End Sub

        Friend ReadOnly Property Status As NexamasUIFinalArchitectureClosureStatus
        Friend ReadOnly Property ClosureName As String
        Friend ReadOnly Property ClosureId As String
        Friend ReadOnly Property HasVisualSurfaceClosure As Boolean
        Friend ReadOnly Property HasThemeBoundaryClosure As Boolean
        Friend ReadOnly Property HasApplicationOwnershipClosure As Boolean
        Friend ReadOnly Property HasSizeLayoutCompositionBoundaryClosure As Boolean
        Friend ReadOnly Property HasRenderVerificationEvidenceBoundary As Boolean
        Friend ReadOnly Property HasPerformanceEvidenceBoundary As Boolean
        Friend ReadOnly Property HasVirtualizationOwnershipBoundary As Boolean
        Friend ReadOnly Property HasDemoDisplayOnlyBoundary As Boolean
        Friend ReadOnly Property HasPublicApiLockdownBoundary As Boolean
        Friend ReadOnly Property HasNoDeadPaths As Boolean
        Friend ReadOnly Property HasNoWrappersAdaptersBridges As Boolean
        Friend ReadOnly Property HasNoRuntimeInGovernance As Boolean
        Friend ReadOnly Property HasNoPublicTelemetryApi As Boolean
        Friend ReadOnly Property HasMonitoredDocumentationDebt As Boolean
        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 ClosedSystemAreas As IReadOnlyList(Of String)
            Get
                Return _closedSystemAreas
            End Get
        End Property

        Friend ReadOnly Property HasCompleteArchitectureClosureChain As Boolean
            Get
                Return Not String.IsNullOrWhiteSpace(ClosureName) AndAlso
                       Not String.IsNullOrWhiteSpace(ClosureId) AndAlso
                       RequiredGateNames.Count >= 10 AndAlso
                       EvidenceReportNames.Count >= 10 AndAlso
                       OwnershipContracts.Count >= 10 AndAlso
                       ClosedSystemAreas.Count >= 8 AndAlso
                       HasVisualSurfaceClosure AndAlso
                       HasThemeBoundaryClosure AndAlso
                       HasApplicationOwnershipClosure AndAlso
                       HasSizeLayoutCompositionBoundaryClosure AndAlso
                       HasRenderVerificationEvidenceBoundary AndAlso
                       HasPerformanceEvidenceBoundary AndAlso
                       HasVirtualizationOwnershipBoundary AndAlso
                       HasDemoDisplayOnlyBoundary AndAlso
                       HasPublicApiLockdownBoundary AndAlso
                       HasNoDeadPaths AndAlso
                       HasNoWrappersAdaptersBridges AndAlso
                       HasNoRuntimeInGovernance AndAlso
                       HasNoPublicTelemetryApi
            End Get
        End Property

        Friend ReadOnly Property IsClosed As Boolean
            Get
                Return HasCompleteArchitectureClosureChain AndAlso
                       (Status = NexamasUIFinalArchitectureClosureStatus.Closed OrElse
                        Status = NexamasUIFinalArchitectureClosureStatus.ClosedWithMonitoring)
            End Get
        End Property

        Friend ReadOnly Property HasClosureBlocker As Boolean
            Get
                Return Status = NexamasUIFinalArchitectureClosureStatus.BlockedByVisualSurface OrElse
                       Status = NexamasUIFinalArchitectureClosureStatus.BlockedBySystemBoundaryDrift OrElse
                       Status = NexamasUIFinalArchitectureClosureStatus.BlockedByEvidenceExecutionPath OrElse
                       Status = NexamasUIFinalArchitectureClosureStatus.BlockedByPublicApiOrDemoLeak OrElse
                       Status = NexamasUIFinalArchitectureClosureStatus.IncompleteGovernance OrElse
                       Not HasCompleteArchitectureClosureChain
            End Get
        End Property

        Friend Shared Function CreateCurrent() As NexamasUIFinalArchitectureClosureManifest
            Return CreateFromFacts(
                "Nexamas UI Final Architecture Closure & Dead-Path Lockdown",
                hasVisualSurfaceClosure:=True,
                hasThemeBoundaryClosure:=True,
                hasApplicationOwnershipClosure:=True,
                hasSizeLayoutCompositionBoundaryClosure:=True,
                hasRenderVerificationEvidenceBoundary:=True,
                hasPerformanceEvidenceBoundary:=True,
                hasVirtualizationOwnershipBoundary:=True,
                hasDemoDisplayOnlyBoundary:=True,
                hasPublicApiLockdownBoundary:=True,
                hasNoDeadPaths:=True,
                hasNoWrappersAdaptersBridges:=True,
                hasNoRuntimeInGovernance:=True,
                hasNoPublicTelemetryApi:=True,
                hasMonitoredDocumentationDebt:=False)
        End Function

        Friend Shared Function CreateFromFacts(closureName As String,
                                               hasVisualSurfaceClosure As Boolean,
                                               hasThemeBoundaryClosure As Boolean,
                                               hasApplicationOwnershipClosure As Boolean,
                                               hasSizeLayoutCompositionBoundaryClosure As Boolean,
                                               hasRenderVerificationEvidenceBoundary As Boolean,
                                               hasPerformanceEvidenceBoundary As Boolean,
                                               hasVirtualizationOwnershipBoundary As Boolean,
                                               hasDemoDisplayOnlyBoundary As Boolean,
                                               hasPublicApiLockdownBoundary As Boolean,
                                               hasNoDeadPaths As Boolean,
                                               hasNoWrappersAdaptersBridges As Boolean,
                                               hasNoRuntimeInGovernance As Boolean,
                                               hasNoPublicTelemetryApi As Boolean,
                                               hasMonitoredDocumentationDebt As Boolean) As NexamasUIFinalArchitectureClosureManifest
            Dim status As NexamasUIFinalArchitectureClosureStatus = ResolveStatus(hasVisualSurfaceClosure,
                                                                                     hasThemeBoundaryClosure,
                                                                                     hasApplicationOwnershipClosure,
                                                                                     hasSizeLayoutCompositionBoundaryClosure,
                                                                                     hasRenderVerificationEvidenceBoundary,
                                                                                     hasPerformanceEvidenceBoundary,
                                                                                     hasVirtualizationOwnershipBoundary,
                                                                                     hasDemoDisplayOnlyBoundary,
                                                                                     hasPublicApiLockdownBoundary,
                                                                                     hasNoDeadPaths,
                                                                                     hasNoWrappersAdaptersBridges,
                                                                                     hasNoRuntimeInGovernance,
                                                                                     hasNoPublicTelemetryApi,
                                                                                     hasMonitoredDocumentationDebt)
            Dim name As String = If(String.IsNullOrWhiteSpace(closureName),
                                    "Nexamas UI Final Architecture Closure & Dead-Path Lockdown",
                                    closureName.Trim())

            Return New NexamasUIFinalArchitectureClosureManifest(
                status,
                name,
                "MAS-PLATFORM-FINAL-ARCHITECTURE-CLOSURE-DEAD-PATH-LOCKDOWN-2026-06-20",
                hasVisualSurfaceClosure,
                hasThemeBoundaryClosure,
                hasApplicationOwnershipClosure,
                hasSizeLayoutCompositionBoundaryClosure,
                hasRenderVerificationEvidenceBoundary,
                hasPerformanceEvidenceBoundary,
                hasVirtualizationOwnershipBoundary,
                hasDemoDisplayOnlyBoundary,
                hasPublicApiLockdownBoundary,
                hasNoDeadPaths,
                hasNoWrappersAdaptersBridges,
                hasNoRuntimeInGovernance,
                hasNoPublicTelemetryApi,
                hasMonitoredDocumentationDebt,
                CreateRequiredGates(),
                CreateEvidenceReports(),
                CreateOwnershipContracts(),
                CreateClosedSystemAreas(),
                ResolveSummary(status))
        End Function

        Private Shared Function ResolveStatus(hasVisualSurfaceClosure As Boolean,
                                              hasThemeBoundaryClosure As Boolean,
                                              hasApplicationOwnershipClosure As Boolean,
                                              hasSizeLayoutCompositionBoundaryClosure As Boolean,
                                              hasRenderVerificationEvidenceBoundary As Boolean,
                                              hasPerformanceEvidenceBoundary As Boolean,
                                              hasVirtualizationOwnershipBoundary As Boolean,
                                              hasDemoDisplayOnlyBoundary As Boolean,
                                              hasPublicApiLockdownBoundary As Boolean,
                                              hasNoDeadPaths As Boolean,
                                              hasNoWrappersAdaptersBridges As Boolean,
                                              hasNoRuntimeInGovernance As Boolean,
                                              hasNoPublicTelemetryApi As Boolean,
                                              hasMonitoredDocumentationDebt As Boolean) As NexamasUIFinalArchitectureClosureStatus
            If Not hasVisualSurfaceClosure OrElse Not hasNoRuntimeInGovernance Then
                Return NexamasUIFinalArchitectureClosureStatus.BlockedByVisualSurface
            End If

            If Not hasThemeBoundaryClosure OrElse
               Not hasApplicationOwnershipClosure OrElse
               Not hasSizeLayoutCompositionBoundaryClosure OrElse
               Not hasVirtualizationOwnershipBoundary OrElse
               Not hasNoDeadPaths Then
                Return NexamasUIFinalArchitectureClosureStatus.BlockedBySystemBoundaryDrift
            End If

            If Not hasRenderVerificationEvidenceBoundary OrElse Not hasPerformanceEvidenceBoundary Then
                Return NexamasUIFinalArchitectureClosureStatus.BlockedByEvidenceExecutionPath
            End If

            If Not hasDemoDisplayOnlyBoundary OrElse
               Not hasPublicApiLockdownBoundary OrElse
               Not hasNoWrappersAdaptersBridges OrElse
               Not hasNoPublicTelemetryApi Then
                Return NexamasUIFinalArchitectureClosureStatus.BlockedByPublicApiOrDemoLeak
            End If

            If hasMonitoredDocumentationDebt Then Return NexamasUIFinalArchitectureClosureStatus.ClosedWithMonitoring
            Return NexamasUIFinalArchitectureClosureStatus.Closed
        End Function

        Private Shared Function ResolveSummary(status As NexamasUIFinalArchitectureClosureStatus) As String
            Select Case status
                Case NexamasUIFinalArchitectureClosureStatus.Closed
                    Return "The current Nexamas UI architecture is closed: Application-owned control admission, system boundaries, SizeLayout-owned surface geometry vocabulary, evidence paths, limited public surface material gateway truth, RenderVerification surface material proof coverage, read-only Diagnostics Dashboard foundation, Command / Action System foundation, Data Binding / ViewModel Binding foundation, demo isolation, and dead-path lockdown are complete."
                Case NexamasUIFinalArchitectureClosureStatus.ClosedWithMonitoring
                    Return "The current Nexamas UI architecture is closed, with monitored documentation debt recorded for follow-up without opening unrestricted runtime or public paths."
                Case NexamasUIFinalArchitectureClosureStatus.BlockedByVisualSurface
                    Return "Final architecture closure is blocked by VisualSurface root, policy/runtime/governance, route, or scope drift."
                Case NexamasUIFinalArchitectureClosureStatus.BlockedBySystemBoundaryDrift
                    Return "Final architecture closure is blocked by Theme, Application ownership, SizeLayout, Composition, Layout, layout surface role, Virtualization, or dead-path boundary drift."
                Case NexamasUIFinalArchitectureClosureStatus.BlockedByEvidenceExecutionPath
                    Return "Final architecture closure is blocked by proof/evidence logic attempting to execute captures, scenarios, or runtime work."
                Case NexamasUIFinalArchitectureClosureStatus.BlockedByPublicApiOrDemoLeak
                    Return "Final architecture closure is blocked by public API, public telemetry, wrapper/adapter/bridge, or demo-owned platform logic leakage."
                Case Else
                    Return "Final architecture closure is incomplete because one or more governance facts are missing."
            End Select
        End Function

        Private Shared Function CreateRequiredGates() As IReadOnlyList(Of String)
            Return New ReadOnlyCollection(Of String)(New List(Of String) From {
                "quality-orchestrator",
                "quality-asset-presence",
                "architecture-validation",
                "application-ownership-closure",
                "component-coverage-map",
                "render-verification-coverage",
                "render-verification-element-coverage-closure",
                "dead-path-closure",
                "size-layout-runtime-verification",
                "size-layout-element-closure",
                "render-hit-bounds-closure",
                "localization-runtime-ownership",
                "output-application-route",
                "performance-diagnostics-orchestration",
                "render-verification-readiness",
                "surface-material-proof",
                "certification-orchestration-closure",
                "commercial-gate",
                "sdk-harness"
            })
        End Function

        Private Shared Function CreateEvidenceReports() As IReadOnlyList(Of String)
            Return New ReadOnlyCollection(Of String)(New List(Of String) From {
                "MAS_SURFACE_VISUAL_SYSTEM_SINGLE_ROOT_RUNTIME_GOVERNANCE_2026_06_20.md",
                "MAS_SURFACE_VISUAL_SYSTEM_THEME_NEUTRALITY_2026_06_20.md",
                "MAS_SURFACE_VISUAL_SYSTEM_RUNTIME_ROUTE_CLOSURE_2026_06_20.md",
                "MAS_SURFACE_VISUAL_SYSTEM_RESPONSIBILITY_SPLIT_2026_06_20.md",
                "MAS_PUBLIC_SURFACE_MATERIAL_GATEWAY_TRUTH_CLOSURE_2026_06_20.md",
                "MAS_RENDER_VERIFICATION_SURFACE_MATERIAL_PROOF_CATCHUP_2026_06_20.md",
                "MAS_LAYOUT_SURFACE_BOUNDARY_HARDENING_2026_06_20.md",
                "MAS_SURFACE_VISUAL_SYSTEM_FINAL_CONSUMER_PROOF_ALIGNMENT_2026_06_20.md",
                "MAS_APPLICATION_OWNERSHIP_CLOSURE_2026_07_01.md",
                "MAS_SIZE_LAYOUT_ELEMENT_CLOSURE_2026_07_01.md",
                "MAS_RENDER_HIT_BOUNDS_CLOSURE_2026_07_01.md",
                "MAS_RENDER_VERIFICATION_ELEMENT_COVERAGE_CLOSURE_2026_07_01.md",
                "MAS_DEAD_PATH_PARALLEL_PATH_CLOSURE_2026_07_01.md",
                "MAS_LOCALIZATION_RUNTIME_OWNERSHIP_CLOSURE_2026_07_01.md",
                "MAS_RENDER_VERIFICATION_FINAL_CLOSURE_EVIDENCE_GOVERNANCE_2026_06_18.md",
                "MAS_PERFORMANCE_FINAL_CLOSURE_AUDIT_DEAD_PATH_LOCKDOWN_2026_06_18.md",
                "MAS_VIRTUALIZATION_FINAL_CLOSURE_COMMERCIAL_SCALE_GOVERNANCE_2026_06_18.md",
                "MAS_DATAGRID_COMMERCIAL_RUNTIME_READINESS_FINAL_CLOSURE_2026_06_19.md",
                "MAS_FLOATRUNTIME_OVERLAY_GOVERNANCE_FINAL_CLOSURE_2026_06_19.md",
                "NEXAMAS_UI_CERTIFICATION_CENTER_COMMERCIAL_EVIDENCE_SURFACE_2026_06_19.md",
                "MAS_SDK_RELEASE_READINESS_PUBLIC_CONTRACT_FREEZE_2026_06_19.md",
                "MAS_DIAGNOSTICS_DASHBOARD_FOUNDATION_2026_06_21.md",
                "MAS_COMMAND_ACTION_SYSTEM_FOUNDATION_2026_06_21.md",
                "MAS_DATA_BINDING_VIEWMODEL_FOUNDATION_2026_06_21.md",
                "NEXAMAS_UI_FINAL_ARCHITECTURE_CLOSURE_DEAD_PATH_LOCKDOWN_2026_06_20.md"
            })
        End Function

        Private Shared Function CreateOwnershipContracts() As IReadOnlyList(Of String)
            Return New ReadOnlyCollection(Of String)(New List(Of String) From {
                "MASSurfaceTreatmentConsumerCatalog",
                "MASSurfaceTreatmentResolver",
                "MASPublicSurfaceMaterialGatewayManifest",
                "MASRenderVerificationSurfaceMaterialProofAudit",
                "MASRenderVerificationSurfaceMaterialProofReport",
                "MASLayoutSurfaceBoundaryAudit",
                "MASLayoutSurfaceRole",
                "MASApplicationOwnershipClosureAudit",
                "MASApplicationOwnershipClosureReport",
                "MASSizeLayoutElementClosureAudit",
                "MASSizeLayoutElementClosureReport",
                "MASSizeLayoutElementClosureFinding",
                "MASRenderHitBoundsClosureAudit",
                "MASRenderHitBoundsClosureReport",
                "MASRenderHitBoundsClosureFinding",
                "MASRenderVerificationElementCoverageClosureAudit",
                "MASRenderVerificationElementCoverageClosureReport",
                "MASRenderVerificationElementCoverageClosureFinding",
                "MASLocalizationRuntimeOwnershipAudit",
                "MASLocalizationRuntimeOwnershipReport",
                "MASLocalizationRuntimeOwnershipFinding",
                "MASDeadPathClosureAudit",
                "MASDeadPathClosureReport",
                "MASDeadPathClosureFinding",
                "MASSurfaceTreatmentConsumerProofAlignmentAudit",
                "MASSurfaceTreatmentRuntimeSurfaceVisual",
                "MASSurfaceThemeProfileResolver",
                "MASRenderVerificationGovernanceClosureManifest",
                "MASPerformanceGovernanceClosureManifest",
                "MASVirtualizationGovernanceClosureManifest",
                "MASDataGridCommercialRuntimeClosureManifest",
                "MASFloatRuntimeGovernanceClosureManifest",
                "NexamasUICertificationManifest",
                "NexamasUISdkReleaseReadinessManifest",
                "MASDiagnosticsDashboardBuilder",
                "MASDiagnosticsDashboardSnapshot",
                "MASCommandRegistry",
                "MASCommandRegistrySnapshot",
                "MASViewModelBinder",
                "MASViewModelBindingSnapshot",
                "NexamasUIFinalArchitectureClosureManifest"
            })
        End Function

        Private Shared Function CreateClosedSystemAreas() As IReadOnlyList(Of String)
            Return New ReadOnlyCollection(Of String)(New List(Of String) From {
                "VisualSurface",
                "ThemeSystem",
                "ApplicationSystem",
                "SizeLayoutSystem",
                "CompositionSystem",
                "RenderVerificationSystem",
                "PerformanceSystem",
                "VirtualizationSystem",
                "DiagnosticsDashboardSystem",
                "CommandActionSystem",
                "DataBindingSystem",
                "LocalizationSystem",
                "QualitySourceTree",
                "ProductShowcaseDemoBoundary",
                "PublicApiCommercialBoundary"
            })
        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)
            Next

            Return result
        End Function

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

End Namespace
