Option Strict On
Option Explicit On

Imports System
Imports Nexamas.UI.Performance

Namespace Nexamas.UI.DiagnosticsDashboard

    ''' <summary>
    ''' Immutable read-only summary of an already-built Baseline Performance Benchmark report.
    ''' </summary>
    ''' <remarks>
    ''' This contract does not execute benchmarks or compare current measurements. It only projects the
    ''' existing PerformanceSystem-owned baseline report into dashboard rows for engineering visibility.
    ''' </remarks>
    Friend NotInheritable Class MASDiagnosticsDashboardBaselineReference

        Friend Shared ReadOnly Unavailable As New MASDiagnosticsDashboardBaselineReference(
            String.Empty,
            String.Empty,
            0,
            0,
            0,
            0,
            0,
            0,
            False,
            MASDiagnosticsDashboardBaselineAwarenessStatus.Unavailable)

        Private Sub New(benchmarkName As String,
                        baselineId As String,
                        entryCount As Integer,
                        requiredBenchmarkCount As Integer,
                        readyEntryCount As Integer,
                        passedCount As Integer,
                        warningCount As Integer,
                        failedCount As Integer,
                        readyForRegressionComparison As Boolean,
                        status As MASDiagnosticsDashboardBaselineAwarenessStatus)
            Me.BenchmarkName = If(benchmarkName, String.Empty).Trim()
            Me.BaselineId = If(baselineId, String.Empty).Trim()
            Me.EntryCount = Math.Max(0, entryCount)
            Me.RequiredBenchmarkCount = Math.Max(0, requiredBenchmarkCount)
            Me.ReadyEntryCount = Math.Max(0, readyEntryCount)
            Me.PassedCount = Math.Max(0, passedCount)
            Me.WarningCount = Math.Max(0, warningCount)
            Me.FailedCount = Math.Max(0, failedCount)
            Me.ReadyForRegressionComparison = readyForRegressionComparison
            Me.Status = status
        End Sub

        Friend ReadOnly Property BenchmarkName As String
        Friend ReadOnly Property BaselineId As String
        Friend ReadOnly Property EntryCount As Integer
        Friend ReadOnly Property RequiredBenchmarkCount As Integer
        Friend ReadOnly Property ReadyEntryCount As Integer
        Friend ReadOnly Property PassedCount As Integer
        Friend ReadOnly Property WarningCount As Integer
        Friend ReadOnly Property FailedCount As Integer
        Friend ReadOnly Property ReadyForRegressionComparison As Boolean
        Friend ReadOnly Property Status As MASDiagnosticsDashboardBaselineAwarenessStatus

        Friend ReadOnly Property IsAvailable As Boolean
            Get
                Return Status <> MASDiagnosticsDashboardBaselineAwarenessStatus.Unavailable
            End Get
        End Property

        Friend ReadOnly Property HasRequiredCoverage As Boolean
            Get
                Return EntryCount >= RequiredBenchmarkCount AndAlso RequiredBenchmarkCount > 0
            End Get
        End Property

        Friend ReadOnly Property HasRegressionRiskWarning As Boolean
            Get
                Return IsAvailable AndAlso (Not ReadyForRegressionComparison OrElse WarningCount > 0 OrElse FailedCount > 0)
            End Get
        End Property

        Friend Shared Function FromReport(report As MASPerformanceBaselineBenchmarkReport) As MASDiagnosticsDashboardBaselineReference
            If report Is Nothing Then Return Unavailable

            Dim status As MASDiagnosticsDashboardBaselineAwarenessStatus
            If report.FailedCount > 0 Then
                status = MASDiagnosticsDashboardBaselineAwarenessStatus.FailedBaseline
            ElseIf report.WarningCount > 0 Then
                status = MASDiagnosticsDashboardBaselineAwarenessStatus.WarningBaseline
            ElseIf Not report.ReadyForRegressionComparison Then
                status = MASDiagnosticsDashboardBaselineAwarenessStatus.IncompleteCoverage
            Else
                status = MASDiagnosticsDashboardBaselineAwarenessStatus.Ready
            End If

            Return New MASDiagnosticsDashboardBaselineReference(
                report.BenchmarkName,
                report.BaselineId,
                report.EntryCount,
                report.RequiredBenchmarkCount,
                report.ReadyEntryCount,
                report.PassedCount,
                report.WarningCount,
                report.FailedCount,
                report.ReadyForRegressionComparison,
                status)
        End Function

    End Class

End Namespace
