Option Strict On
Option Explicit On

Imports System
Imports System.Collections.Generic
Imports Nexamas.UI.DataBinding

Namespace Nexamas.UI.Components

    ''' <summary>
    ''' Friend-only governance probe for MASKanbanBoard source projection. It proves
    ''' stable source records can build columns/cards, update incrementally through
    ''' IMASItemsSource changes, and detach cleanly when a manual board mutation occurs.
    ''' </summary>
    Friend NotInheritable Class MASKanbanBoardSourceProjectionGate
        Private Sub New()
        End Sub

        Friend Shared Function Passes() As Boolean
            Try
                If Not MASKanbanBoardPolicy.HasDataSourceProjectionPath() Then Return False

                Dim records As New List(Of Object) From {
                    MASKanbanSourceRecord.Create("todo", "Write spec", "Todo", "c-1", "Initial draft", "P1"),
                    MASKanbanSourceRecord.Create("doing", "Build feature", "Doing", "c-2", "Implementation", "P2"),
                    MASKanbanSourceRecord.Create("todo", "Review notes", "Todo", "c-3", "Checklist", "P3")
                }
                Dim source As MASListItemsSource = MASListItemsSource.FromEnumerable(records, AddressOf ResolveProbeKey)
                Dim definition As MASKanbanSourceDefinition = MASKanbanSourceDefinition.Create(
                    columnKeyField:="column",
                    cardTitleField:="title",
                    columnTitleField:="columnTitle",
                    cardKeyField:="key",
                    cardDetailField:="detail",
                    badgeTextField:="badge")

                Dim board As MASKanbanBoard = MASKanbanBoard.Create("Source board")
                board.SetKanbanSource(source, definition, "MASKanbanBoardSourceProjectionGate")

                If Not board.HasKanbanSource Then Return False
                If board.KanbanSourceRevision < 0 Then Return False
                If board.ColumnCount <> 2 Then Return False
                If board.CardCount <> 3 Then Return False
                If board.GetCardKeys("todo").Count <> 2 Then Return False
                If Not board.SelectCard("c-2") Then Return False

                source.Add(MASKanbanSourceRecord.Create("done", "Ship", "Done", "c-4", "Released", "OK"))
                If board.ColumnCount <> 3 Then Return False
                If board.CardCount <> 4 Then Return False
                If board.SelectedCardKey <> "c-2" Then Return False
                If Not board.SelectCard("c-4") Then Return False

                If Not board.MoveCardToIndex("c-4", "todo", 1) Then Return False
                If board.HasKanbanSource Then Return False
                If board.GetCardKeys("todo").Count <> 3 Then Return False
                If board.SelectedCardKey <> "c-4" Then Return False

                Return True
            Catch ex As Exception
                Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowDiagnosticOnly(ex, "MASKanbanBoardSourceProjectionGate")
                Return False
            End Try
        End Function

        Private Shared Function ResolveProbeKey(item As Object) As String
            Dim record As MASKanbanSourceRecord = TryCast(item, MASKanbanSourceRecord)
            If record Is Nothing Then Return String.Empty
            Dim value As Object = Nothing
            If record.TryGetField("key", value) Then Return Convert.ToString(value, System.Globalization.CultureInfo.InvariantCulture)
            Return String.Empty
        End Function
    End Class

End Namespace
