Option Strict On
Option Explicit On

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

Namespace Nexamas.UI.Quality

    ''' <summary>
    ''' Central friend-only policy for product-control adapter exposure. It keeps the
    ''' internal source/projection work done for previews from becoming accidental
    ''' public API before write-back, validation, batching, docs, and measured stress
    ''' evidence exist.
    ''' </summary>
    Friend NotInheritable Class MASPublicAdapterPolicyManifest

        Private Const ExpectedProductControlCount As Integer = 8
        Private ReadOnly _records As IReadOnlyList(Of MASPublicAdapterPolicyRecord)

        Private Sub New(records As IEnumerable(Of MASPublicAdapterPolicyRecord))
            Dim normalized As New List(Of MASPublicAdapterPolicyRecord)()
            If records IsNot Nothing Then
                For Each record As MASPublicAdapterPolicyRecord In records
                    If record IsNot Nothing Then normalized.Add(record)
                Next
            End If
            _records = New ReadOnlyCollection(Of MASPublicAdapterPolicyRecord)(normalized.ToArray())
        End Sub

        Friend ReadOnly Property Records As IReadOnlyList(Of MASPublicAdapterPolicyRecord)
            Get
                Return _records
            End Get
        End Property

        Friend ReadOnly Property Count As Integer
            Get
                Return _records.Count
            End Get
        End Property

        Friend ReadOnly Property HasExpectedProductControlCoverage As Boolean
            Get
                Return Count = ExpectedProductControlCount AndAlso
                       FindById("treegrid") IsNot Nothing AndAlso
                       FindById("pivottable") IsNot Nothing AndAlso
                       FindById("kanban") IsNot Nothing AndAlso
                       FindById("agenda") IsNot Nothing AndAlso
                       FindById("dashboardgrid") IsNot Nothing AndAlso
                       FindById("masterdetail") IsNot Nothing AndAlso
                       FindById("applayout") IsNot Nothing AndAlso
                       FindById("splitview") IsNot Nothing
            End Get
        End Property

        Friend Function FindById(componentId As String) As MASPublicAdapterPolicyRecord
            Dim normalized As String = If(componentId, String.Empty).Trim()
            If normalized.Length = 0 Then Return Nothing

            For Each record As MASPublicAdapterPolicyRecord In _records
                If record IsNot Nothing AndAlso String.Equals(record.ComponentId, normalized, System.StringComparison.OrdinalIgnoreCase) Then
                    Return record
                End If
            Next

            Return Nothing
        End Function

        Friend Shared Function CreateDefault() As MASPublicAdapterPolicyManifest
            Dim records As New List(Of MASPublicAdapterPolicyRecord)()

            records.Add(CreateNoPublicAdapterRequiredRecord("treegrid", "TreeGrid", "TreeGrid is promoted as a public visual hierarchy-with-columns control. Its production boundary is the public AddTreeGrid surface plus public row/column/selection/editing members; Friend-only source projection, source write-back, incremental delta handling, and stress proofs remain internal, and no public external provider/database adapter is required for ProductionReady promotion."))
            records.Add(CreateNoPublicAdapterRequiredRecord("pivottable", "PivotTable", "PivotTable is promoted as a public visual cross-tab summary control. Its production boundary is the public AddPivotTable/manual row-column-cell surface plus Friend-only pivot source projection, grouping policy, drilldown/export snapshot, and in-memory export writer proof; no public external provider/database/OLAP/Excel adapter is required for ProductionReady promotion."))
            records.Add(CreateNoPublicAdapterRequiredRecord("kanban", "KanbanBoard", "KanbanBoard is promoted as a public visual workflow-board control. Its production boundary is the public AddKanbanBoard surface plus public column/card/snapshot/reorder members; Friend-only source projection, drop-rule diagnostics, source-detach write-back proof, and stress gates remain internal, and no public provider/database/write-back adapter is required for ProductionReady promotion."))
            records.Add(CreateNoPublicAdapterRequiredRecord("agenda", "AgendaView", "AgendaView is promoted as a public visual agenda/range surface. Its production boundary is the public AddAgendaView/manual event surface plus Friend-only source projection, recurrence/time-zone visual boundary proof, event move/resize contract proof, and bounded range-window evidence; no public external calendar provider, recurrence engine, booking database, scheduler engine, reminder transport, or write-back adapter is required for ProductionReady promotion."))
            records.Add(CreateNoPublicAdapterRequiredRecord("dashboardgrid", "DashboardGrid", "DashboardGrid is a visual dashboard/widget layout and hosting control. Its production boundary is the public AddDashboardGrid surface plus public layout snapshot/reorder/resize members; Friend-only provider refresh/adoption proofs remain internal and no public provider/write-back adapter is required for ProductionReady promotion."))
            records.Add(CreateNoPublicAdapterRequiredRecord("masterdetail", "MasterDetailView", "MasterDetailView is promoted as a visual/source-projection master/detail control. Its production boundary is the public AddMasterDetailView surface plus public manual item and snapshot members; Friend-only source projection and routing-adapter proofs remain internal, and no public external write-back/routing adapter is required for ProductionReady promotion."))
            records.Add(CreateNoPublicAdapterRequiredRecord("applayout", "AppLayout", "AppLayout is a visual application-slot layout control. Its production boundary is the public AddAppLayout surface plus public layout snapshot members; Friend-only slot-host adoption proof remains internal and no public data/provider adapter is required for ProductionReady promotion."))
            records.Add(CreateNoPublicAdapterRequiredRecord("splitview", "SplitView", "SplitView is a visual hosting/layout control. Its production boundary is the public AddSplitView surface plus public pane state/snapshot members; Friend-only pane-host adoption proof remains internal and no public data/provider adapter is required for ProductionReady promotion."))

            Return New MASPublicAdapterPolicyManifest(records)
        End Function

        Private Shared Function CreateFriendOnlyDataAdapterRecord(componentId As String,
                                                                 displayName As String,
                                                                 blockerSummary As String) As MASPublicAdapterPolicyRecord
            Return New MASPublicAdapterPolicyRecord(
                componentId:=componentId,
                displayName:=displayName,
                exposureStatus:=MASPublicAdapterExposureStatus.FriendOnlyPrototype,
                hasInternalProjectionPath:=True,
                hasPublicAdapterContract:=False,
                requiresPublicAdapterForProduction:=True,
                hasWriteBackContract:=False,
                hasValidationPipeline:=False,
                hasBatchUpdatePolicy:=False,
                hasAdapterDocsAndSamples:=False,
                hasMeasuredAdapterStressProof:=False,
                blockerSummary:=blockerSummary)
        End Function

        Private Shared Function CreateNoPublicAdapterRequiredRecord(componentId As String,
                                                                    displayName As String,
                                                                    boundarySummary As String) As MASPublicAdapterPolicyRecord
            Return New MASPublicAdapterPolicyRecord(
                componentId:=componentId,
                displayName:=displayName,
                exposureStatus:=MASPublicAdapterExposureStatus.NotApplicable,
                hasInternalProjectionPath:=True,
                hasPublicAdapterContract:=False,
                requiresPublicAdapterForProduction:=False,
                hasWriteBackContract:=False,
                hasValidationPipeline:=False,
                hasBatchUpdatePolicy:=False,
                hasAdapterDocsAndSamples:=False,
                hasMeasuredAdapterStressProof:=False,
                blockerSummary:=boundarySummary)
        End Function

    End Class

End Namespace
