Option Strict On
Option Explicit On

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

Namespace Nexamas.UI.Verification

    ''' <summary>
    ''' Immutable internal manifest closing the Render Verification evidence-governance chain.
    ''' </summary>
    ''' <remarks>
    ''' This contract does not capture screenshots, create baselines, compare pixels, construct controls,
    ''' render scenes, run performance proof scenes, or expose public diagnostics. It records whether the
    ''' already-owned Render Verification catalog, registry, policy, reports, and gates form a complete
    ''' commercial evidence chain.
    ''' </remarks>
    Friend NotInheritable Class MASRenderVerificationGovernanceClosureManifest
        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 MASRenderVerificationGovernanceClosureStatus,
                       closureName As String,
                       manifestId As String,
                       targetCount As Integer,
                       scenarioCount As Integer,
                       componentKeyCount As Integer,
                       registeredSceneCount As Integer,
                       requiredGateNames As IEnumerable(Of String),
                       evidenceReportNames As IEnumerable(Of String),
                       ownershipContracts As IEnumerable(Of String),
                       summary As String)
            Me.Status = status
            Me.ClosureName = NormalizeText(closureName)
            Me.ManifestId = NormalizeText(manifestId)
            Me.TargetCount = Math.Max(0, targetCount)
            Me.ScenarioCount = Math.Max(0, scenarioCount)
            Me.ComponentKeyCount = Math.Max(0, componentKeyCount)
            Me.RegisteredSceneCount = Math.Max(0, registeredSceneCount)
            _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 MASRenderVerificationGovernanceClosureStatus
        Friend ReadOnly Property ClosureName As String
        Friend ReadOnly Property ManifestId As String
        Friend ReadOnly Property TargetCount As Integer
        Friend ReadOnly Property ScenarioCount As Integer
        Friend ReadOnly Property ComponentKeyCount As Integer
        Friend ReadOnly Property RegisteredSceneCount As Integer
        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 HasSceneRegistryCoverage As Boolean
            Get
                Return TargetCount > 0 AndAlso
                       ScenarioCount > 0 AndAlso
                       ComponentKeyCount > 0 AndAlso
                       RegisteredSceneCount >= ComponentKeyCount
            End Get
        End Property

        Friend ReadOnly Property HasCompleteEvidenceGovernanceChain As Boolean
            Get
                Return Not String.IsNullOrWhiteSpace(ClosureName) AndAlso
                       Not String.IsNullOrWhiteSpace(ManifestId) AndAlso
                       RequiredGateNames.Count >= 2 AndAlso
                       EvidenceReportNames.Count >= 10 AndAlso
                       OwnershipContracts.Count >= 8 AndAlso
                       HasSceneRegistryCoverage
            End Get
        End Property

        Friend ReadOnly Property IsClosed As Boolean
            Get
                Return Status = MASRenderVerificationGovernanceClosureStatus.Closed AndAlso
                       HasCompleteEvidenceGovernanceChain
            End Get
        End Property

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

        Friend Shared Function CreateFromInventorySnapshot(snapshot As MASRenderVerificationInventorySnapshot,
                                                           closureName As String) As MASRenderVerificationGovernanceClosureManifest
            If snapshot Is Nothing Then Throw New ArgumentNullException(NameOf(snapshot))

            Dim status As MASRenderVerificationGovernanceClosureStatus = ResolveStatus(snapshot)
            Dim name As String = If(String.IsNullOrWhiteSpace(closureName),
                                    "Nexamas UI Render Verification Final Closure",
                                    closureName.Trim())
            Dim manifestId As String = "MAS-RENDER-VERIFICATION-FINAL-CLOSURE-2026-06-18"

            Return New MASRenderVerificationGovernanceClosureManifest(
                status,
                name,
                manifestId,
                snapshot.TargetCount,
                snapshot.ScenarioCount,
                snapshot.ComponentKeyCount,
                snapshot.RegisteredSceneCount,
                CreateRequiredGateNames(),
                CreateEvidenceReportNames(),
                CreateOwnershipContracts(),
                ResolveSummary(status, snapshot))
        End Function

        Private Shared Function ResolveStatus(snapshot As MASRenderVerificationInventorySnapshot) As MASRenderVerificationGovernanceClosureStatus
            If snapshot.TargetCount <= 0 OrElse snapshot.ScenarioCount <= 0 OrElse snapshot.ComponentKeyCount <= 0 Then
                Return MASRenderVerificationGovernanceClosureStatus.IncompleteGovernance
            End If

            If snapshot.RegisteredSceneCount < snapshot.ComponentKeyCount Then
                Return MASRenderVerificationGovernanceClosureStatus.BlockedBySceneRegistry
            End If

            Return MASRenderVerificationGovernanceClosureStatus.Closed
        End Function

        Private Shared Function ResolveSummary(status As MASRenderVerificationGovernanceClosureStatus,
                                               snapshot As MASRenderVerificationInventorySnapshot) As String
            Select Case status
                Case MASRenderVerificationGovernanceClosureStatus.Closed
                    Return "Render Verification is closed: the catalog, registry, deterministic capture host, approval policy, surface material proof catch-up, dashboard gateway, and evidence reports form one owned chain."
                Case MASRenderVerificationGovernanceClosureStatus.BlockedBySceneRegistry
                    Return "Render Verification is blocked because one or more catalog component keys do not have registered deterministic capture scenes."
                Case MASRenderVerificationGovernanceClosureStatus.BlockedByEvidencePolicy
                    Return "Render Verification is blocked because the evidence or approval policy chain is not commercially acceptable."
                Case Else
                    Return "Render Verification is incomplete because required catalog or evidence-governance facts 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_RENDER_VERIFICATION_SYSTEM_PHASE1.md",
                "MAS_RENDER_VERIFICATION_APPROVAL_POLICY_HARDENING_PASS1.md",
                "MAS_RENDER_VERIFICATION_RUNTIME_PERFORMANCE_COMPLETION_FIX2.md",
                "MAS_RENDER_VERIFICATION_UNIFIED_TARGET_CATALOG_PASS2.md",
                "MAS_RENDER_VERIFICATION_GATE_HARDENING_PASS3.md",
                "MAS_RENDER_VERIFICATION_DETERMINISTIC_CAPTURE_HOST_PASS4.md",
                "MAS_RENDER_VERIFICATION_COVERAGE_MATRIX_EXPANSION_PASS5.md",
                "MAS_RENDER_VERIFICATION_REAL_RUNTIME_TESTS_PASS6.md",
                "MAS_RENDER_VERIFICATION_LOCKDOWN_PASS7.md",
                "MAS_RENDER_VERIFICATION_SYSTEM_PROOF_INVENTORY_SNAPSHOT.md",
                "MAS_RENDER_VERIFICATION_COMMERCIAL_DASHBOARD_GATEWAY.md",
                "MAS_RENDER_VERIFICATION_SURFACE_MATERIAL_PROOF_CATCHUP_2026_06_20.md",
                "MAS_RENDER_VERIFICATION_FINAL_CLOSURE_EVIDENCE_GOVERNANCE_2026_06_18.md"
            })
        End Function

        Private Shared Function CreateOwnershipContracts() As IReadOnlyList(Of String)
            Return New ReadOnlyCollection(Of String)(New List(Of String) From {
                "MASRenderVerificationTargetCatalog",
                "MASRenderVerificationCaptureSceneRegistry",
                "MASRenderVerificationCaptureHost",
                "MASRenderVerificationThemeResolver",
                "MASRenderVerificationGate",
                "MASRenderVerificationRunner",
                "MASRenderBaselineStore",
                "MASRenderImageComparer",
                "MASRenderDifferenceImageWriter",
                "MASRenderVerification.CreateSnapshot",
                "MASRenderVerification.AttachDashboard",
                "MASRenderVerificationInventorySnapshotBuilder",
                "MASRenderVerificationSurfaceMaterialProofAudit",
                "MASRenderVerificationSurfaceMaterialProofReport"
            })
        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
