Option Strict On
Option Explicit On

Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel

Namespace Nexamas.UI.Quality

    ''' <summary>
    ''' Aggregated friend-only commercial readiness report for the Phase-1 product-control
    ''' hardening work. It separates preview-governance health from production marketing
    ''' readiness so compact controls can remain addable without being over-sold.
    ''' </summary>
    Friend NotInheritable Class MASCommercialReadinessReport

        Private ReadOnly _findings As IReadOnlyList(Of MASCommercialReadinessFinding)

        Friend Sub New(matrix As MASCommercialReadinessMatrix,
                       requiredEvidenceCount As Integer,
                       passingEvidenceCount As Integer,
                       findings As IEnumerable(Of MASCommercialReadinessFinding))
            Me.Matrix = If(matrix, New MASCommercialReadinessMatrix(Array.Empty(Of MASCommercialReadinessRecord)()))
            Me.RequiredEvidenceCount = Math.Max(0, requiredEvidenceCount)
            Me.PassingEvidenceCount = Math.Max(0, Math.Min(Me.RequiredEvidenceCount, passingEvidenceCount))
            _findings = New ReadOnlyCollection(Of MASCommercialReadinessFinding)(NormalizeFindings(findings).ToArray())
        End Sub

        Friend ReadOnly Property Matrix As MASCommercialReadinessMatrix
        Friend ReadOnly Property RequiredEvidenceCount As Integer
        Friend ReadOnly Property PassingEvidenceCount As Integer

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

        Friend ReadOnly Property ErrorCount As Integer
            Get
                Dim total As Integer = 0
                For Each finding As MASCommercialReadinessFinding In _findings
                    If finding IsNot Nothing AndAlso finding.IsError Then total += 1
                Next
                Return total
            End Get
        End Property

        Friend ReadOnly Property WarningCount As Integer
            Get
                Dim total As Integer = 0
                For Each finding As MASCommercialReadinessFinding In _findings
                    If finding IsNot Nothing AndAlso finding.IsWarning Then total += 1
                Next
                Return total
            End Get
        End Property

        Friend ReadOnly Property EvidencePassRatio As Double
            Get
                If RequiredEvidenceCount <= 0 Then Return 1.0R
                Return CDbl(PassingEvidenceCount) / CDbl(RequiredEvidenceCount)
            End Get
        End Property

        Friend ReadOnly Property HasBlockingFailures As Boolean
            Get
                Return ErrorCount > 0
            End Get
        End Property

        Friend ReadOnly Property IsPreviewGovernanceReady As Boolean
            Get
                Return Not HasBlockingFailures AndAlso RequiredEvidenceCount > 0 AndAlso PassingEvidenceCount = RequiredEvidenceCount
            End Get
        End Property

        Friend ReadOnly Property IsProductionMarketingReady As Boolean
            Get
                Return IsPreviewGovernanceReady AndAlso Matrix IsNot Nothing AndAlso Matrix.Count > 0 AndAlso Not Matrix.HasCommercialBlockers
            End Get
        End Property

        Friend ReadOnly Property Summary As String
            Get
                If IsProductionMarketingReady Then Return "Commercial product-control matrix is production-ready."
                If IsPreviewGovernanceReady AndAlso Matrix IsNot Nothing AndAlso Matrix.ProductionReadyCount > 0 Then Return "Commercial product-control matrix is coherent for mixed-stage marketing; targeted ProductionReady controls and remaining blockers are declared."
                If IsPreviewGovernanceReady Then Return "Commercial product-control matrix is coherent for preview-only marketing; production blockers remain declared."
                Return "Commercial product-control matrix has blocking governance or evidence failures."
            End Get
        End Property

        Private Shared Function NormalizeFindings(findings As IEnumerable(Of MASCommercialReadinessFinding)) As List(Of MASCommercialReadinessFinding)
            Dim result As New List(Of MASCommercialReadinessFinding)()
            If findings Is Nothing Then Return result

            For Each finding As MASCommercialReadinessFinding In findings
                If finding IsNot Nothing Then result.Add(finding)
            Next
            Return result
        End Function

    End Class

End Namespace
