Option Strict On
Option Explicit On

Imports System
Imports System.Collections.Generic

Namespace Nexamas.UI.Components

    ''' <summary>
    ''' Friend-only governance probe for the single-day Agenda overlap layout. It proves
    ''' that timed events are placed by their minute range, overlapping events are split
    ''' into lanes, and the deterministic now-provider path can drive day navigation.
    ''' </summary>
    Friend NotInheritable Class MASAgendaViewOverlapLayoutGate
        Private Sub New()
        End Sub

        Friend Shared Function Passes() As Boolean
            Try
                If Not MASAgendaViewPolicy.HasOverlapLayoutPath() Then Return False
                If Not MASAgendaViewPolicy.HasDeterministicNowProviderPath() Then Return False
                If Not MASAgendaViewPolicy.HasSingleDayTimeWindowPath() Then Return False

                Dim day As New Date(2026, 1, 7)
                Dim agenda As MASAgendaView = MASAgendaView.Create("Overlap gate", day).WithHours(8, 12)
                agenda.AddEvent("early", "Early", day.AddHours(8), day.AddHours(9), "No overlap")
                agenda.AddEvent("left", "Left", day.AddHours(9), day.AddMinutes(10 * 60 + 0), "First overlap")
                agenda.AddEvent("right", "Right", day.AddMinutes(9 * 60 + 30), day.AddMinutes(10 * 60 + 30), "Second overlap")
                agenda.AddEvent("after", "After", day.AddMinutes(10 * 60 + 30), day.AddHours(11), "Touches overlap end")

                Dim evidence As IReadOnlyList(Of MASAgendaView.AgendaEventLayoutEvidence) = agenda.CreateTimedLayoutEvidenceForTests(760.0F, 420.0F, 1.0F, False)
                If evidence.Count <> 4 Then Return False

                Dim early As MASAgendaView.AgendaEventLayoutEvidence = FindEvidence(evidence, "early")
                Dim left As MASAgendaView.AgendaEventLayoutEvidence = FindEvidence(evidence, "left")
                Dim right As MASAgendaView.AgendaEventLayoutEvidence = FindEvidence(evidence, "right")
                Dim after As MASAgendaView.AgendaEventLayoutEvidence = FindEvidence(evidence, "after")
                If early Is Nothing OrElse left Is Nothing OrElse right Is Nothing OrElse after Is Nothing Then Return False

                If left.LaneCount < 2 OrElse right.LaneCount < 2 Then Return False
                If left.LaneIndex = right.LaneIndex Then Return False
                If RectsHorizontallyOverlap(left.Rect.Left, left.Rect.Right, right.Rect.Left, right.Rect.Right) Then Return False
                If Not (early.Rect.Top < left.Rect.Top AndAlso left.Rect.Top < after.Rect.Top) Then Return False

                agenda.SetNowProviderForTests(Function() day.AddHours(9).AddMinutes(45))
                agenda.MoveToday()
                If agenda.Day <> day Then Return False

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

        Private Shared Function FindEvidence(items As IReadOnlyList(Of MASAgendaView.AgendaEventLayoutEvidence),
                                             key As String) As MASAgendaView.AgendaEventLayoutEvidence
            If items Is Nothing Then Return Nothing
            For Each item As MASAgendaView.AgendaEventLayoutEvidence In items
                If item IsNot Nothing AndAlso String.Equals(item.EventKey, key, StringComparison.OrdinalIgnoreCase) Then Return item
            Next
            Return Nothing
        End Function

        Private Shared Function RectsHorizontallyOverlap(leftA As Single,
                                                         rightA As Single,
                                                         leftB As Single,
                                                         rightB As Single) As Boolean
            Return Math.Max(leftA, leftB) < Math.Min(rightA, rightB)
        End Function
    End Class

End Namespace
