Option Strict On
Option Explicit On

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

Namespace Nexamas.UI.Quality

    Friend NotInheritable Class MASCommercialReadinessMatrix

        Private ReadOnly _records As IReadOnlyList(Of MASCommercialReadinessRecord)

        Friend Sub New(records As IEnumerable(Of MASCommercialReadinessRecord))
            Dim normalized As New List(Of MASCommercialReadinessRecord)()
            If records IsNot Nothing Then
                For Each record As MASCommercialReadinessRecord In records
                    If record IsNot Nothing Then normalized.Add(record)
                Next
            End If
            _records = New ReadOnlyCollection(Of MASCommercialReadinessRecord)(normalized.ToArray())
        End Sub

        Friend ReadOnly Property Records As IReadOnlyList(Of MASCommercialReadinessRecord)
            Get
                Return _records
            End Get
        End Property

        Friend ReadOnly Property Count As Integer
            Get
                Return _records.Count
            End Get
        End Property

        Friend ReadOnly Property ProductionReadyCount As Integer
            Get
                Dim total As Integer = 0
                For Each record As MASCommercialReadinessRecord In _records
                    If record IsNot Nothing AndAlso record.IsCommercialProductionReady Then total += 1
                Next
                Return total
            End Get
        End Property

        Friend ReadOnly Property PreviewCount As Integer
            Get
                Dim total As Integer = 0
                For Each record As MASCommercialReadinessRecord In _records
                    If record IsNot Nothing AndAlso record.Stage = MASCommercialReadinessStage.CompactPreview Then total += 1
                Next
                Return total
            End Get
        End Property

        Friend ReadOnly Property HasCommercialBlockers As Boolean
            Get
                For Each record As MASCommercialReadinessRecord In _records
                    If record IsNot Nothing AndAlso Not record.IsCommercialProductionReady Then Return True
                Next
                Return False
            End Get
        End Property

        Friend Function GetBlockedRecords() As IReadOnlyList(Of MASCommercialReadinessRecord)
            Dim blocked As New List(Of MASCommercialReadinessRecord)()
            For Each record As MASCommercialReadinessRecord In _records
                If record IsNot Nothing AndAlso Not record.IsCommercialProductionReady Then blocked.Add(record)
            Next
            Return New ReadOnlyCollection(Of MASCommercialReadinessRecord)(blocked.ToArray())
        End Function

        Friend Function FindById(id As String) As MASCommercialReadinessRecord
            Dim normalized As String = If(id, String.Empty).Trim()
            If normalized.Length = 0 Then Return Nothing

            For Each record As MASCommercialReadinessRecord In _records
                If record IsNot Nothing AndAlso String.Equals(record.Id, normalized, System.StringComparison.OrdinalIgnoreCase) Then
                    Return record
                End If
            Next
            Return Nothing
        End Function

        Friend Function CountByStage(stage As MASCommercialReadinessStage) As Integer
            Dim total As Integer = 0
            For Each record As MASCommercialReadinessRecord In _records
                If record IsNot Nothing AndAlso record.Stage = stage Then total += 1
            Next
            Return total
        End Function

    End Class

End Namespace
