Option Strict On
Option Explicit On

Imports System
Imports System.Collections.Generic

Namespace Nexamas.UI.Quality

    ''' <summary>
    ''' Verifies that preview product controls have an explicit adapter exposure
    ''' policy and that no internal source/provider prototype is accidentally treated
    ''' as a public production contract.
    ''' </summary>
    Friend NotInheritable Class MASPublicAdapterPolicyGate

        Private Sub New()
        End Sub

        Friend Shared Function Passes() As Boolean
            Return EvaluateFindings().Count = 0
        End Function

        Friend Shared Function EvaluateFindings() As IReadOnlyList(Of MASCommercialReadinessFinding)
            Dim findings As New List(Of MASCommercialReadinessFinding)()
            Dim manifest As MASPublicAdapterPolicyManifest = Nothing

            Try
                manifest = MASPublicAdapterPolicyManifest.CreateDefault()
            Catch ex As Exception
                Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowDiagnosticOnly(ex, "MASPublicAdapterPolicyGate.CreateDefault")
                findings.Add(New MASCommercialReadinessFinding("adapter-policy", "Build", MASCommercialReadinessFindingSeverity.Error, "Public adapter policy manifest could not be built."))
                Return findings
            End Try

            If manifest Is Nothing Then
                findings.Add(New MASCommercialReadinessFinding("adapter-policy", "Manifest", MASCommercialReadinessFindingSeverity.Error, "Public adapter policy manifest is missing."))
                Return findings
            End If

            If Not manifest.HasExpectedProductControlCoverage Then
                findings.Add(New MASCommercialReadinessFinding("adapter-policy", "Coverage", MASCommercialReadinessFindingSeverity.Error, "Public adapter policy must cover exactly the eight product controls."))
            End If

            Dim ids As New HashSet(Of String)(StringComparer.OrdinalIgnoreCase)
            For Each record As MASPublicAdapterPolicyRecord In manifest.Records
                If record Is Nothing Then
                    findings.Add(New MASCommercialReadinessFinding("adapter-policy", "Record", MASCommercialReadinessFindingSeverity.Error, "Public adapter policy contains a null record."))
                    Continue For
                End If

                If Not ids.Add(record.ComponentId) Then
                    findings.Add(New MASCommercialReadinessFinding(record.ComponentId, "DuplicateId", MASCommercialReadinessFindingSeverity.Error, "Public adapter policy id is duplicated."))
                End If

                If Not record.IsStructurallyValid Then
                    findings.Add(New MASCommercialReadinessFinding(record.ComponentId, "Structure", MASCommercialReadinessFindingSeverity.Error, record.DisplayName & " public adapter policy record is structurally invalid."))
                End If

                If Not record.IsPublicAdapterPolicyReady Then
                    findings.Add(New MASCommercialReadinessFinding(record.ComponentId, "AdapterPolicy", MASCommercialReadinessFindingSeverity.Error, record.DisplayName & " public adapter policy is not internally consistent."))
                End If

                If record.ExposureStatus = MASPublicAdapterExposureStatus.FriendOnlyPrototype AndAlso record.HasPublicAdapterContract Then
                    findings.Add(New MASCommercialReadinessFinding(record.ComponentId, "AdapterExposure", MASCommercialReadinessFindingSeverity.Error, record.DisplayName & " is marked FriendOnlyPrototype but declares a public adapter contract."))
                End If

                If record.RequiresPublicAdapterForProduction AndAlso String.IsNullOrWhiteSpace(record.BlockerSummary) Then
                    findings.Add(New MASCommercialReadinessFinding(record.ComponentId, "AdapterBlockers", MASCommercialReadinessFindingSeverity.Error, record.DisplayName & " requires adapter blockers before production promotion."))
                End If
            Next

            Return findings
        End Function

        Friend Shared Function FindPolicy(componentId As String) As MASPublicAdapterPolicyRecord
            Dim manifest As MASPublicAdapterPolicyManifest = MASPublicAdapterPolicyManifest.CreateDefault()
            Return If(manifest Is Nothing, Nothing, manifest.FindById(componentId))
        End Function

    End Class

End Namespace
