@php
    $breadcrumbs = [
        ['label' => __('common.home'), 'url' => route('dashboard.index')],
        ['label' => __('operations_control.title')],
    ];
    $severityBadge = static function (string $sev): string {
        return match ($sev) {
            'critical', 'error', 'high' => 'danger',
            'warning', 'contained' => 'warning',
            'clean', 'info' => 'secondary',
            default => 'secondary',
        };
    };
@endphp

<x-ui.layout.page
    :title="__('operations_control.title')"
    :subtitle="__('operations_control.subtitle')"
    page-icon="fe fe-activity"
    :breadcrumbs="$breadcrumbs"
>
    <h5 class="mt-2">{{ __('operations_control.sections.escalation') }}</h5>
    <x-ui.table.table :columns="[
        ['label' => __('operations_control.columns.severity')],
        ['label' => 'Guidance'],
    ]">
        @foreach (config('operations-control.severity', []) as $sev => $meta)
            <x-ui.table.row>
                <td><span class="badge bg-{{ $severityBadge((string) $sev) }}">{{ $meta['label'] ?? $sev }}</span></td>
                <td class="small">{{ $meta['operator_note'] ?? '' }}</td>
            </x-ui.table.row>
        @endforeach
    </x-ui.table.table>

    <h5 class="mt-4">{{ __('operations_control.sections.reconciliation') }}</h5>
    <div class="alert {{ $projectionDrift ? 'alert-warning' : 'alert-success' }} mb-2">
        {{ $projectionDrift ? __('operations_control.reconciliation.drift_yes') : __('operations_control.reconciliation.drift_no') }}
    </div>
    @can('operations.control.manage')
        <div class="row g-3 mb-4">
            <div class="col-md-6">
                <form method="post" action="{{ route('operations.control-center.inventory.rebuild') }}" class="border rounded p-3" data-operational-confirm="{{ __('operations_control.reconciliation.rebuild') }}">
                    @csrf
                    <label class="form-label small">{{ __('operations_control.reconciliation.rebuild_confirm') }}</label>
                    <input type="text" name="confirmation" class="form-control form-control-sm mb-2" required autocomplete="off">
                    <button type="submit" class="btn btn-sm btn-warning">{{ __('operations_control.reconciliation.rebuild') }}</button>
                </form>
            </div>
            <div class="col-md-6">
                <form method="post" action="{{ route('operations.control-center.inventory.reconcile-check') }}" class="border rounded p-3" data-operational-confirm="{{ __('operations_control.reconciliation.check') }}">
                    @csrf
                    <label class="form-label small">{{ __('operations_control.reconciliation.check_confirm') }}</label>
                    <input type="text" name="confirmation" class="form-control form-control-sm mb-2" required autocomplete="off">
                    <button type="submit" class="btn btn-sm btn-outline-primary">{{ __('operations_control.reconciliation.check') }}</button>
                </form>
            </div>
        </div>
    @endcan

    <h5 class="mt-2">{{ __('operations_control.sections.integrity') }}</h5>
    <div class="mb-3">
        <span class="badge bg-{{ $severityBadge($integrityAudit['severity'] ?? 'clean') }}">{{ $integrityAudit['severity'] ?? '—' }}</span>
        <span class="small text-muted ms-2">{{ __('operations_control.integrity.cross_po') }}: {{ $integrityAudit['active_cross_po'] ?? 0 }}</span>
        <span class="small text-muted ms-2">{{ __('operations_control.integrity.deleted_poi') }}: {{ $integrityAudit['active_deleted_poi'] ?? 0 }}</span>
        <span class="small text-muted ms-2">{{ __('operations_control.integrity.missing_poi') }}: {{ $integrityAudit['active_missing_poi'] ?? 0 }}</span>
        <span class="small text-muted ms-2">{{ __('operations_control.integrity.quarantined_lines') }}: {{ $integrityAudit['quarantined_lines'] ?? 0 }}</span>
    </div>

    <h5 class="mt-4">{{ __('operations_control.sections.failed_receipts') }}</h5>
    <x-ui.table.table :columns="[
        ['label' => __('operations_control.columns.receipt')],
        ['label' => __('operations_control.columns.po')],
        ['label' => __('operations_control.columns.correlation')],
        ['label' => __('operations_control.columns.updated')],
        ['label' => __('common.view')],
    ]">
        @forelse ($failedReceipts as $r)
            <x-ui.table.row>
                <td>#{{ $r->id }}</td>
                <td>{{ $r->purchaseOrder?->po_number ?? '—' }}</td>
                <td class="small">{{ $r->correlation_id ?? '—' }}</td>
                <td class="small">{{ $r->updated_at }}</td>
                <td><x-ui.buttons.secondary :href="route('procurement.goods-receipts.show', $r)">{{ __('common.view') }}</x-ui.buttons.secondary></td>
            </x-ui.table.row>
        @empty
            <x-ui.table.row><td colspan="5" class="text-muted">—</td></x-ui.table.row>
        @endforelse
    </x-ui.table.table>

    <h5 class="mt-4">{{ __('operations_control.sections.quarantined_receipts') }}</h5>
    <x-ui.table.table :columns="[
        ['label' => __('operations_control.columns.receipt')],
        ['label' => __('operations_control.columns.po')],
        ['label' => __('operations_control.columns.status')],
        ['label' => __('common.view')],
    ]">
        @forelse ($quarantinedReceipts as $r)
            <x-ui.table.row>
                <td>#{{ $r->id }}</td>
                <td>{{ $r->purchaseOrder?->po_number ?? '—' }}</td>
                <td><span class="badge bg-warning text-dark">{{ $r->status }}</span></td>
                <td><x-ui.buttons.secondary :href="route('procurement.goods-receipts.show', $r)">{{ __('common.view') }}</x-ui.buttons.secondary></td>
            </x-ui.table.row>
        @empty
            <x-ui.table.row><td colspan="4" class="text-muted">—</td></x-ui.table.row>
        @endforelse
    </x-ui.table.table>

    <h5 class="mt-4">{{ __('operations_control.sections.stale_drafts') }}</h5>
    <p class="small text-muted">{{ __('operations_control.stale.draft_help', ['hours' => $thresholds['draft_hours']]) }}</p>
    <x-ui.table.table :columns="[
        ['label' => __('operations_control.columns.receipt')],
        ['label' => __('operations_control.columns.updated')],
        ['label' => __('common.view')],
    ]">
        @forelse ($staleDraftReceipts as $r)
            <x-ui.table.row>
                <td>#{{ $r->id }}</td>
                <td class="small">{{ $r->updated_at }}</td>
                <td><x-ui.buttons.secondary :href="route('procurement.goods-receipts.show', $r)">{{ __('common.view') }}</x-ui.buttons.secondary></td>
            </x-ui.table.row>
        @empty
            <x-ui.table.row><td colspan="3" class="text-muted">—</td></x-ui.table.row>
        @endforelse
    </x-ui.table.table>

    <h5 class="mt-4">{{ __('operations_control.sections.stale_validated') }}</h5>
    <p class="small text-muted">{{ __('operations_control.stale.validated_help', ['hours' => $thresholds['validated_hours']]) }}</p>
    <x-ui.table.table :columns="[
        ['label' => __('operations_control.columns.receipt')],
        ['label' => __('operations_control.columns.updated')],
        ['label' => __('common.view')],
    ]">
        @forelse ($staleValidatedReceipts as $r)
            <x-ui.table.row>
                <td>#{{ $r->id }}</td>
                <td class="small">{{ $r->updated_at }}</td>
                <td><x-ui.buttons.secondary :href="route('procurement.goods-receipts.show', $r)">{{ __('common.view') }}</x-ui.buttons.secondary></td>
            </x-ui.table.row>
        @empty
            <x-ui.table.row><td colspan="3" class="text-muted">—</td></x-ui.table.row>
        @endforelse
    </x-ui.table.table>

    <h5 class="mt-4">{{ __('operations_control.sections.posting_signals') }}</h5>
    <p class="small text-muted">{{ __('operations_control.signals.filter_hint') }}</p>
    <x-ui.table.table :columns="[
        ['label' => __('operations_control.columns.severity')],
        ['label' => __('operations_control.columns.category')],
        ['label' => __('operations_control.columns.code')],
        ['label' => __('operations_control.columns.summary')],
        ['label' => __('operations_control.columns.correlation')],
        ['label' => __('operations_control.columns.when')],
        ['label' => __('operations_control.columns.ack')],
    ]">
        @forelse ($recentSignals as $ev)
            <x-ui.table.row>
                <td><span class="badge bg-{{ $severityBadge($ev->severity) }}">{{ $ev->severity }}</span></td>
                <td class="small">{{ $ev->category }}</td>
                <td class="small">{{ $ev->event_code }}</td>
                <td class="small">{{ \Illuminate\Support\Str::limit($ev->summary ?? '—', 80) }}</td>
                <td class="small">{{ $ev->correlation_id ?? '—' }}</td>
                <td class="small">{{ $ev->created_at }}</td>
                <td>
                    @can('acknowledge', $ev)
                        <form method="post" action="{{ route('operations.control-center.events.acknowledge', $ev) }}" class="d-inline">
                            @csrf
                            <button type="submit" class="btn btn-sm btn-outline-secondary">{{ __('operations_control.columns.ack') }}</button>
                        </form>
                    @endcan
                </td>
            </x-ui.table.row>
        @empty
            <x-ui.table.row><td colspan="7" class="text-muted">{{ __('operations_control.signals.empty') }}</td></x-ui.table.row>
        @endforelse
    </x-ui.table.table>

    @php $overSignals = $recentSignals->where('category', 'over_receive_attempt'); @endphp
    <h5 class="mt-4">{{ __('operations_control.sections.over_receive') }}</h5>
    <x-ui.table.table :columns="[
        ['label' => __('operations_control.columns.when')],
        ['label' => __('operations_control.columns.code')],
        ['label' => __('operations_control.columns.summary')],
    ]">
        @forelse ($overSignals as $ev)
            <x-ui.table.row>
                <td class="small">{{ $ev->created_at }}</td>
                <td class="small">{{ $ev->event_code }}</td>
                <td class="small">{{ \Illuminate\Support\Str::limit($ev->summary ?? '—', 120) }}</td>
            </x-ui.table.row>
        @empty
            <x-ui.table.row><td colspan="3" class="text-muted">—</td></x-ui.table.row>
        @endforelse
    </x-ui.table.table>

    <h5 class="mt-4">{{ __('operations_control.retry.title') }}</h5>
    <p class="small text-muted">{{ __('operations_control.retry.body') }}</p>

    <x-inventory.vite-operations />
</x-ui.layout.page>
