@extends('layouts.default')
@section('title', $page_title)
@section('content')
<div class="container-fluid">
    @include('admin.partials.page-header')

    <div class="row">
        @foreach($snapshotCounts as $label => $count)
            <div class="col-xl-3 col-sm-6">
                <div class="card">
                    <div class="card-body">
                        <p class="text-muted mb-1">{{ $label }}</p>
                        <h3 class="mb-0">{{ number_format($count, 0, ',', '.') }}</h3>
                    </div>
                </div>
            </div>
        @endforeach
    </div>

    <div class="card">
        <div class="card-header d-flex justify-content-between align-items-center">
            <div>
                <h4 class="card-title mb-0">Toplu Senkronizasyon</h4>
                <small class="text-muted">Toplu işlem queue içine alınır. Worker çalışmıyorsa <code>php artisan queue:work</code> çalıştırılmalı.</small>
            </div>
        </div>
        <div class="card-body">
            <form action="{{ route('admin.sync.all') }}" method="POST" class="row g-3 align-items-end">
                @csrf
                <div class="col-md-3">
                    <label class="form-label">Platform</label>
                    <select name="platform" class="form-control">
                        <option value="">Tümü</option>
                        <option value="google_ads">Google Ads</option>
                        <option value="meta_ads">Meta Ads</option>
                        <option value="search_console">Search Console</option>
                        <option value="ga4">GA4</option>
                    </select>
                </div>
                <div class="col-md-3">
                    <label class="form-label">Başlangıç</label>
                    <input type="date" name="from" class="form-control" value="{{ now()->subDay()->toDateString() }}">
                </div>
                <div class="col-md-3">
                    <label class="form-label">Bitiş</label>
                    <input type="date" name="to" class="form-control" value="{{ now()->subDay()->toDateString() }}">
                </div>
                <div class="col-md-3">
                    <button class="btn btn-primary w-100">Queue ile Toplu Çalıştır</button>
                </div>
            </form>
        </div>
    </div>

    <div class="card">
        <div class="card-header"><h4 class="card-title mb-0">Bağlantılar</h4></div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-striped mb-0">
                    <thead>
                        <tr>
                            <th>Müşteri</th>
                            <th>Platform</th>
                            <th>Bağlantı</th>
                            <th>Durum</th>
                            <th>Son Senkron</th>
                            <th style="width: 360px;">Manuel Senkron</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($connections as $connection)
                            <tr>
                                <td>{{ $connection->client?->company_name }}</td>
                                <td>{{ strtoupper(str_replace('_', ' ', $connection->platform)) }}</td>
                                <td>{{ $connection->connection_name }}</td>
                                <td>@include('admin.partials.status-badge', ['status' => $connection->status])</td>
                                <td>{{ $connection->last_sync_at?->format('d.m.Y H:i') ?? '-' }}</td>
                                <td>
                                    <form action="{{ route('admin.sync.connection', $connection) }}" method="POST" class="d-flex gap-2">
                                        @csrf
                                        <input type="date" name="from" class="form-control form-control-sm" value="{{ now()->subDay()->toDateString() }}">
                                        <input type="date" name="to" class="form-control form-control-sm" value="{{ now()->subDay()->toDateString() }}">
                                        <button class="btn btn-sm btn-outline-primary">Çalıştır</button>
                                    </form>
                                </td>
                            </tr>
                        @empty
                            <tr><td colspan="6" class="text-center text-muted py-4">Bağlantı bulunamadı.</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
        @if($connections->hasPages())
            <div class="card-footer">{{ $connections->links() }}</div>
        @endif
    </div>

    <div class="card">
        <div class="card-header"><h4 class="card-title mb-0">Son API Logları</h4></div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table mb-0">
                    <thead><tr><th>Tarih</th><th>Platform</th><th>Endpoint</th><th>Kod</th><th>Hata</th></tr></thead>
                    <tbody>
                    @forelse($recentLogs as $log)
                        <tr>
                            <td>{{ $log->requested_at?->format('d.m.Y H:i') }}</td>
                            <td>{{ $log->platform }}</td>
                            <td>{{ \Illuminate\Support\Str::limit($log->endpoint, 60) }}</td>
                            <td>{{ $log->status_code ?: '-' }}</td>
                            <td>{{ $log->error_message ?: '-' }}</td>
                        </tr>
                    @empty
                        <tr><td colspan="5" class="text-center text-muted py-4">Henüz API logu yok.</td></tr>
                    @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
@endsection
