@extends('layouts.admin', ['title' => 'صحة النظام'])

@section('content')
    <section class="ac-section">
        <div class="ac-panel">
            <div class="ac-panel-head">
                <div><h2>صحة النظام</h2><p>حالة النظام، الإعدادات التقنية، سجلات المراجعة، ونشاط التتبع.</p></div>
                <div class="ac-actions">
                    <a class="ac-btn secondary" href="{{ route('admin.audit-logs.index') }}">سجل التدقيق</a>
                    <a class="ac-btn secondary" href="{{ route('admin.exports.provider-logs') }}">تصدير لوجات المزود</a>
                </div>
            </div>
            <div class="ac-kpi-grid">
                @foreach($data['health'] as $label => $value)
                    <div class="ac-kpi"><span>{{ $label }}</span><strong style="font-size:20px">{{ $value }}</strong></div>
                @endforeach
            </div>
        </div>

        <div class="ac-grid-2">
            <div class="ac-panel">
                <div class="ac-panel-head"><h2>أعداد قاعدة البيانات</h2></div>
                <div class="ac-kpi-grid">
                    @foreach($data['counts'] as $label => $value)
                        <div class="ac-kpi"><span>{{ $label }}</span><strong>{{ number_format($value) }}</strong></div>
                    @endforeach
                </div>
            </div>
            <div class="ac-panel">
                <div class="ac-panel-head"><h2>آخر النشاط</h2></div>
                <div class="ac-list">
                    @forelse($data['activity'] as $event)
                        <div class="ac-list-row"><span>{{ $event->event_name }}</span><strong>{{ $event->entity_type ?: 'المتجر' }}</strong><em>{{ $event->occurred_at->diffForHumans() }}</em></div>
                    @empty
                        <div class="ac-empty">لا توجد أحداث حديثة.</div>
                    @endforelse
                </div>
            </div>
        </div>

        <div class="ac-grid-2">
            <div class="ac-panel">
                <div class="ac-panel-head"><h2>جاهزية الإنتاج</h2><span class="ac-badge blue">Checklist</span></div>
                <div class="ac-list">
                    @foreach($data['readiness'] as $check)
                        <div class="ac-list-row">
                            <span>{{ $check['label'] }}</span>
                            <strong class="ac-badge {{ $check['status'] ? 'green' : 'red' }}">{{ $check['status'] ? 'جاهز' : 'يحتاج ضبط' }}</strong>
                            <em>{{ $check['note'] }}</em>
                        </div>
                    @endforeach
                </div>
            </div>

            <div class="ac-panel">
                <div class="ac-panel-head"><h2>نبض المزودين</h2><a class="ac-btn secondary" href="{{ route('admin.providers.index') }}">فتح</a></div>
                <div class="ac-micro-grid">
                    <div><span>مزودين</span><strong>{{ $data['providerPulse']['providers'] }}</strong></div>
                    <div><span>ربوط</span><strong>{{ $data['providerPulse']['activeMappings'] }}</strong></div>
                    <div><span>صحي</span><strong>{{ $data['providerPulse']['healthy'] }}</strong></div>
                    <div><span>فشل اليوم</span><strong>{{ $data['providerPulse']['failedToday'] }}</strong></div>
                </div>
            </div>
        </div>

        <div class="ac-panel">
            <div class="ac-panel-head"><h2>سجل التدقيق</h2><a class="ac-btn secondary" href="{{ route('admin.audit-logs.index') }}">كل السجلات</a></div>
            <div class="ac-table-wrap">
                <table class="ac-table">
                    <thead><tr><th>المستخدم</th><th>الإجراء</th><th>العنصر</th><th>التاريخ</th></tr></thead>
                    <tbody>
                        @forelse($data['auditLogs'] as $log)
                            <tr><td>{{ $log->user?->display_name ?: $log->user?->name ?: 'النظام' }}</td><td>{{ $log->action }}</td><td>{{ $log->auditable_type }} #{{ $log->auditable_id }}</td><td>{{ $log->created_at->diffForHumans() }}</td></tr>
                        @empty
                            <tr><td colspan="4"><div class="ac-empty">لا توجد سجلات مراجعة.</div></td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </section>
@endsection
