Option Strict On
Option Explicit On

Imports System

Namespace Nexamas.UI.Components

    ''' <summary>
    ''' Friend-only proof for AgendaView temporal ownership. AgendaView consumes
    ''' concrete timestamps and projects them deterministically into visual day/minute
    ''' windows; recurrence expansion and time-zone databases stay outside the control.
    ''' </summary>
    Friend NotInheritable Class MASAgendaViewRecurrenceTimeZoneGate
        Private Sub New()
        End Sub

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

                Dim anchor As New DateTime(2026, 3, 29, 9, 0, 0, DateTimeKind.Unspecified)
                Dim snapshot As MASAgendaTemporalPolicySnapshot = MASAgendaTemporalPolicy.CreatePolicySnapshot(anchor, anchor.Date, 1, 23, Function() anchor)
                If snapshot Is Nothing OrElse Not snapshot.IsProductionPolicyClosed Then Return False
                If snapshot.OwnsRecurrenceExpansion Then Return False
                If snapshot.OwnsTimeZoneDatabase Then Return False
                If Not snapshot.AcceptsConcreteTimestamps Then Return False
                If snapshot.SampleWindow Is Nothing OrElse snapshot.SampleWindow.DurationMinutes <= 0 Then Return False

                Dim crossingWindow As MASAgendaVisualTimeWindow = MASAgendaTemporalPolicy.CreateWindow(
                    New DateTime(2026, 3, 29, 1, 30, 0, DateTimeKind.Unspecified),
                    New DateTime(2026, 3, 29, 3, 30, 0, DateTimeKind.Unspecified),
                    anchor.Date,
                    1,
                    23)
                If crossingWindow Is Nothing OrElse Not crossingWindow.IsValid Then Return False
                If crossingWindow.EventDay <> anchor.Date Then Return False
                If crossingWindow.StartMinute <> 90 OrElse crossingWindow.EndMinute <> 210 Then Return False

                Dim agenda As MASAgendaView = MASAgendaView.Create("Temporal gate", anchor.Date).WithHours(1, 23)
                agenda.SetNowProviderForTests(Function() anchor)
                agenda.AddEvent("concrete", "Concrete timestamp", anchor.AddHours(1), anchor.AddHours(2))
                If agenda.EventCount <> 1 Then Return False
                If Not agenda.SelectEvent("concrete") Then Return False
                If Not String.Equals(agenda.SelectedEventKey, "concrete", StringComparison.OrdinalIgnoreCase) Then Return False

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

End Namespace
