@extends('layouts.default')

@section('content')
@php($card = $data['card'])
<div class="container-fluid">
    <div class="page-titles d-flex align-items-center justify-content-between flex-wrap gap-2">
        <div>
            <h4>{{ $client->company_name }} · Operasyon Detayı</h4>
            <p class="mb-0">Müşteri bazlı bağlantı, rapor, mail, kalite ve uyarı takibi.</p>
        </div>
        <div class="d-flex gap-2">
            <a href="{{ route('admin.operations.index') }}" class="btn btn-light btn-sm">Operasyon Merkezi</a>
            <a href="{{ route('admin.clients.show', $client) }}" class="btn btn-outline-secondary btn-sm">Müşteri Kartı</a>
            <a href="{{ route('admin.first-report.show', $client) }}" class="btn btn-primary btn-sm">İlk Rapor</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">
        <div class="col-xl-3 col-sm-6"><div class="card"><div class="card-body"><span class="text-muted">Operasyon Durumu</span><h4 class="mb-0"><span class="badge {{ $card['badge_class'] }}">{{ $card['readiness_label'] }}</span></h4></div></div></div>
        <div class="col-xl-3 col-sm-6"><div class="card"><div class="card-body"><span class="text-muted">Bağlantılar</span><h3 class="mb-0">{{ $card['connections_active'] }}/{{ $card['connections_total'] }}</h3></div></div></div>
        <div class="col-xl-3 col-sm-6"><div class="card"><div class="card-body"><span class="text-muted">Kalite Skoru</span><h3 class="mb-0">{{ $card['avg_quality'] !== null ? $card['avg_quality'].'/100' : 'Yok' }}</h3></div></div></div>
        <div class="col-xl-3 col-sm-6"><div class="card"><div class="card-body"><span class="text-muted">Açık Uyarı</span><h3 class="mb-0">{{ $card['open_alerts'] }}</h3></div></div></div>
    </div>

    <div class="row">
        <div class="col-xl-5">
            <div class="card">
                <div class="card-header"><h5 class="mb-0">Önerilen Aksiyonlar</h5></div>
                <div class="card-body">
                    @forelse($card['actions'] as $action)
                        <div class="border-bottom pb-3 mb-3">
                            <div class="d-flex justify-content-between gap-2">
                                <strong>{{ $action['title'] }}</strong>
                                <span class="badge {{ $action['priority'] === 'critical' ? 'badge-danger' : ($action['priority'] === 'high' ? 'badge-warning' : 'badge-info') }}">{{ strtoupper($action['priority']) }}</span>
                            </div>
                            <p class="text-muted mb-2">{{ $action['description'] }}</p>
                            @if(!empty($action['url']))
                                <a href="{{ $action['url'] }}" class="btn btn-sm btn-outline-primary">İlgili ekrana git</a>
                            @endif
                        </div>
                    @empty
                        <p class="text-muted mb-0">Şu an aksiyon bekleyen kritik konu yok.</p>
                    @endforelse
                </div>
            </div>

            <div class="card">
                <div class="card-header"><h5 class="mb-0">Manuel Operasyon Görevi Ekle</h5></div>
                <div class="card-body">
                    <form method="POST" action="{{ route('admin.operations.tasks.store', $client) }}">
                        @csrf
                        <div class="mb-3">
                            <label class="form-label">Başlık</label>
                            <input type="text" name="title" class="form-control" required placeholder="Örn: Meta token yenilenecek">
                        </div>
                        <div class="mb-3">
                            <label class="form-label">Açıklama</label>
                            <textarea name="description" class="form-control" rows="3"></textarea>
                        </div>
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Kategori</label>
                                <select name="category" class="form-control">
                                    <option value="general">Genel</option>
                                    <option value="connection">API Bağlantısı</option>
                                    <option value="report">Rapor</option>
                                    <option value="mail">Mail</option>
                                    <option value="quality">Veri Kalitesi</option>
                                </select>
                            </div>
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Öncelik</label>
                                <select name="priority" class="form-control">
                                    <option value="critical">Kritik</option>
                                    <option value="high">Yüksek</option>
                                    <option value="medium" selected>Orta</option>
                                    <option value="low">Düşük</option>
                                </select>
                            </div>
                        </div>
                        <button class="btn btn-primary">Görev Ekle</button>
                    </form>
                </div>
            </div>
        </div>

        <div class="col-xl-7">
            <div class="card">
                <div class="card-header"><h5 class="mb-0">Bağlantı Durumu</h5></div>
                <div class="card-body p-0">
                    <div class="table-responsive">
                        <table class="table mb-0">
                            <thead><tr><th>Platform</th><th>Hesap</th><th>Durum</th><th>Kalite</th><th>Son Sync</th><th>İşlem</th></tr></thead>
                            <tbody>
                            @forelse($data['connections'] as $row)
                                @php($connection = $row['model'])
                                <tr>
                                    <td><strong>{{ str_replace('_', ' ', strtoupper($connection->platform)) }}</strong></td>
                                    <td>{{ $connection->account_name ?: $connection->connection_name }}<br><small class="text-muted">{{ $connection->account_id }}</small></td>
                                    <td>{{ $row['status_label'] }}</td>
                                    <td>{{ $row['quality']['score'] !== null ? $row['quality']['score'].'/100' : '-' }}</td>
                                    <td>{{ $connection->last_sync_at?->format('d.m.Y H:i') ?: '-' }}</td>
                                    <td>
                                        <a href="{{ route('admin.integrations.preview.index', ['connection_id' => $connection->id]) }}" class="btn btn-xs btn-outline-primary">Önizle</a>
                                        <form method="POST" action="{{ route('admin.sync.connection', $connection) }}" class="d-inline">
                                            @csrf
                                            <button class="btn btn-xs btn-outline-secondary">Sync</button>
                                        </form>
                                    </td>
                                </tr>
                                @if($row['last_error'] || $row['last_api_error'])
                                    <tr><td colspan="6" class="bg-light text-danger small">{{ $row['last_error'] ?: \Illuminate\Support\Str::limit($row['last_api_error']?->error_message, 220) }}</td></tr>
                                @endif
                            @empty
                                <tr><td colspan="6" class="text-center text-muted py-4">Bağlantı yok.</td></tr>
                            @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

            <div class="card">
                <div class="card-header"><h5 class="mb-0">Son Raporlar</h5></div>
                <div class="card-body p-0">
                    <div class="table-responsive">
                        <table class="table mb-0">
                            <thead><tr><th>Rapor</th><th>Tarih</th><th>Durum</th><th>Gönderim</th><th></th></tr></thead>
                            <tbody>
                            @forelse($data['reports'] as $report)
                                <tr>
                                    <td>{{ $report->report_type }}</td>
                                    <td>{{ $report->date_from?->format('d.m.Y') }} - {{ $report->date_to?->format('d.m.Y') }}</td>
                                    <td><span class="badge badge-light">{{ $report->status }}</span></td>
                                    <td>{{ $report->sent_at?->format('d.m.Y H:i') ?: '-' }}</td>
                                    <td><a href="{{ route('admin.reports.show', $report) }}" class="btn btn-xs btn-primary">Aç</a></td>
                                </tr>
                            @empty
                                <tr><td colspan="5" class="text-center text-muted py-4">Henüz rapor yok.</td></tr>
                            @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

            <div class="card">
                <div class="card-header"><h5 class="mb-0">Manuel Görevler</h5></div>
                <div class="card-body">
                    @forelse($data['manual_tasks'] as $task)
                        <div class="d-flex justify-content-between align-items-start border-bottom pb-2 mb-2 gap-3">
                            <div>
                                <strong>{{ $task->title }}</strong>
                                <div class="small text-muted">{{ $task->description }}</div>
                                <span class="badge {{ $task->priorityBadgeClass() }}">{{ $task->priorityLabel() }}</span>
                                <span class="badge badge-light">{{ $task->statusLabel() }}</span>
                            </div>
                            <form method="POST" action="{{ route('admin.operations.tasks.update', $task) }}">
                                @csrf
                                @method('PATCH')
                                <select name="status" class="form-control form-control-sm" onchange="this.form.submit()">
                                    @foreach(['open' => 'Açık', 'in_progress' => 'İşlemde', 'waiting' => 'Bekliyor', 'completed' => 'Tamamlandı', 'cancelled' => 'İptal'] as $value => $label)
                                        <option value="{{ $value }}" @selected($task->status === $value)>{{ $label }}</option>
                                    @endforeach
                                </select>
                            </form>
                        </div>
                    @empty
                        <p class="text-muted mb-0">Manuel görev yok.</p>
                    @endforelse
                </div>
            </div>

            <div class="card">
                <div class="card-header"><h5 class="mb-0">Son Mail / API Hataları</h5></div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-6">
                            <h6>Mail Logları</h6>
                            @forelse($data['emails'] as $email)
                                <div class="small border-bottom py-2">
                                    <strong>{{ $email->status }}</strong> · {{ $email->created_at?->format('d.m H:i') }}<br>
                                    {{ \Illuminate\Support\Str::limit($email->subject, 80) }}
                                    @if($email->error_message)<div class="text-danger">{{ \Illuminate\Support\Str::limit($email->error_message, 120) }}</div>@endif
                                </div>
                            @empty
                                <p class="text-muted small">Mail logu yok.</p>
                            @endforelse
                        </div>
                        <div class="col-md-6">
                            <h6>API Hataları</h6>
                            @forelse($data['api_errors'] as $log)
                                <div class="small border-bottom py-2">
                                    <strong>{{ strtoupper($log->platform) }}</strong> · {{ $log->created_at?->format('d.m H:i') }}<br>
                                    <span class="text-danger">{{ \Illuminate\Support\Str::limit($log->error_message, 140) }}</span>
                                </div>
                            @empty
                                <p class="text-muted small">API hatası yok.</p>
                            @endforelse
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
