<x-filament-panels::page>
    @php($landingHierarchy = $this->landingHierarchySummary())
    @php($environmentFilterChip = $this->environmentFilterChip())
    @php($lifecycleSummary = $this->lifecycleVisibilitySummary())
    @php($staleAttentionTab = \App\Models\OperationRun::PROBLEM_CLASS_ACTIVE_STALE_ATTENTION)
    @php($terminalFollowUpTab = \App\Models\OperationRun::PROBLEM_CLASS_TERMINAL_FOLLOW_UP)

    <x-filament::section heading="Monitoring landing" class="mb-6">
        <p class="text-sm text-gray-600 dark:text-gray-400">
            Tabs, filters, and row inspection define the active work lane. Scope context and return navigation stay secondary.
        </p>

        <div class="mt-4 grid gap-4 md:grid-cols-2 xl:grid-cols-4">
            <div class="rounded-xl border border-gray-200 bg-white/80 p-4 dark:border-white/10 dark:bg-white/5">
                <p class="text-xs font-semibold uppercase tracking-[0.2em] text-gray-500 dark:text-gray-400">Scope context</p>
                <p class="mt-2 text-sm font-semibold text-gray-900 dark:text-white">{{ $landingHierarchy['scope_label'] }}</p>
                <p class="mt-2 text-sm text-gray-600 dark:text-gray-400">{{ $landingHierarchy['scope_body'] }}</p>
            </div>

            @if ($landingHierarchy['return_label'] !== null && $landingHierarchy['return_body'] !== null)
                <div class="rounded-xl border border-gray-200 bg-white/80 p-4 dark:border-white/10 dark:bg-white/5">
                    <p class="text-xs font-semibold uppercase tracking-[0.2em] text-gray-500 dark:text-gray-400">Return path</p>
                    <p class="mt-2 text-sm font-semibold text-gray-900 dark:text-white">{{ $landingHierarchy['return_label'] }}</p>
                    <p class="mt-2 text-sm text-gray-600 dark:text-gray-400">{{ $landingHierarchy['return_body'] }}</p>
                </div>
            @endif

            @if ($landingHierarchy['scope_reset_label'] !== null && $landingHierarchy['scope_reset_body'] !== null)
                <div class="rounded-xl border border-gray-200 bg-white/80 p-4 dark:border-white/10 dark:bg-white/5">
                    <p class="text-xs font-semibold uppercase tracking-[0.2em] text-gray-500 dark:text-gray-400">Scope reset</p>
                    <p class="mt-2 text-sm font-semibold text-gray-900 dark:text-white">{{ $landingHierarchy['scope_reset_label'] }}</p>
                    <p class="mt-2 text-sm text-gray-600 dark:text-gray-400">{{ $landingHierarchy['scope_reset_body'] }}</p>
                </div>
            @endif

            <div class="rounded-xl border border-gray-200 bg-white/80 p-4 dark:border-white/10 dark:bg-white/5">
                <p class="text-xs font-semibold uppercase tracking-[0.2em] text-gray-500 dark:text-gray-400">Inspect flow</p>
                <p class="mt-2 text-sm font-semibold text-gray-900 dark:text-white">Open run detail</p>
                <p class="mt-2 text-sm text-gray-600 dark:text-gray-400">{{ $landingHierarchy['inspect_body'] }}</p>
            </div>
        </div>

        @if ($environmentFilterChip !== null)
            <div class="mt-4">
                @include('filament.partials.workspace-hub-environment-filter-chip', [
                    'label' => $environmentFilterChip['label'],
                    'clearUrl' => $environmentFilterChip['clear_url'],
                ])
            </div>
        @endif
    </x-filament::section>

    <x-filament::tabs label="Operations tabs">
        <x-filament::tabs.item
            :active="$this->activeTab === 'all'"
            :href="$this->tabUrl('all')"
            tag="a"
            :spa-mode="true"
        >
            All
        </x-filament::tabs.item>
        <x-filament::tabs.item
            :active="$this->activeTab === 'active'"
            :href="$this->tabUrl('active')"
            tag="a"
            :spa-mode="true"
        >
            Active
        </x-filament::tabs.item>
        <x-filament::tabs.item
            :active="$this->activeTab === $staleAttentionTab"
            :href="$this->tabUrl($staleAttentionTab)"
            tag="a"
            :spa-mode="true"
        >
            Likely stale
        </x-filament::tabs.item>
        <x-filament::tabs.item
            :active="$this->activeTab === $terminalFollowUpTab"
            :href="$this->tabUrl($terminalFollowUpTab)"
            tag="a"
            :spa-mode="true"
        >
            Terminal follow-up
        </x-filament::tabs.item>
        <x-filament::tabs.item
            :active="$this->activeTab === 'succeeded'"
            :href="$this->tabUrl('succeeded')"
            tag="a"
            :spa-mode="true"
        >
            Succeeded
        </x-filament::tabs.item>
        <x-filament::tabs.item
            :active="$this->activeTab === 'partial'"
            :href="$this->tabUrl('partial')"
            tag="a"
            :spa-mode="true"
        >
            Partial
        </x-filament::tabs.item>
        <x-filament::tabs.item
            :active="$this->activeTab === 'failed'"
            :href="$this->tabUrl('failed')"
            tag="a"
            :spa-mode="true"
        >
            Failed
        </x-filament::tabs.item>
    </x-filament::tabs>

    <p class="mb-4 text-sm text-gray-600 dark:text-gray-400">
        Environment filters and the selected operations tab remain shareable through the URL. Additional table filters still restore from the last compatible session state.
    </p>

    @if (($lifecycleSummary['likely_stale'] ?? 0) > 0 || ($lifecycleSummary['reconciled'] ?? 0) > 0)
        <div class="mb-4 rounded-lg border border-amber-200 bg-amber-50 px-4 py-3 text-sm text-amber-900 dark:border-amber-500/30 dark:bg-amber-500/10 dark:text-amber-100">
            {{ ($lifecycleSummary['likely_stale'] ?? 0) }} active operation(s) are beyond their lifecycle window and belong in the stale-attention view.
            {{ ($lifecycleSummary['reconciled'] ?? 0) }} operation(s) already carry reconciled stale lineage and belong in terminal follow-up.
        </div>
    @endif

    {{ $this->table }}
</x-filament-panels::page>
