@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>{{ $client->company_name }} · Sağlık Skoru</h4>
            <p class="mb-0">Müşterinin operasyon, API, veri, mail ve rapor sağlığını detaylı kontrol edin.</p>
        </div>
        <div class="d-flex gap-2">
            <form method="POST" action="{{ route('admin.health-scores.calculate-client', $client) }}">
                @csrf
                <button class="btn btn-primary btn-sm">Skoru Kaydet / Güncelle</button>
            </form>
            <a href="{{ route('admin.health-scores.index') }}" class="btn btn-outline-secondary btn-sm">Listeye Dön</a>
        </div>
    </div>

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

    @php($card = $data['card'])
    <div class="row">
        <div class="col-xl-3 col-sm-6">
            <div class="card">
                <div class="card-body">
                    <span class="text-muted">Genel Skor</span>
                    <div class="d-flex align-items-center justify-content-between">
                        <h2 class="mb-0">{{ $card['score'] }}/100</h2>
                        <span class="badge {{ $card['badge_class'] }}">{{ $card['status_label'] }}</span>
                    </div>
                    <div class="progress mt-3" style="height:10px;"><div class="progress-bar" style="width: {{ $card['score'] }}%"></div></div>
                </div>
            </div>
        </div>
        @foreach([
            'api' => 'API',
            'data_quality' => 'Veri Kalitesi',
            'report' => 'Rapor',
            'mail' => 'Mail',
            'alert' => 'Uyarı',
            'task' => 'Görev',
            'onboarding' => 'Onboarding',
        ] as $key => $label)
            <div class="col-xl-3 col-sm-6">
                <div class="card">
                    <div class="card-body">
                        <span class="text-muted">{{ $label }}</span>
                        <h3 class="mb-2">{{ $card['breakdown'][$key]['score'] }}</h3>
                        <div class="small text-muted">{{ $card['breakdown'][$key]['label'] ?? '' }}</div>
                    </div>
                </div>
            </div>
        @endforeach
    </div>

    <div class="row">
        <div class="col-xl-7">
            <div class="card">
                <div class="card-header"><h5 class="mb-0">Riskler</h5></div>
                <div class="card-body">
                    @forelse($card['risk_items'] as $risk)
                        <div class="border rounded p-3 mb-3">
                            <div class="d-flex justify-content-between gap-2">
                                <div>
                                    <strong>{{ $risk['title'] }}</strong>
                                    <div class="text-muted small mt-1">{{ $risk['description'] ?? '' }}</div>
                                </div>
                                <span class="badge {{ ($risk['priority'] ?? '') === 'critical' ? 'badge-dark' : (($risk['priority'] ?? '') === 'high' ? 'badge-danger' : 'badge-warning') }}">
                                    {{ strtoupper($risk['priority'] ?? 'medium') }}
                                </span>
                            </div>
                            @if(! empty($risk['url']))
                                <a href="{{ $risk['url'] }}" class="btn btn-sm btn-outline-primary mt-3">İlgili Ekrana Git</a>
                            @endif
                        </div>
                    @empty
                        <p class="text-muted mb-0">Bu müşteri için kritik risk görünmüyor.</p>
                    @endforelse
                </div>
            </div>

            <div class="card">
                <div class="card-header"><h5 class="mb-0">Önerilen Aksiyonlar</h5></div>
                <div class="card-body">
                    @forelse($card['recommendations'] as $recommendation)
                        <div class="d-flex gap-2 mb-2">
                            <span class="text-success">✓</span>
                            <span>{{ $recommendation }}</span>
                        </div>
                    @empty
                        <p class="text-muted mb-0">Ek aksiyon önerisi yok.</p>
                    @endforelse
                </div>
            </div>
        </div>
        <div class="col-xl-5">
            <div class="card">
                <div class="card-header"><h5 class="mb-0">Platform Durumu</h5></div>
                <div class="card-body p-0">
                    <div class="table-responsive">
                        <table class="table mb-0">
                            <thead><tr><th>Platform</th><th>Durum</th><th>Kalite</th><th>Son Sync</th></tr></thead>
                            <tbody>
                                @forelse($data['platforms'] as $platform)
                                    <tr>
                                        <td><a href="{{ $platform['url'] }}">{{ $platform['platform_label'] }}</a></td>
                                        <td><span class="badge {{ $platform['status'] === 'active' ? 'badge-success' : 'badge-warning' }}">{{ $platform['status'] }}</span></td>
                                        <td>{{ $platform['quality_score'] ?? '-' }}</td>
                                        <td>{{ $platform['last_sync_at'] ? \Illuminate\Support\Carbon::parse($platform['last_sync_at'])->format('d.m.Y H:i') : '-' }}</td>
                                    </tr>
                                @empty
                                    <tr><td colspan="4" class="text-center text-muted py-3">Bağlantı yok.</td></tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

            <div class="card">
                <div class="card-header"><h5 class="mb-0">Skor Geçmişi</h5></div>
                <div class="card-body p-0">
                    <div class="table-responsive">
                        <table class="table mb-0">
                            <thead><tr><th>Tarih</th><th>Skor</th><th>Durum</th></tr></thead>
                            <tbody>
                                @forelse($data['history'] as $row)
                                    <tr>
                                        <td>{{ $row->score_date?->format('d.m.Y') }}</td>
                                        <td><strong>{{ $row->score }}</strong></td>
                                        <td><span class="badge {{ $row->badgeClass() }}">{{ $row->statusLabel() }}</span></td>
                                    </tr>
                                @empty
                                    <tr><td colspan="3" class="text-center text-muted py-3">Geçmiş skor yok.</td></tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

            <div class="card">
                <div class="card-header"><h5 class="mb-0">Açık Görevler / Uyarılar</h5></div>
                <div class="card-body">
                    <h6>Görevler</h6>
                    @forelse($data['open_tasks'] as $task)
                        <div class="border-bottom py-2 small">
                            <strong>{{ $task->title }}</strong>
                            <div class="text-muted">{{ $task->priority }} · {{ $task->status }}</div>
                        </div>
                    @empty
                        <p class="text-muted small">Açık görev yok.</p>
                    @endforelse

                    <h6 class="mt-3">Uyarılar</h6>
                    @forelse($data['open_alerts']->take(8) as $alert)
                        <div class="border-bottom py-2 small">
                            <strong>{{ $alert->title ?? $alert->message ?? 'Uyarı' }}</strong>
                            <div class="text-muted">{{ $alert->severity ?? '-' }}</div>
                        </div>
                    @empty
                        <p class="text-muted small mb-0">Açık uyarı yok.</p>
                    @endforelse
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
