Option Strict On
Option Explicit On

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

Namespace Nexamas.UI.Components

    ''' <summary>
    ''' Friend-only proof for KanbanBoard write-back policy. Public/manual card moves
    ''' intentionally detach from projected sources instead of mutating external data,
    ''' keeping task persistence, workflow providers, and background sync outside the
    ''' control while preserving visual move behavior.
    ''' </summary>
    Friend NotInheritable Class MASKanbanBoardWriteBackPolicyGate
        Private Sub New()
        End Sub

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

                Dim todoRecord As MASKanbanSourceRecord = MASKanbanSourceRecord.Create("todo", "Spec", "Todo", "c-1", "Draft", "P1")
                Dim doingRecord As MASKanbanSourceRecord = MASKanbanSourceRecord.Create("doing", "Build", "Doing", "c-2", "Impl", "P2")
                Dim source As MASListItemsSource = MASListItemsSource.FromEnumerable(New List(Of Object) From {todoRecord, doingRecord}, 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("Write-back policy")
                board.SetKanbanSource(source, definition, "MASKanbanBoardWriteBackPolicyGate")
                If Not board.HasKanbanSource Then Return False
                Dim sourceRevision As Integer = source.Revision

                Dim legalDrop As MASKanbanBoardDropRuleSnapshot = board.CreateDropRuleSnapshot("c-1", "doing", 0)
                If legalDrop Is Nothing OrElse Not legalDrop.IsLegalTarget OrElse Not legalDrop.WouldDetachSource Then Return False

                If Not board.MoveCardToIndex("c-1", "doing", 0) Then Return False
                If board.HasKanbanSource Then Return False
                If source.Revision <> sourceRevision Then Return False
                If board.GetCardKeys("doing").Count <> 2 Then Return False
                If board.GetCardKeys("doing")(0) <> "c-1" Then Return False

                Dim rawColumn As Object = Nothing
                If Not todoRecord.TryGetField("column", rawColumn) Then Return False
                If Not String.Equals(Convert.ToString(rawColumn, Globalization.CultureInfo.InvariantCulture), "todo", StringComparison.OrdinalIgnoreCase) Then Return False

                source.ReplaceAt(0, MASKanbanSourceRecord.Create("done", "Spec", "Done", "c-1", "Changed after detach", "P1"))
                If board.GetCardKeys("doing")(0) <> "c-1" Then Return False

                Return True
            Catch ex As Exception
                Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowDiagnosticOnly(ex, "MASKanbanBoardWriteBackPolicyGate")
                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, Globalization.CultureInfo.InvariantCulture)
            Return String.Empty
        End Function
    End Class

End Namespace
