Option Strict On
Option Explicit On

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

Namespace Nexamas.UI.Verification

    ''' <summary>
    ''' Internal report proving that every current VisualSurface runtime/public material consumer has
    ''' an official Render Verification scenario and registered capture scene.
    ''' </summary>
    ''' <remarks>
    ''' This report is evidence-only. It does not run Render Verification, compare pixels, approve baselines,
    ''' allocate Skia objects, or expose a public proof API.
    ''' </remarks>
    Friend NotInheritable Class MASRenderVerificationSurfaceMaterialProofReport
        Private ReadOnly _entries As IReadOnlyList(Of MASRenderVerificationSurfaceMaterialProofEntry)
        Private ReadOnly _requiredGateNames As IReadOnlyList(Of String)
        Private ReadOnly _evidenceReportNames As IReadOnlyList(Of String)

        Friend Sub New(status As MASRenderVerificationSurfaceMaterialProofStatus,
                       entries As IEnumerable(Of MASRenderVerificationSurfaceMaterialProofEntry),
                       requiredGateNames As IEnumerable(Of String),
                       evidenceReportNames As IEnumerable(Of String),
                       expectedRuntimeSurfaceVisualConsumerCount As Integer,
                       expectedPublicGatewayMemberCount As Integer,
                       notes As String)
            Me.Status = status
            _entries = New ReadOnlyCollection(Of MASRenderVerificationSurfaceMaterialProofEntry)(NormalizeEntries(entries))
            _requiredGateNames = New ReadOnlyCollection(Of String)(NormalizeList(requiredGateNames))
            _evidenceReportNames = New ReadOnlyCollection(Of String)(NormalizeList(evidenceReportNames))
            Me.ExpectedRuntimeSurfaceVisualConsumerCount = Math.Max(0, expectedRuntimeSurfaceVisualConsumerCount)
            Me.ExpectedPublicGatewayMemberCount = Math.Max(0, expectedPublicGatewayMemberCount)
            Me.Notes = Normalize(notes)
        End Sub

        Friend ReadOnly Property Status As MASRenderVerificationSurfaceMaterialProofStatus
        Friend ReadOnly Property ExpectedRuntimeSurfaceVisualConsumerCount As Integer
        Friend ReadOnly Property ExpectedPublicGatewayMemberCount As Integer
        Friend ReadOnly Property Notes As String

        Friend ReadOnly Property Entries As IReadOnlyList(Of MASRenderVerificationSurfaceMaterialProofEntry)
            Get
                Return _entries
            End Get
        End Property

        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 EntryCount As Integer
            Get
                Return Entries.Count
            End Get
        End Property

        Friend ReadOnly Property CompleteProofCount As Integer
            Get
                Dim count As Integer = 0
                For Each entry As MASRenderVerificationSurfaceMaterialProofEntry In Entries
                    If entry IsNot Nothing AndAlso entry.HasCurrentRenderVerificationProof Then count += 1
                Next
                Return count
            End Get
        End Property

        Friend ReadOnly Property ProofedPublicGatewayMemberCount As Integer
            Get
                Dim count As Integer = 0
                For Each entry As MASRenderVerificationSurfaceMaterialProofEntry In Entries
                    If entry IsNot Nothing AndAlso entry.HasPublicGatewayRouteMappingProof Then count += entry.PublicGatewayMemberCount
                Next
                Return count
            End Get
        End Property

        Friend ReadOnly Property MissingRuntimeConsumerProofCount As Integer
            Get
                Return Math.Max(0, ExpectedRuntimeSurfaceVisualConsumerCount - CompleteProofCount)
            End Get
        End Property

        Friend ReadOnly Property MissingPublicGatewayProofCount As Integer
            Get
                Return Math.Max(0, ExpectedPublicGatewayMemberCount - ProofedPublicGatewayMemberCount)
            End Get
        End Property

        Friend ReadOnly Property HasAllRuntimeSurfaceVisualConsumersProofed As Boolean
            Get
                Return ExpectedRuntimeSurfaceVisualConsumerCount > 0 AndAlso
                       EntryCount = ExpectedRuntimeSurfaceVisualConsumerCount AndAlso
                       CompleteProofCount = ExpectedRuntimeSurfaceVisualConsumerCount
            End Get
        End Property

        Friend ReadOnly Property HasAllPublicGatewayMembersProofed As Boolean
            Get
                Return ExpectedPublicGatewayMemberCount > 0 AndAlso
                       ProofedPublicGatewayMemberCount = ExpectedPublicGatewayMemberCount
            End Get
        End Property

        Friend ReadOnly Property HasAllPublicGatewayMembersRouteMapped As Boolean
            Get
                Return HasAllPublicGatewayMembersProofed
            End Get
        End Property

        Friend ReadOnly Property HasCompleteEvidenceChain As Boolean
            Get
                Return RequiredGateNames.Count >= 2 AndAlso
                       EvidenceReportNames.Count >= 2 AndAlso
                       HasAllRuntimeSurfaceVisualConsumersProofed AndAlso
                       HasAllPublicGatewayMembersProofed
            End Get
        End Property

        Friend ReadOnly Property IsComplete As Boolean
            Get
                Return Status = MASRenderVerificationSurfaceMaterialProofStatus.Complete AndAlso HasCompleteEvidenceChain
            End Get
        End Property

        Friend Function ContainsConsumer(consumerKind As MASSurfaceTreatmentConsumerKind) As Boolean
            For Each entry As MASRenderVerificationSurfaceMaterialProofEntry In Entries
                If entry IsNot Nothing AndAlso entry.ConsumerKind = consumerKind AndAlso entry.HasCurrentRenderVerificationProof Then Return True
            Next
            Return False
        End Function

        Private Shared Function NormalizeEntries(entries As IEnumerable(Of MASRenderVerificationSurfaceMaterialProofEntry)) As List(Of MASRenderVerificationSurfaceMaterialProofEntry)
            Dim result As New List(Of MASRenderVerificationSurfaceMaterialProofEntry)()
            If entries Is Nothing Then Return result

            For Each entry As MASRenderVerificationSurfaceMaterialProofEntry In entries
                If entry IsNot Nothing Then result.Add(entry)
            Next

            Return result
        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)
                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) As String
            Return If(value, String.Empty).Trim()
        End Function
    End Class

End Namespace
