Option Strict On
Option Explicit On

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

Namespace Nexamas.UI.Rendering

    ''' <summary>
    ''' Commercial closure manifest for the Visual system ownership refactor.
    ''' It summarizes the existing Visual ownership lockdown report without drawing,
    ''' loading themes, touching controls, or changing runtime visuals.
    ''' </summary>
    Friend NotInheritable Class MASVisualCommercialGovernanceClosureManifest
        Private ReadOnly _ownershipContracts As IReadOnlyList(Of String)
        Private ReadOnly _evidenceReports As IReadOnlyList(Of String)

        Friend Sub New(name As String,
                       ownershipReport As MASVisualOwnershipLockdownReport,
                       ownershipContracts As IEnumerable(Of String),
                       evidenceReports As IEnumerable(Of String))
            Me.Name = Normalize(name, "Nexamas UI Visual Commercial Governance Closure")
            Me.OwnershipReport = ownershipReport
            _ownershipContracts = New ReadOnlyCollection(Of String)(NormalizeList(ownershipContracts))
            _evidenceReports = New ReadOnlyCollection(Of String)(NormalizeList(evidenceReports))
        End Sub

        Friend ReadOnly Property Name As String
        Friend ReadOnly Property OwnershipReport As MASVisualOwnershipLockdownReport

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

        Friend ReadOnly Property EvidenceReports As IReadOnlyList(Of String)
            Get
                Return _evidenceReports
            End Get
        End Property

        Friend ReadOnly Property HasOwnershipReport As Boolean
            Get
                Return OwnershipReport IsNot Nothing
            End Get
        End Property

        Friend ReadOnly Property HasCompleteAreaCoverage As Boolean
            Get
                Return HasOwnershipReport AndAlso
                       OwnershipReport.EntryCount = MASVisualOwnershipLockdownReport.ExpectedAreaCount() AndAlso
                       OwnershipReport.ReadyAreaCount = OwnershipReport.EntryCount AndAlso
                       OwnershipReport.Contains(MASVisualOwnershipArea.Surface) AndAlso
                       OwnershipReport.Contains(MASVisualOwnershipArea.Shadow) AndAlso
                       OwnershipReport.Contains(MASVisualOwnershipArea.Effects) AndAlso
                       OwnershipReport.Contains(MASVisualOwnershipArea.Interaction) AndAlso
                       OwnershipReport.Contains(MASVisualOwnershipArea.ComponentChrome) AndAlso
                       OwnershipReport.Contains(MASVisualOwnershipArea.VisualPrimitives) AndAlso
                       OwnershipReport.Contains(MASVisualOwnershipArea.SharedTokens) AndAlso
                       OwnershipReport.Contains(MASVisualOwnershipArea.ThemeBoundary)
            End Get
        End Property

        Friend ReadOnly Property PreservesCurrentVisuals As Boolean
            Get
                Return HasOwnershipReport AndAlso OwnershipReport.PreservesCurrentVisuals
            End Get
        End Property

        Friend ReadOnly Property LocksThemeRenderingBoundary As Boolean
            Get
                Return HasOwnershipReport AndAlso OwnershipReport.ThemeRenderingBoundaryLocked
            End Get
        End Property

        Friend ReadOnly Property BlocksParallelVisualRoutes As Boolean
            Get
                Return HasOwnershipReport AndAlso OwnershipReport.BlocksParallelRoutes
            End Get
        End Property

        Friend ReadOnly Property HasCommercialEvidence As Boolean
            Get
                Return OwnershipContracts.Count >= 8 AndAlso
                       EvidenceReports.Count >= 3 AndAlso
                       ContainsContract("MASVisualOwnershipLockdownAudit") AndAlso
                       ContainsContract("MASVisualCommercialGovernanceClosureManifest") AndAlso
                       ContainsContract("MASVisualMaterialMigrationClosureAudit") AndAlso
                       ContainsContract("MASVisualComponentSurfaceMaterialAdoptionAudit") AndAlso
                       ContainsContract("MASVisualComponentShadowOwnershipAudit") AndAlso
                       ContainsContract("MASVisualSurfaceMaterialSwitchReadinessAudit") AndAlso
                       ContainsContract("MASVisualSurfaceMaterialCoverageAudit") AndAlso
                       ContainsContract("MASVisualMaterialRegionCompositionAudit") AndAlso
                       ContainsContract("MASSurfaceMaterialIntegrityAudit") AndAlso
                       ContainsContract("MASVisualFolderHygieneAudit") AndAlso
                       ContainsEvidenceReport("Nexamas UI source-contract visual ownership gates") AndAlso
                       ContainsEvidenceReport("Material migration final closure") AndAlso
                       ContainsEvidenceReport("Component surface material adoption") AndAlso
                       ContainsEvidenceReport("Component shadow ownership adoption") AndAlso
                       ContainsEvidenceReport("Surface material switch readiness") AndAlso
                       ContainsEvidenceReport("Surface material component coverage") AndAlso
                       ContainsEvidenceReport("Material region composition authoring") AndAlso
                       ContainsEvidenceReport("Frame-backed surface body material routing") AndAlso
                       ContainsEvidenceReport("Surface material integrity") AndAlso
                       ContainsEvidenceReport("Visual folder hygiene closure")
            End Get
        End Property

        Friend ReadOnly Property IsClosed As Boolean
            Get
                Return HasCompleteAreaCoverage AndAlso
                       PreservesCurrentVisuals AndAlso
                       LocksThemeRenderingBoundary AndAlso
                       BlocksParallelVisualRoutes AndAlso
                       HasCommercialEvidence AndAlso
                       OwnershipReport.IsLockedDown
            End Get
        End Property

        Friend Shared Function CreateCurrent() As MASVisualCommercialGovernanceClosureManifest
            Return New MASVisualCommercialGovernanceClosureManifest(
                name:="Nexamas UI Visual Commercial Governance Closure",
                ownershipReport:=MASVisualOwnershipLockdownAudit.BuildCurrentVisualOwnershipLockdown(),
                ownershipContracts:=CreateOwnershipContracts(),
                evidenceReports:=CreateEvidenceReports())
        End Function

        Private Function ContainsContract(value As String) As Boolean
            Return Contains(OwnershipContracts, value)
        End Function

        Private Function ContainsEvidenceReport(value As String) As Boolean
            Return Contains(EvidenceReports, value)
        End Function

        Private Shared Function Contains(values As IReadOnlyList(Of String), value As String) As Boolean
            Dim normalizedValue As String = Normalize(value, String.Empty)
            If normalizedValue.Length = 0 Then Return False

            For Each candidate As String In values
                If String.Equals(candidate, normalizedValue, StringComparison.Ordinal) Then Return True
            Next

            Return False
        End Function

        Private Shared Function CreateOwnershipContracts() As IReadOnlyList(Of String)
            Return New ReadOnlyCollection(Of String)(New List(Of String) From {
                "MASVisualOwnershipLockdownAudit",
                "MASVisualOwnershipLockdownReport",
                "MASVisualCommercialGovernanceClosureManifest",
                "MASVisualMaterialMigrationClosureAudit",
                "MASVisualMaterialMigrationClosureReport",
                "MASVisualComponentSurfaceMaterialAdoptionAudit",
                "MASVisualComponentSurfaceMaterialAdoptionReport",
                "MASVisualComponentShadowOwnershipAudit",
                "MASVisualComponentShadowOwnershipReport",
                "MASVisualSurfaceMaterialSwitchReadinessAudit",
                "MASVisualSurfaceMaterialSwitchReadinessReport",
                "MASVisualSurfaceMaterialCoverageAudit",
                "MASVisualSurfaceMaterialCoverageReport",
                "MASVisualMaterialRegionCompositionAudit",
                "MASVisualMaterialRegionCompositionReport",
                "MASVisualFolderHygieneAudit",
                "MASVisualFolderHygieneReport",
                "MASSurfaceMaterialIntegrityAudit",
                "MASSurfaceMaterialIntegrityReport",
                "MASSurfaceTreatmentRuntimeSurfaceVisual",
                "MASSurfaceTreatmentRuntimeMaterialBody",
                "MASSurfaceMaterialComponentGateway",
                "MASComponentSurfaceMaterialComposition",
                "MASComponentSurfaceMaterialBinding",
                "MASSurfaceMaterialRuntimeSwitch",
                "MASApplicationSurfaceMaterialGateway",
                "MASApplicationSurfaceMaterialScope",
                "MASSurfaceFrameBodyMaterialRenderer",
                "MASVisualComponentShadowOwnershipAudit",
                "MASShadowPainter",
                "MASAcrylicEffectPainter",
                "MASActionChromePainter",
                "MASInteractionStateVisualResolver",
                "MASVisualPrimitivesPainter"
            })
        End Function

        Private Shared Function CreateEvidenceReports() As IReadOnlyList(Of String)
            Return New ReadOnlyCollection(Of String)(New List(Of String) From {
                "Nexamas UI source-contract visual ownership gates",
                "Nexamas UI Visual Ownership Lockdown",
                "Surface treatment consumer proof alignment",
                "Material migration final closure",
                "Component surface material adoption",
                "Component shadow ownership adoption",
                "Surface material switch readiness",
                "Surface material component coverage",
                "Material region composition authoring",
                "Frame-backed surface body material routing",
                "Surface material integrity",
                "Visual folder hygiene closure"
            })
        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 = Normalize(value, String.Empty)
                If normalized.Length > 0 AndAlso Not result.Contains(normalized) Then result.Add(normalized)
            Next

            Return result
        End Function

        Private Shared Function Normalize(value As String, fallback As String) As String
            Dim result As String = If(value, String.Empty).Trim()
            If result.Length = 0 Then Return fallback
            Return result
        End Function
    End Class

End Namespace
