@extends('layouts.default')

@section('content')
<div class="container-fluid">
    <div class="page-titles d-flex align-items-center justify-content-between flex-wrap gap-2">
        <div>
            <h4>Ajans Operasyon Merkezi</h4>
            <p class="mb-0">Tüm müşterilerde rapor, bağlantı, kalite, mail ve uyarı durumunu tek ekrandan takip edin.</p>
        </div>
        <div class="d-flex gap-2">
            <a href="{{ route('admin.onboarding.index') }}" class="btn btn-primary btn-sm">Onboarding</a>
            <a href="{{ route('admin.first-report.index') }}" class="btn btn-outline-primary btn-sm">İlk Rapor</a>
            <a href="{{ route('admin.sync.quality') }}" class="btn btn-outline-secondary btn-sm">Kalite Kontrol</a>
        </div>
    </div>

    @if(session('success'))
        <div class="alert alert-success">{{ session('success') }}</div>
    @endif
    @if(session('error'))
        <div class="alert alert-danger">{{ session('error') }}</div>
    @endif

    <div class="row">
        @php($summary = $data['summary'])
        <div class="col-xl-2 col-sm-6">
            <div class="card"><div class="card-body"><span class="text-muted">Toplam Müşteri</span><h3 class="mb-0">{{ $summary['clients_total'] }}</h3></div></div>
        </div>
        <div class="col-xl-2 col-sm-6">
            <div class="card"><div class="card-body"><span class="text-muted">Hazır</span><h3 class="mb-0 text-success">{{ $summary['ready'] }}</h3></div></div>
        </div>
        <div class="col-xl-2 col-sm-6">
            <div class="card"><div class="card-body"><span class="text-muted">Kontrol Gerekli</span><h3 class="mb-0 text-warning">{{ $summary['warning'] }}</h3></div></div>
        </div>
        <div class="col-xl-2 col-sm-6">
            <div class="card"><div class="card-body"><span class="text-muted">Kritik</span><h3 class="mb-0 text-danger">{{ $summary['critical'] }}</h3></div></div>
        </div>
        <div class="col-xl-2 col-sm-6">
            <div class="card"><div class="card-body"><span class="text-muted">İlk Rapor Bekleyen</span><h3 class="mb-0">{{ $summary['needs_first_report'] }}</h3></div></div>
        </div>
        <div class="col-xl-2 col-sm-6">
            <div class="card"><div class="card-body"><span class="text-muted">API Hata / 24s</span><h3 class="mb-0 text-danger">{{ $summary['api_errors_24h'] }}</h3></div></div>
        </div>
    </div>

    <div class="card">
        <div class="card-body">
            <form method="GET" class="row g-3 align-items-end">
                <div class="col-lg-3">
                    <label class="form-label">Arama</label>
                    <input type="text" name="q" value="{{ $filters['q'] ?? '' }}" class="form-control" placeholder="Müşteri, kişi, e-posta...">
                </div>
                <div class="col-lg-2">
                    <label class="form-label">Platform</label>
                    <select name="platform" class="form-control">
                        <option value="">Tümü</option>
                        @foreach(['google_ads' => 'Google Ads', 'meta_ads' => 'Meta Ads', 'search_console' => 'Search Console', 'ga4' => 'GA4'] as $value => $label)
                            <option value="{{ $value }}" @selected(($filters['platform'] ?? '') === $value)>{{ $label }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-lg-2">
                    <label class="form-label">Müşteri Durumu</label>
                    <select name="status" class="form-control">
                        <option value="">Tümü</option>
                        <option value="active" @selected(($filters['status'] ?? '') === 'active')>Aktif</option>
                        <option value="passive" @selected(($filters['status'] ?? '') === 'passive')>Pasif</option>
                    </select>
                </div>
                <div class="col-lg-2">
                    <label class="form-label">Operasyon Durumu</label>
                    <select name="readiness" class="form-control">
                        <option value="">Tümü</option>
                        <option value="ready" @selected(($filters['readiness'] ?? '') === 'ready')>Hazır</option>
                        <option value="warning" @selected(($filters['readiness'] ?? '') === 'warning')>Kontrol Gerekli</option>
                        <option value="critical" @selected(($filters['readiness'] ?? '') === 'critical')>Kritik</option>
                    </select>
                </div>
                <div class="col-lg-3 d-flex gap-2">
                    <button class="btn btn-primary">Filtrele</button>
                    <a href="{{ route('admin.operations.index') }}" class="btn btn-light">Temizle</a>
                </div>
            </form>
        </div>
    </div>

    <div class="row">
        <div class="col-xl-8">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="mb-0">Müşteri Operasyon Listesi</h5>
                    <span class="badge badge-light">{{ $data['clients']->count() }} kayıt</span>
                </div>
                <div class="card-body p-0">
                    <div class="table-responsive">
                        <table class="table table-hover mb-0">
                            <thead>
                            <tr>
                                <th>Müşteri</th>
                                <th>Durum</th>
                                <th>Bağlantı</th>
                                <th>Kalite</th>
                                <th>Son Rapor</th>
                                <th>Uyarı</th>
                                <th>Aksiyon</th>
                            </tr>
                            </thead>
                            <tbody>
                            @forelse($data['clients'] as $card)
                                <tr>
                                    <td>
                                        <strong>{{ $card['client']->company_name }}</strong><br>
                                        <small class="text-muted">{{ $card['client']->email ?: $card['client']->website }}</small>
                                    </td>
                                    <td><span class="badge {{ $card['badge_class'] }}">{{ $card['readiness_label'] }}</span></td>
                                    <td>
                                        <strong>{{ $card['connections_active'] }}/{{ $card['connections_total'] }}</strong><br>
                                        <small class="text-muted">{{ implode(', ', array_map(fn($p) => str_replace('_', ' ', $p), $card['platforms'])) ?: 'Bağlantı yok' }}</small>
                                    </td>
                                    <td>
                                        @if($card['avg_quality'] !== null)
                                            <strong>{{ $card['avg_quality'] }}</strong>/100
                                        @else
                                            <span class="text-muted">Yok</span>
                                        @endif
                                    </td>
                                    <td>
                                        @if($card['last_report'])
                                            {{ $card['last_report']->created_at?->format('d.m.Y H:i') }}<br>
                                            <small class="text-muted">{{ $card['last_report']->report_type }}</small>
                                        @else
                                            <span class="text-muted">Henüz yok</span>
                                        @endif
                                    </td>
                                    <td>
                                        <span class="badge badge-light">{{ $card['open_alerts'] }}</span>
                                        @if($card['critical_alerts'] > 0)
                                            <span class="badge badge-danger">{{ $card['critical_alerts'] }} kritik</span>
                                        @endif
                                    </td>
                                    <td>
                                        <a href="{{ route('admin.operations.show', $card['client']) }}" class="btn btn-sm btn-primary">Detay</a>
                                        @if($card['needs_first_report'])
                                            <a href="{{ route('admin.first-report.show', $card['client']) }}" class="btn btn-sm btn-outline-primary">İlk Rapor</a>
                                        @endif
                                    </td>
                                </tr>
                            @empty
                                <tr><td colspan="7" class="text-center text-muted py-4">Kayıt bulunamadı.</td></tr>
                            @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-xl-4">
            <div class="card">
                <div class="card-header"><h5 class="mb-0">Kritik İlk 10</h5></div>
                <div class="card-body">
                    @forelse($data['critical_items'] as $card)
                        <div class="border-bottom pb-3 mb-3">
                            <strong>{{ $card['client']->company_name }}</strong>
                            <div class="mt-2">
                                @foreach(array_slice($card['actions'], 0, 2) as $action)
                                    <div class="small text-muted">• {{ $action['title'] }}</div>
                                @endforeach
                            </div>
                            <a href="{{ route('admin.operations.show', $card['client']) }}" class="small">Detaya git</a>
                        </div>
                    @empty
                        <p class="text-muted mb-0">Kritik müşteri yok. Mis gibi.</p>
                    @endforelse
                </div>
            </div>

            <div class="card">
                <div class="card-header"><h5 class="mb-0">Yaklaşan Raporlar</h5></div>
                <div class="card-body">
                    @forelse($data['upcoming_reports'] as $schedule)
                        <div class="d-flex justify-content-between border-bottom py-2">
                            <div>
                                <strong>{{ $schedule->client?->company_name }}</strong><br>
                                <small class="text-muted">{{ ucfirst($schedule->frequency) }}</small>
                            </div>
                            <small>{{ $schedule->next_run_at?->format('d.m H:i') }}</small>
                        </div>
                    @empty
                        <p class="text-muted mb-0">Yaklaşan plan yok.</p>
                    @endforelse
                </div>
            </div>

            <div class="card">
                <div class="card-header"><h5 class="mb-0">Son Hatalar</h5></div>
                <div class="card-body">
                    @forelse($data['recent_api_errors'] as $log)
                        <div class="border-bottom pb-2 mb-2">
                            <strong>{{ $log->client?->company_name ?: 'Genel' }}</strong>
                            <div class="small text-muted">{{ strtoupper($log->platform) }} · {{ $log->created_at?->diffForHumans() }}</div>
                            <div class="small">{{ \Illuminate\Support\Str::limit($log->error_message, 120) }}</div>
                        </div>
                    @empty
                        <p class="text-muted mb-0">Son API hatası yok.</p>
                    @endforelse
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
