Option Strict On
Option Explicit On

Imports System.Collections.Generic
Imports System.Linq

Namespace Nexamas.UI.ThemeStudio

    ''' <summary>
    ''' Official readiness gate for Phase 5 Theme Studio / Surface Designer foundation.
    ''' </summary>
    ''' <remarks>
    ''' The gate proves that Phase 5 consumes the existing Theme and Surface Visual systems
    ''' through a small read-only snapshot, with no second theme registry or design-time renderer.
    ''' </remarks>
    Friend NotInheritable Class MASThemeStudioReadinessGate
        Private Sub New()
        End Sub

        Friend Shared Function Evaluate() As MASThemeStudioReadinessReport
            Dim findings As New List(Of MASThemeStudioReadinessFinding)()
            Dim snapshot As MASThemeStudioSnapshot = MASThemeStudioSnapshotBuilder.Build()

            If snapshot Is Nothing Then
                findings.Add(New MASThemeStudioReadinessFinding("MASThemeStudio.MissingSnapshot", "Theme Studio snapshot builder returned Nothing.", MASThemeStudioReadinessSeverity.Error))
                Return New MASThemeStudioReadinessReport(0, 0, 0, 0, 0, 0, 0, findings)
            End If

            If snapshot.ContractVersion <> MASThemeStudioContractVersion.Current Then
                findings.Add(New MASThemeStudioReadinessFinding("MASThemeStudio.ContractVersionMismatch", "Theme Studio snapshot contract version is not current.", MASThemeStudioReadinessSeverity.Error))
            End If

            If snapshot.ThemeCount < 3 Then
                findings.Add(New MASThemeStudioReadinessFinding("MASThemeStudio.NotEnoughThemes", "Theme Studio requires the three built-in MASSignature themes to be visible.", MASThemeStudioReadinessSeverity.Error))
            End If

            If Global.System.Linq.Enumerable.Count(snapshot.Themes, Function(theme) theme IsNot Nothing AndAlso theme.IsCurrent) <> 1 Then
                findings.Add(New MASThemeStudioReadinessFinding("MASThemeStudio.InvalidCurrentTheme", "Exactly one Theme Studio theme entry must be marked as current.", MASThemeStudioReadinessSeverity.Error))
            End If

            If snapshot.MaterialCount < 8 Then
                findings.Add(New MASThemeStudioReadinessFinding("MASSurfaceDesigner.NotEnoughMaterials", "Surface Designer must expose all built-in reusable surface materials.", MASThemeStudioReadinessSeverity.Error))
            End If

            If snapshot.MaterialProfileCount < 5 Then
                findings.Add(New MASThemeStudioReadinessFinding("MASSurfaceDesigner.NotEnoughMaterialProfiles", "Surface Designer must expose reusable material profiles for generic slot distribution.", MASThemeStudioReadinessSeverity.Error))
            End If

            If Global.System.Linq.Enumerable.Count(snapshot.MaterialProfiles, Function(profile) profile IsNot Nothing AndAlso profile.HasNativeAssignments) < 2 Then
                findings.Add(New MASThemeStudioReadinessFinding("MASSurfaceDesigner.MissingNativeMaterialProfiles", "Surface Designer must expose native/identity-preserving material profiles without dissolving them into theme colors.", MASThemeStudioReadinessSeverity.Error))
            End If

            If snapshot.SlotCount < 10 Then
                findings.Add(New MASThemeStudioReadinessFinding("MASSurfaceDesigner.NotEnoughSlots", "Surface Designer must expose the reusable generic surface slot matrix.", MASThemeStudioReadinessSeverity.Error))
            End If

            If snapshot.RoleCount < 8 Then
                findings.Add(New MASThemeStudioReadinessFinding("MASSurfaceDesigner.NotEnoughRoles", "Surface Designer must expose the reusable surface role matrix.", MASThemeStudioReadinessSeverity.Error))
            End If

            If snapshot.VisualStyleCount < 1 Then
                findings.Add(New MASThemeStudioReadinessFinding("MASSurfaceDesigner.MissingVisualStyles", "Surface Designer must expose at least one reusable surface visual style.", MASThemeStudioReadinessSeverity.Error))
            End If

            If snapshot.ContrastCheckCount < snapshot.ThemeCount * 3 Then
                findings.Add(New MASThemeStudioReadinessFinding("MASThemeStudio.MissingContrastChecks", "Theme Studio must produce the minimum contrast matrix for every theme.", MASThemeStudioReadinessSeverity.Error))
            End If

            Dim failedContrastCount As Integer = Global.System.Linq.Enumerable.Count(snapshot.ContrastChecks, Function(check) check IsNot Nothing AndAlso Not check.Passes)
            If failedContrastCount > 0 Then
                findings.Add(New MASThemeStudioReadinessFinding("MASThemeStudio.ContrastWarning", failedContrastCount.ToString(Global.System.Globalization.CultureInfo.InvariantCulture) & " contrast checks are below their target ratio.", MASThemeStudioReadinessSeverity.Warning))
            End If

            If Not snapshot.ReadyForDesignerPreview Then
                findings.Add(New MASThemeStudioReadinessFinding("MASThemeStudio.SnapshotNotPreviewReady", "The snapshot is missing required designer preview facts.", MASThemeStudioReadinessSeverity.Error))
            End If

            Return New MASThemeStudioReadinessReport(snapshot.ThemeCount, snapshot.MaterialCount, snapshot.MaterialProfileCount, snapshot.SlotCount, snapshot.RoleCount, snapshot.VisualStyleCount, snapshot.ContrastCheckCount, findings)
        End Function

    End Class

End Namespace
