Option Strict On
Option Explicit On

Imports System
Imports System.Collections.Generic
Imports Nexamas.UI.Rendering

Namespace Nexamas.UI.Verification

    ''' <summary>
    ''' Builds the current Render Verification proof catch-up report for VisualSurface material gateways.
    ''' </summary>
    ''' <remarks>
    ''' The audit reads existing catalog/manifest facts only. It does not capture, compare, approve, allocate bitmaps,
    ''' construct UI controls, scan the file system, or expose proof APIs.
    ''' </remarks>
    Friend NotInheritable Class MASRenderVerificationSurfaceMaterialProofAudit
        Private Sub New()
        End Sub

        Friend Shared Function BuildCurrent() As MASRenderVerificationSurfaceMaterialProofReport
            Dim manifest As MASPublicSurfaceMaterialGatewayManifest = MASPublicSurfaceMaterialGatewayManifest.CreateCurrent()
            Dim scenarios As IReadOnlyList(Of MASRenderVerificationScenario) = FlattenScenarios(MASRenderVerificationTargetCatalog.CreateAllTargets())
            Dim entries As New List(Of MASRenderVerificationSurfaceMaterialProofEntry)()

            For Each consumerKind As MASSurfaceTreatmentConsumerKind In MASSurfaceTreatmentConsumerCatalog.RuntimeSurfaceVisualConsumers()
                Dim scenarioId As String = ResolveScenarioId(consumerKind)
                Dim scenario As MASRenderVerificationScenario = FindScenarioById(scenarios, scenarioId)
                Dim componentKey As String = If(scenario Is Nothing, String.Empty, scenario.ComponentKey)
                Dim stateKey As String = If(scenario Is Nothing, ResolveStateKey(consumerKind), scenario.StateKey)
                Dim publicGatewayCount As Integer = manifest.CountForConsumer(consumerKind)
                Dim materialKey As String = MASSurfaceMaterialIds.CrystalIce
                Dim resolution As MASSurfaceTreatmentResolution = MASSurfaceTreatmentRuntimeSurfaceVisual.ResolveRuntimeResolution(
                    consumerKind:=consumerKind,
                    requestedMaterialKey:=materialKey,
                    requestedFrameStrength:=MASSurfaceFrameStrength.Level3,
                    hasExplicitMaterialOverride:=True)

                Dim hasResolution As Boolean = resolution IsNot Nothing AndAlso
                                               Not String.IsNullOrWhiteSpace(resolution.ResolvedMaterialKey) AndAlso
                                               (resolution.IsCompatibleForCurrentRuntime OrElse
                                                resolution.IsReadyForCurrentRuntimeConsumption OrElse
                                                resolution.CompatibilityStatus = MASSurfaceTreatmentCompatibilityStatus.ResolvedWithMaterialFallback)

                entries.Add(New MASRenderVerificationSurfaceMaterialProofEntry(
                    consumerKind:=consumerKind,
                    consumerDisplayName:=MASSurfaceTreatmentConsumerCatalog.ConsumerDisplayName(consumerKind),
                    scenarioId:=scenarioId,
                    componentKey:=componentKey,
                    stateKey:=stateKey,
                    materialKey:=materialKey,
                    publicGatewayMemberCount:=publicGatewayCount,
                    hasRuntimeSurfaceVisualConsumer:=MASSurfaceTreatmentConsumerCatalog.IsRuntimeSurfaceVisualConsumer(consumerKind),
                    hasPublicSurfaceGateway:=MASSurfaceTreatmentConsumerCatalog.IsPublicSurfaceGateway(consumerKind),
                    hasCatalogScenario:=scenario IsNot Nothing,
                    hasRegisteredCaptureScene:=scenario IsNot Nothing AndAlso MASRenderVerificationShowcaseCapture.HasRegisteredScene(componentKey),
                    hasExplicitMaterialResolution:=hasResolution))
            Next

            Dim status As MASRenderVerificationSurfaceMaterialProofStatus = ResolveStatus(entries, manifest)
            Return New MASRenderVerificationSurfaceMaterialProofReport(
                status:=status,
                entries:=entries,
                requiredGateNames:=CreateRequiredGateNames(),
                evidenceReportNames:=CreateEvidenceReportNames(),
                expectedRuntimeSurfaceVisualConsumerCount:=MASSurfaceTreatmentConsumerCatalog.RuntimeSurfaceVisualConsumerCount,
                expectedPublicGatewayMemberCount:=MASPublicSurfaceMaterialGatewayManifest.ExpectedGatewayMemberCount,
                notes:="Every runtime surface visual consumer maps to an official Render Verification scenario using the single Surface Visual runtime route; public gateway members are manifest-mapped to those consumer routes rather than represented as one scene per member.")
        End Function

        Private Shared Function ResolveStatus(entries As IReadOnlyList(Of MASRenderVerificationSurfaceMaterialProofEntry),
                                              manifest As MASPublicSurfaceMaterialGatewayManifest) As MASRenderVerificationSurfaceMaterialProofStatus
            If entries Is Nothing OrElse entries.Count = 0 OrElse manifest Is Nothing Then
                Return MASRenderVerificationSurfaceMaterialProofStatus.IncompleteGovernance
            End If

            Dim completeCount As Integer = 0
            Dim publicGatewayProofCount As Integer = 0
            For Each entry As MASRenderVerificationSurfaceMaterialProofEntry In entries
                If entry Is Nothing Then Continue For
                If Not entry.HasCatalogScenario OrElse Not entry.HasRegisteredCaptureScene Then
                    Return MASRenderVerificationSurfaceMaterialProofStatus.BlockedBySceneRegistry
                End If
                If entry.HasCurrentRenderVerificationProof Then
                    completeCount += 1
                End If
                If entry.HasPublicGatewayRouteMappingProof Then
                    publicGatewayProofCount += entry.PublicGatewayMemberCount
                End If
            Next

            If completeCount <> MASSurfaceTreatmentConsumerCatalog.RuntimeSurfaceVisualConsumerCount Then
                Return MASRenderVerificationSurfaceMaterialProofStatus.BlockedByMissingRuntimeConsumerProof
            End If

            If publicGatewayProofCount <> manifest.EntryCount Then
                Return MASRenderVerificationSurfaceMaterialProofStatus.BlockedByMissingPublicGatewayProof
            End If

            Return MASRenderVerificationSurfaceMaterialProofStatus.Complete
        End Function

        Private Shared Function FlattenScenarios(targets As IReadOnlyList(Of MASRenderVerificationTarget)) As IReadOnlyList(Of MASRenderVerificationScenario)
            Dim result As New List(Of MASRenderVerificationScenario)()
            If targets Is Nothing Then Return result

            For Each target As MASRenderVerificationTarget In targets
                If target Is Nothing OrElse target.Scenarios Is Nothing Then Continue For
                For Each scenario As MASRenderVerificationScenario In target.Scenarios
                    If scenario IsNot Nothing Then result.Add(scenario)
                Next
            Next

            Return result
        End Function

        Private Shared Function FindScenarioById(scenarios As IReadOnlyList(Of MASRenderVerificationScenario),
                                                 scenarioId As String) As MASRenderVerificationScenario
            If scenarios Is Nothing Then Return Nothing
            For Each scenario As MASRenderVerificationScenario In scenarios
                If scenario IsNot Nothing AndAlso String.Equals(scenario.Id, scenarioId, StringComparison.OrdinalIgnoreCase) Then Return scenario
            Next
            Return Nothing
        End Function

        Private Shared Function ResolveScenarioId(consumerKind As MASSurfaceTreatmentConsumerKind) As String
            Return "showcase-surface-material-" & ResolveStateKey(consumerKind)
        End Function

        Private Shared Function ResolveStateKey(consumerKind As MASSurfaceTreatmentConsumerKind) As String
            Select Case consumerKind
                Case MASSurfaceTreatmentConsumerKind.BackgroundSurface
                    Return "background-crystal"
                Case MASSurfaceTreatmentConsumerKind.ApplicationChromeSurface
                    Return "application-chrome-crystal"
                Case MASSurfaceTreatmentConsumerKind.PanelShellSurface
                    Return "panel-shell-crystal"
                Case MASSurfaceTreatmentConsumerKind.ProgressSurface
                    Return "progress-crystal"
                Case MASSurfaceTreatmentConsumerKind.CardSurface
                    Return "card-crystal"
                Case MASSurfaceTreatmentConsumerKind.ToastSurface
                    Return "toast-crystal"
                Case MASSurfaceTreatmentConsumerKind.TooltipSurface
                    Return "tooltip-crystal"
                Case MASSurfaceTreatmentConsumerKind.TopBarSurface
                    Return "topbar-crystal"
                Case MASSurfaceTreatmentConsumerKind.ToolbarSurface
                    Return "toolbar-crystal"
                Case MASSurfaceTreatmentConsumerKind.GroupBoxSurface
                    Return "groupbox-crystal"
                Case Else
                    Return "unknown-crystal"
            End Select
        End Function

        Private Shared Function CreateRequiredGateNames() As IReadOnlyList(Of String)
            Return New String() {
                "tools/quality/Run-NexamasUIQuality.ps1",
                "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 String() {
                "MAS_RENDER_VERIFICATION_SURFACE_MATERIAL_PROOF_CATCHUP_2026_06_20.md",
                "MAS_PUBLIC_SURFACE_MATERIAL_GATEWAY_TRUTH_CLOSURE_2026_06_20.md",
                "MAS_SURFACE_TREATMENT_RUNTIME_MATERIAL_APPLICATION_2026_06_20.md",
                "MAS_RENDER_VERIFICATION_FINAL_CLOSURE_EVIDENCE_GOVERNANCE_2026_06_18.md"
            }
        End Function
    End Class

End Namespace
