Option Strict On
Option Explicit On

Namespace Nexamas.UI.Rendering

    ''' <summary>
    ''' Builds the final VisualSurface consumer/proof alignment evidence.
    ''' This audit reads already-built reports only; it does not draw, instantiate controls,
    ''' run captures, mutate registries, create public API, or alter runtime surface visual behavior.
    ''' </summary>
    Friend NotInheritable Class MASSurfaceTreatmentConsumerProofAlignmentAudit

        Private Sub New()
        End Sub

        Friend Shared Function BuildFinalConsumerProofAlignment() As MASSurfaceTreatmentConsumerProofAlignmentReport
            Dim inventory As MASSurfaceTreatmentOwnershipReport = MASSurfaceTreatmentOwnershipInventoryBuilder.BuildCurrentSurfaceTreatmentInventory()
            Dim capture As MASSurfaceTreatmentCurrentVisualCaptureReport = MASSurfaceTreatmentCurrentVisualCaptureBuilder.BuildLowRiskCurrentVisualCapture()
            Dim runtime As MASSurfaceTreatmentRuntimeAlignmentReport = MASSurfaceTreatmentRuntimeAlignmentBuilder.BuildLowRiskRuntimeAlignment()
            Dim runtimeApplication As MASSurfaceTreatmentRuntimeSurfaceVisualApplicationReport = MASSurfaceTreatmentRuntimeSurfaceVisualApplicationBuilder.BuildRuntimeSurfaceVisualApplicationReadiness()
            Dim frameProof As MASSurfaceTreatmentFrameRuntimeProofPlanningReport = MASSurfaceTreatmentFrameRuntimeProofPlanningBuilder.BuildFrameRuntimeProofPlanning()
            Dim scope As MASSurfaceTreatmentScopeBoundaryReport = MASSurfaceTreatmentScopeBoundaryAudit.BuildSurfaceOnlyScopeBoundary()
            Dim materialProfiles As MASSurfaceTreatmentMaterialProfile() = MASSurfaceTreatmentMaterialProfileCatalog.RegisteredProfiles()

            Dim expectedOwnershipCount As Integer = MASSurfaceTreatmentConsumerCatalog.CurrentOwnershipConsumerCount
            Dim expectedCaptureCount As Integer = MASSurfaceTreatmentConsumerCatalog.CurrentVisualCaptureConsumerCount
            Dim expectedRuntimeCount As Integer = MASSurfaceTreatmentConsumerCatalog.LowRiskRuntimeAlignedConsumerCount
            Dim expectedRuntimeSurfaceVisualCount As Integer = MASSurfaceTreatmentConsumerCatalog.RuntimeSurfaceVisualConsumerCount
            Dim expectedFrameConsumerCount As Integer = MASSurfaceTreatmentConsumerCatalog.FrameRuntimeProofConsumerCount
            Dim materialProfileCount As Integer = If(materialProfiles Is Nothing, 0, materialProfiles.Length)
            Dim expectedFramePlanCount As Integer = MASSurfaceTreatmentConsumerCatalog.ExpectedFrameRuntimeProofPlanCount(materialProfileCount)

            Dim inventoryMatchesCatalog As Boolean = inventory IsNot Nothing AndAlso
                                                    inventory.EntryCount = expectedOwnershipCount AndAlso
                                                    inventory.HasCompleteOwnershipInventory AndAlso
                                                    ContainsAllInventoryConsumers(inventory)
            Dim captureMatchesCatalog As Boolean = capture IsNot Nothing AndAlso
                                                  capture.EntryCount = expectedCaptureCount AndAlso
                                                  capture.LowRiskCapturedCount = expectedCaptureCount AndAlso
                                                  capture.ReadyForLowRiskRuntimeAlignmentPlanning AndAlso
                                                  ContainsAllCaptureConsumers(capture)
            Dim runtimeMatchesCatalog As Boolean = runtime IsNot Nothing AndAlso
                                                  runtime.EntryCount = expectedRuntimeCount AndAlso
                                                  runtime.LowRiskAlignedCount = expectedRuntimeCount AndAlso
                                                  runtime.ReadyForLowRiskFramePairingPlanning AndAlso
                                                  ContainsAllRuntimeAlignmentConsumers(runtime)
            Dim runtimeApplicationMatchesCatalog As Boolean = runtimeApplication IsNot Nothing AndAlso
                                                             runtimeApplication.EntryCount = expectedRuntimeSurfaceVisualCount AndAlso
                                                             runtimeApplication.ReadySurfaceCount = expectedRuntimeSurfaceVisualCount AndAlso
                                                             runtimeApplication.ReadyForSurfaceVisualMaterialSelection AndAlso
                                                             ContainsAllRuntimeSurfaceVisualConsumers(runtimeApplication)
            Dim frameProofMatchesCatalog As Boolean = frameProof IsNot Nothing AndAlso
                                                      frameProof.EntryCount = expectedFramePlanCount AndAlso
                                                      frameProof.LowRiskFrameRuntimeProofPlanCount = expectedFramePlanCount AndAlso
                                                      frameProof.MaterialProfileCoverageCount = materialProfileCount AndAlso
                                                      frameProof.ReadyForFutureFrameRuntimeProofPlanning AndAlso
                                                      ContainsAllFrameProofConsumers(frameProof)
            Dim surfaceOnlyScopeLocked As Boolean = scope IsNot Nothing AndAlso
                                                    scope.IsSurfaceOnlyScopeLocked AndAlso
                                                    scope.InventoryEntryCount = expectedOwnershipCount AndAlso
                                                    scope.CapturedLowRiskSurfaceCount = expectedCaptureCount AndAlso
                                                    scope.RuntimeAlignedSurfaceCount = expectedRuntimeCount AndAlso
                                                    scope.FrameRuntimeProofPlanCount = expectedFramePlanCount
            Dim excludesControls As Boolean = inventoryMatchesCatalog AndAlso
                                              capture IsNot Nothing AndAlso capture.ExcludesInteractiveControls AndAlso
                                              runtime IsNot Nothing AndAlso runtime.ExcludesInteractiveControls AndAlso
                                              runtimeApplication IsNot Nothing AndAlso runtimeApplication.ExcludesInteractiveControls AndAlso
                                              frameProof IsNot Nothing AndAlso frameProof.ExcludesInteractiveControls AndAlso
                                              scope IsNot Nothing AndAlso scope.KeepsInteractiveControlsOutOfSurfaceTreatment AndAlso
                                              MASSurfaceTreatmentConsumerCatalog.InteractiveControlsAreOutOfScope
            Dim usesSingleCatalog As Boolean = expectedOwnershipCount > 0 AndAlso
                                               expectedCaptureCount > 0 AndAlso
                                               expectedRuntimeCount = expectedCaptureCount AndAlso
                                               expectedFrameConsumerCount = expectedRuntimeCount AndAlso
                                               expectedRuntimeSurfaceVisualCount >= expectedOwnershipCount
            Dim noParallelPath As Boolean = scope IsNot Nothing AndAlso scope.HasNoParallelMaterialOrFramePath

            Dim status As MASSurfaceTreatmentConsumerProofAlignmentStatus = DetermineStatus(
                inventoryMatchesCatalog,
                captureMatchesCatalog,
                runtimeMatchesCatalog,
                runtimeApplicationMatchesCatalog,
                frameProofMatchesCatalog,
                surfaceOnlyScopeLocked)

            Return New MASSurfaceTreatmentConsumerProofAlignmentReport(
                name:="Surface Treatment Final Consumer/Proof Alignment",
                status:=status,
                catalogOwnershipConsumerCount:=expectedOwnershipCount,
                catalogCurrentVisualCaptureConsumerCount:=expectedCaptureCount,
                catalogLowRiskRuntimeConsumerCount:=expectedRuntimeCount,
                catalogRuntimeSurfaceVisualConsumerCount:=expectedRuntimeSurfaceVisualCount,
                catalogFrameRuntimeProofConsumerCount:=expectedFrameConsumerCount,
                materialProfileCount:=materialProfileCount,
                expectedFrameRuntimeProofPlanCount:=expectedFramePlanCount,
                inventoryEntryCount:=If(inventory Is Nothing, 0, inventory.EntryCount),
                currentVisualCaptureEntryCount:=If(capture Is Nothing, 0, capture.EntryCount),
                runtimeAlignmentEntryCount:=If(runtime Is Nothing, 0, runtime.EntryCount),
                runtimeSurfaceVisualApplicationEntryCount:=If(runtimeApplication Is Nothing, 0, runtimeApplication.EntryCount),
                frameRuntimeProofPlanCount:=If(frameProof Is Nothing, 0, frameProof.EntryCount),
                inventoryMatchesCatalog:=inventoryMatchesCatalog,
                currentVisualCaptureMatchesCatalog:=captureMatchesCatalog,
                runtimeAlignmentMatchesCatalog:=runtimeMatchesCatalog,
                runtimeSurfaceVisualApplicationMatchesCatalog:=runtimeApplicationMatchesCatalog,
                frameRuntimeProofMatchesCatalog:=frameProofMatchesCatalog,
                surfaceOnlyScopeLocked:=surfaceOnlyScopeLocked,
                usesSingleConsumerCatalog:=usesSingleCatalog,
                excludesInteractiveControls:=excludesControls,
                hasNoParallelMaterialOrFramePath:=noParallelPath)
        End Function

        Private Shared Function DetermineStatus(inventoryMatchesCatalog As Boolean,
                                                captureMatchesCatalog As Boolean,
                                                runtimeMatchesCatalog As Boolean,
                                                runtimeApplicationMatchesCatalog As Boolean,
                                                frameProofMatchesCatalog As Boolean,
                                                surfaceOnlyScopeLocked As Boolean) As MASSurfaceTreatmentConsumerProofAlignmentStatus
            If Not inventoryMatchesCatalog Then Return MASSurfaceTreatmentConsumerProofAlignmentStatus.BlockedByOwnershipInventoryDrift
            If Not captureMatchesCatalog Then Return MASSurfaceTreatmentConsumerProofAlignmentStatus.BlockedByCurrentVisualCaptureDrift
            If Not runtimeMatchesCatalog Then Return MASSurfaceTreatmentConsumerProofAlignmentStatus.BlockedByRuntimeAlignmentDrift
            If Not runtimeApplicationMatchesCatalog Then Return MASSurfaceTreatmentConsumerProofAlignmentStatus.BlockedByRuntimeSurfaceVisualApplicationDrift
            If Not frameProofMatchesCatalog Then Return MASSurfaceTreatmentConsumerProofAlignmentStatus.BlockedByFrameRuntimeProofDrift
            If Not surfaceOnlyScopeLocked Then Return MASSurfaceTreatmentConsumerProofAlignmentStatus.BlockedBySurfaceOnlyScopeDrift
            Return MASSurfaceTreatmentConsumerProofAlignmentStatus.Aligned
        End Function

        Private Shared Function ContainsAllInventoryConsumers(report As MASSurfaceTreatmentOwnershipReport) As Boolean
            If report Is Nothing Then Return False
            For Each consumerKind As MASSurfaceTreatmentConsumerKind In MASSurfaceTreatmentConsumerCatalog.CurrentOwnershipConsumers()
                If Not report.ContainsConsumerKind(consumerKind) Then Return False
            Next
            Return True
        End Function

        Private Shared Function ContainsAllCaptureConsumers(report As MASSurfaceTreatmentCurrentVisualCaptureReport) As Boolean
            If report Is Nothing Then Return False
            For Each consumerKind As MASSurfaceTreatmentConsumerKind In MASSurfaceTreatmentConsumerCatalog.CurrentVisualCaptureConsumers()
                If Not report.ContainsConsumerKind(consumerKind) Then Return False
            Next
            Return True
        End Function

        Private Shared Function ContainsAllRuntimeAlignmentConsumers(report As MASSurfaceTreatmentRuntimeAlignmentReport) As Boolean
            If report Is Nothing Then Return False
            For Each consumerKind As MASSurfaceTreatmentConsumerKind In MASSurfaceTreatmentConsumerCatalog.LowRiskRuntimeAlignedConsumers()
                If Not report.ContainsConsumerKind(consumerKind) Then Return False
            Next
            Return True
        End Function

        Private Shared Function ContainsAllRuntimeSurfaceVisualConsumers(report As MASSurfaceTreatmentRuntimeSurfaceVisualApplicationReport) As Boolean
            If report Is Nothing Then Return False
            For Each consumerKind As MASSurfaceTreatmentConsumerKind In MASSurfaceTreatmentConsumerCatalog.RuntimeSurfaceVisualConsumers()
                If Not report.Contains(consumerKind) Then Return False
            Next
            Return True
        End Function

        Private Shared Function ContainsAllFrameProofConsumers(report As MASSurfaceTreatmentFrameRuntimeProofPlanningReport) As Boolean
            If report Is Nothing Then Return False
            For Each consumerKind As MASSurfaceTreatmentConsumerKind In MASSurfaceTreatmentConsumerCatalog.FrameRuntimeProofConsumers()
                If Not report.ContainsLowRiskPlanFor(consumerKind) Then Return False
            Next
            Return True
        End Function

    End Class

End Namespace
