Imports System.Collections.Generic
Imports System.Linq

Namespace Nexamas.UI.Verification

    ''' <summary>
    ''' Aggregate execution result for a render verification pass.
    ''' It carries snapshots, diffs, and findings separately so callers can produce CI logs without re-reading files.
    ''' Passed excludes seeded baselines; seeded artifacts are review evidence and surface through RequiresApproval.
    ''' </summary>
    Friend NotInheritable Class MASRenderVerificationRunResult

        Private ReadOnly _snapshots As IReadOnlyList(Of MASRenderSnapshotResult)
        Private ReadOnly _diffs As IReadOnlyList(Of MASRenderDiffResult)
        Private ReadOnly _findings As IReadOnlyList(Of MASRenderVerificationFinding)

        Friend Sub New(snapshots As IEnumerable(Of MASRenderSnapshotResult),
                       diffs As IEnumerable(Of MASRenderDiffResult),
                       findings As IEnumerable(Of MASRenderVerificationFinding))
            _snapshots = If(snapshots, Array.Empty(Of MASRenderSnapshotResult)()).Where(Function(snapshot) snapshot IsNot Nothing).ToArray()
            _diffs = If(diffs, Array.Empty(Of MASRenderDiffResult)()).Where(Function(diff) diff IsNot Nothing).ToArray()
            _findings = If(findings, Array.Empty(Of MASRenderVerificationFinding)()).Where(Function(finding) finding IsNot Nothing).ToArray()
        End Sub

        Friend ReadOnly Property Snapshots As IReadOnlyList(Of MASRenderSnapshotResult)
            Get
                Return _snapshots
            End Get
        End Property

        Friend ReadOnly Property Diffs As IReadOnlyList(Of MASRenderDiffResult)
            Get
                Return _diffs
            End Get
        End Property

        Friend ReadOnly Property Findings As IReadOnlyList(Of MASRenderVerificationFinding)
            Get
                Return _findings
            End Get
        End Property

        Friend ReadOnly Property Passed As Boolean
            Get
                Return _findings.All(Function(finding) Not finding.IsBlocking) AndAlso _diffs.Count > 0 AndAlso _diffs.All(Function(diff) diff.Passed)
            End Get
        End Property

        Friend ReadOnly Property MatchedScenarioCount As Integer
            Get
                Return System.Linq.Enumerable.Count(_diffs, Function(diff) diff.Outcome = MASRenderVerificationOutcome.Passed)
            End Get
        End Property

        Friend ReadOnly Property SeededScenarioCount As Integer
            Get
                Return System.Linq.Enumerable.Count(_diffs, Function(diff) diff.Outcome = MASRenderVerificationOutcome.BaselineSeeded)
            End Get
        End Property

        Friend ReadOnly Property FailedScenarioCount As Integer
            Get
                Return System.Linq.Enumerable.Count(_diffs, Function(diff) diff.Outcome = MASRenderVerificationOutcome.Failed)
            End Get
        End Property

        Friend ReadOnly Property InvalidScenarioCount As Integer
            Get
                Return System.Linq.Enumerable.Count(_diffs, Function(diff) diff.Outcome = MASRenderVerificationOutcome.Invalid)
            End Get
        End Property

        Friend ReadOnly Property RequiresApproval As Boolean
            Get
                Return SeededScenarioCount > 0
            End Get
        End Property

    End Class

End Namespace
