Option Strict On
Option Explicit On

Imports System
Imports Nexamas.UI.Diagnostics

Namespace Nexamas.UI.Application

    ''' <summary>
    ''' Safe public projection of one retained MAS runtime fault.
    ''' </summary>
    ''' <remarks>
    ''' This summary intentionally omits internal source file paths, source line numbers,
    ''' mutable channel details, and Friend-owned enum types.
    ''' </remarks>
    Public NotInheritable Class MASApplicationDiagnosticsFaultSummary

        Private Sub New(timestampUtc As DateTimeOffset,
                        severity As String,
                        category As String,
                        context As String,
                        exceptionTypeName As String,
                        message As String,
                        occurrenceCount As Integer,
                        isFirstOccurrence As Boolean)

            Me.TimestampUtc = timestampUtc
            Me.Severity = If(severity, String.Empty)
            Me.Category = If(category, String.Empty)
            Me.Context = If(context, String.Empty)
            Me.ExceptionTypeName = If(exceptionTypeName, String.Empty)
            Me.Message = If(message, String.Empty)
            Me.OccurrenceCount = Math.Max(1, occurrenceCount)
            Me.IsFirstOccurrence = isFirstOccurrence
        End Sub

        Public ReadOnly Property TimestampUtc As DateTimeOffset
        Public ReadOnly Property Severity As String
        Public ReadOnly Property Category As String
        Public ReadOnly Property Context As String
        Public ReadOnly Property ExceptionTypeName As String
        Public ReadOnly Property Message As String
        Public ReadOnly Property OccurrenceCount As Integer
        Public ReadOnly Property IsFirstOccurrence As Boolean

        Friend Shared Function FromRuntimeRecord(record As MASRuntimeFaultRecord) As MASApplicationDiagnosticsFaultSummary
            If record Is Nothing Then
                Return New MASApplicationDiagnosticsFaultSummary(
                    timestampUtc:=DateTimeOffset.MinValue,
                    severity:=String.Empty,
                    category:=String.Empty,
                    context:=String.Empty,
                    exceptionTypeName:=String.Empty,
                    message:=String.Empty,
                    occurrenceCount:=1,
                    isFirstOccurrence:=False)
            End If

            Return New MASApplicationDiagnosticsFaultSummary(
                timestampUtc:=record.TimestampUtc,
                severity:=record.Severity.ToString(),
                category:=record.Category.ToString(),
                context:=record.Context,
                exceptionTypeName:=record.ExceptionTypeName,
                message:=record.Message,
                occurrenceCount:=record.OccurrenceCount,
                isFirstOccurrence:=record.IsFirstOccurrence)
        End Function

    End Class

End Namespace
