Option Strict On
Option Explicit On

Imports System
Imports System.Collections.Generic
Imports Nexamas.UI.Diagnostics

Namespace Nexamas.UI.Application

    ''' <summary>
    ''' Official public, read-only diagnostics gateway for SDK consumers.
    ''' </summary>
    ''' <remarks>
    ''' This facade deliberately exposes safe summaries only. It does not expose
    ''' MASRuntimeFaultRecord, MASRuntimeFaultChannel, source file paths, source line numbers,
    ''' or any mutable diagnostics internals.
    ''' </remarks>
    Public NotInheritable Class MASApplicationDiagnostics

        Private Const DefaultRecentFaultLimit As Integer = 32
        Private Const MaxRecentFaultLimit As Integer = 256

        Friend Sub New()
        End Sub

        ''' <summary>
        ''' Captures a read-only snapshot of retained MAS runtime faults and diagnostics switches.
        ''' </summary>
        ''' <param name="maxRecentFaults">
        ''' Maximum number of most-recent retained faults to project into the public snapshot.
        ''' The value is clamped between 0 and 256.
        ''' </param>
        Public Function GetSnapshot(Optional maxRecentFaults As Integer = DefaultRecentFaultLimit) As MASApplicationDiagnosticsSnapshot
            Dim retainedRecords As IReadOnlyList(Of MASRuntimeFaultRecord) = MASRuntimeFaultChannel.GetRecentFaultsSnapshot()
            Dim normalizedLimit As Integer = Math.Max(0, Math.Min(MaxRecentFaultLimit, maxRecentFaults))
            Dim startIndex As Integer = Math.Max(0, retainedRecords.Count - normalizedLimit)
            Dim recent As New List(Of MASApplicationDiagnosticsFaultSummary)()

            For index As Integer = startIndex To retainedRecords.Count - 1
                recent.Add(MASApplicationDiagnosticsFaultSummary.FromRuntimeRecord(retainedRecords(index)))
            Next

            Dim recoverableCount As Integer = 0
            Dim boundaryFailureCount As Integer = 0
            Dim criticalStateTransitionCount As Integer = 0
            Dim diagnosticOnlyCount As Integer = 0
            Dim renderFallbackCount As Integer = 0
            Dim inputBoundaryCount As Integer = 0
            Dim disposeCleanupCount As Integer = 0
            Dim lifecycleBreakingCount As Integer = 0
            Dim nativeInteropCount As Integer = 0
            Dim fileSystemBoundaryCount As Integer = 0
            Dim clipboardBoundaryCount As Integer = 0
            Dim textShapingFallbackCount As Integer = 0

            For Each record As MASRuntimeFaultRecord In retainedRecords
                Select Case record.Severity
                    Case MASExceptionSeverity.Recoverable
                        recoverableCount += 1
                    Case MASExceptionSeverity.BoundaryFailure
                        boundaryFailureCount += 1
                    Case MASExceptionSeverity.CriticalStateTransition
                        criticalStateTransitionCount += 1
                End Select

                Select Case record.Category
                    Case MASExceptionHandlingCategory.DiagnosticOnly
                        diagnosticOnlyCount += 1
                    Case MASExceptionHandlingCategory.RenderFallback
                        renderFallbackCount += 1
                    Case MASExceptionHandlingCategory.InputBoundary
                        inputBoundaryCount += 1
                    Case MASExceptionHandlingCategory.DisposeCleanup
                        disposeCleanupCount += 1
                    Case MASExceptionHandlingCategory.LifecycleBreaking
                        lifecycleBreakingCount += 1
                    Case MASExceptionHandlingCategory.NativeInterop
                        nativeInteropCount += 1
                    Case MASExceptionHandlingCategory.FileSystemBoundary
                        fileSystemBoundaryCount += 1
                    Case MASExceptionHandlingCategory.ClipboardBoundary
                        clipboardBoundaryCount += 1
                    Case MASExceptionHandlingCategory.TextShapingFallback
                        textShapingFallbackCount += 1
                End Select
            Next

            Return New MASApplicationDiagnosticsSnapshot(
                capturedUtc:=Nexamas.UI.Timing.MASTimeProvider.UtcNowOffset(),
                retainedFaultCount:=retainedRecords.Count,
                recentFaults:=recent.AsReadOnly(),
                recoverableCount:=recoverableCount,
                boundaryFailureCount:=boundaryFailureCount,
                criticalStateTransitionCount:=criticalStateTransitionCount,
                diagnosticOnlyCount:=diagnosticOnlyCount,
                renderFallbackCount:=renderFallbackCount,
                inputBoundaryCount:=inputBoundaryCount,
                disposeCleanupCount:=disposeCleanupCount,
                lifecycleBreakingCount:=lifecycleBreakingCount,
                nativeInteropCount:=nativeInteropCount,
                fileSystemBoundaryCount:=fileSystemBoundaryCount,
                clipboardBoundaryCount:=clipboardBoundaryCount,
                textShapingFallbackCount:=textShapingFallbackCount,
                diagnosticsDiskLoggingEnabled:=MASExceptionSilencer.DiagnosticsDiskLoggingEnabled,
                throwOnSwallowedException:=MASExceptionSilencer.ThrowOnSwallowedException,
                throwOnLifecycleBreakingException:=MASExceptionSilencer.ThrowOnLifecycleBreakingException,
                traceRepeatedBoundaryExceptions:=MASExceptionSilencer.TraceRepeatedBoundaryExceptions)
        End Function

    End Class

End Namespace
