@php
    $controls = $this->controlCards();
@endphp

<x-filament-panels::page>
    <div class="space-y-6">
        <x-filament::section>
            <div class="flex items-start gap-3">
                <x-heroicon-o-pause-circle class="h-6 w-6 shrink-0 text-amber-500 dark:text-amber-400" />

                <div>
                    <p class="text-sm font-semibold text-amber-700 dark:text-amber-300">Runtime safety controls</p>
                    <p class="mt-1 text-sm text-gray-600 dark:text-gray-300">
                        Use these bounded operational controls to pause risky starts without hiding the underlying surface. Global pauses win over workspace-specific pauses.
                    </p>
                </div>
            </div>
        </x-filament::section>

        <div class="grid gap-6">
            @foreach ($controls as $control)
                <x-filament::section>
                    <x-slot name="heading">
                        {{ $control['label'] }}
                    </x-slot>

                    <x-slot name="description">
                        {{ implode(', ', $control['affected_surfaces']) }}
                    </x-slot>

                    <x-slot name="afterHeader">
                        <x-filament::badge color="{{ $control['effective_state'] === 'paused' ? 'danger' : 'success' }}" size="sm">
                            {{ $control['state_label'] }}
                        </x-filament::badge>
                    </x-slot>

                    @php
                        $pauseActionName = 'pause_'.$control['action_slug'];
                        $resumeActionName = 'resume_'.$control['action_slug'];
                        $historyActionName = 'view_history_'.$control['action_slug'];
                    @endphp

                    <div class="space-y-4">
                        <div class="flex flex-wrap gap-2">
                            @foreach ($control['supported_scopes'] as $scope)
                                <x-filament::badge color="gray" size="sm">
                                    {{ ucfirst($scope) }}
                                </x-filament::badge>
                            @endforeach
                        </div>

                        <div class="flex flex-wrap items-center gap-2">
                            @if ($control['effective_state'] === 'paused')
                                <x-filament::button
                                    color="gray"
                                    size="sm"
                                    icon="heroicon-o-play"
                                    wire:click="mountAction('{{ $resumeActionName }}')"
                                >
                                    Resume
                                </x-filament::button>
                            @else
                                <x-filament::button
                                    color="danger"
                                    size="sm"
                                    icon="heroicon-o-pause"
                                    wire:click="mountAction('{{ $pauseActionName }}')"
                                >
                                    Pause
                                </x-filament::button>
                            @endif

                            <x-filament::button
                                color="gray"
                                size="sm"
                                wire:click="mountAction('{{ $historyActionName }}')"
                            >
                                History
                            </x-filament::button>
                        </div>

                        @if ($control['active_activations'] !== [])
                            <div class="space-y-3">
                                @foreach ($control['active_activations'] as $activation)
                                    <div class="rounded-lg bg-gray-50 px-4 py-3 dark:bg-white/5">
                                        <div class="flex flex-wrap items-center gap-x-4 gap-y-2">
                                            <span class="text-sm font-semibold text-gray-900 dark:text-white">
                                                {{ $activation['scope_label'] }}
                                            </span>

                                            <span class="text-xs text-gray-500 dark:text-gray-400">
                                                Owner: {{ $activation['owner_name'] }}
                                            </span>

                                            <span class="text-xs text-gray-500 dark:text-gray-400">
                                                {{ $activation['expires_label'] }}
                                            </span>
                                        </div>

                                        <p class="mt-2 text-sm text-gray-700 dark:text-gray-300">
                                            {{ $activation['reason_text'] }}
                                        </p>
                                    </div>
                                @endforeach
                            </div>
                        @else
                            <div class="rounded-lg border border-dashed border-gray-200 px-4 py-3 text-sm text-gray-500 dark:border-white/10 dark:text-gray-400">
                                No active pauses. New starts are currently enabled.
                            </div>
                        @endif

                        <p class="text-xs text-gray-500 dark:text-gray-400">
                            Use the card actions to pause, resume, or inspect audit history for this control.
                        </p>
                    </div>
                </x-filament::section>
            @endforeach
        </div>
    </div>
</x-filament-panels::page>