@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>Müşteri Sağlık Skoru</h4>
            <p class="mb-0">API, veri kalitesi, rapor, mail, uyarı ve görev durumlarına göre müşterilerin risk seviyesini tek ekrandan takip edin.</p>
        </div>
        <div class="d-flex gap-2">
            <form method="POST" action="{{ route('admin.health-scores.calculate-all') }}">
                @csrf
                <button class="btn btn-primary btn-sm">Skorları Güncelle</button>
            </form>
            <a href="{{ route('admin.operations.index') }}" class="btn btn-outline-secondary btn-sm">Operasyon Merkezi</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

    @php($summary = $data['summary'])
    <div class="row">
        <div class="col-xl-2 col-sm-6"><div class="card"><div class="card-body"><span class="text-muted">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">Ortalama Skor</span><h3 class="mb-0 text-primary">{{ $summary['average_score'] }}</h3></div></div></div>
        <div class="col-xl-2 col-sm-6"><div class="card"><div class="card-body"><span class="text-muted">Çok İyi</span><h3 class="mb-0 text-success">{{ $summary['excellent'] }}</h3></div></div></div>
        <div class="col-xl-2 col-sm-6"><div class="card"><div class="card-body"><span class="text-muted">Takip</span><h3 class="mb-0 text-warning">{{ $summary['watch'] }}</h3></div></div></div>
        <div class="col-xl-2 col-sm-6"><div class="card"><div class="card-body"><span class="text-muted">Riskli</span><h3 class="mb-0 text-danger">{{ $summary['risk'] }}</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-dark">{{ $summary['critical'] }}</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, e-posta, web sitesi">
                </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>
                        @foreach(['active' => 'Aktif', 'passive' => 'Pasif'] as $value => $label)
                            <option value="{{ $value }}" @selected(($filters['status'] ?? '') === $value)>{{ $label }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-lg-2">
                    <label class="form-label">Sağlık Durumu</label>
                    <select name="health_status" class="form-control">
                        <option value="">Tümü</option>
                        @foreach(['excellent' => 'Çok İyi', 'healthy' => 'Sağlıklı', 'watch' => 'Takip Gerekli', 'risk' => 'Riskli', 'critical' => 'Kritik'] as $value => $label)
                            <option value="{{ $value }}" @selected(($filters['health_status'] ?? '') === $value)>{{ $label }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-lg-1">
                    <label class="form-label">Min</label>
                    <input type="number" name="min_score" value="{{ $filters['min_score'] ?? '' }}" class="form-control" min="0" max="100">
                </div>
                <div class="col-lg-1">
                    <label class="form-label">Max</label>
                    <input type="number" name="max_score" value="{{ $filters['max_score'] ?? '' }}" class="form-control" min="0" max="100">
                </div>
                <div class="col-lg-1 d-grid">
                    <button class="btn btn-primary">Filtrele</button>
                </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 Skorları</h5>
                    <span class="text-muted small">{{ $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>Skor</th>
                                    <th>Durum</th>
                                    <th>API</th>
                                    <th>Kalite</th>
                                    <th>Rapor</th>
                                    <th>Risk</th>
                                    <th class="text-end">İşlem</th>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse($data['clients'] as $card)
                                    @php($client = $card['client'])
                                    <tr>
                                        <td>
                                            <strong>{{ $client->company_name }}</strong>
                                            <div class="text-muted small">{{ $client->website ?: $client->email }}</div>
                                        </td>
                                        <td style="min-width:140px">
                                            <div class="d-flex align-items-center gap-2">
                                                <strong>{{ $card['score'] }}</strong>
                                                <div class="progress flex-fill" style="height:8px; min-width:80px;">
                                                    <div class="progress-bar" role="progressbar" style="width: {{ $card['score'] }}%"></div>
                                                </div>
                                            </div>
                                        </td>
                                        <td><span class="badge {{ $card['badge_class'] }}">{{ $card['status_label'] }}</span></td>
                                        <td>{{ $card['breakdown']['api']['score'] }}</td>
                                        <td>{{ $card['breakdown']['data_quality']['score'] }}</td>
                                        <td>{{ $card['breakdown']['report']['score'] }}</td>
                                        <td>
                                            @if(count($card['risk_items']) > 0)
                                                <span class="badge badge-warning">{{ count($card['risk_items']) }} risk</span>
                                            @else
                                                <span class="badge badge-success">Temiz</span>
                                            @endif
                                        </td>
                                        <td class="text-end">
                                            <div class="d-flex justify-content-end gap-1">
                                                <a href="{{ route('admin.health-scores.show', $client) }}" class="btn btn-sm btn-outline-primary">Detay</a>
                                                <form method="POST" action="{{ route('admin.health-scores.calculate-client', $client) }}">
                                                    @csrf
                                                    <button class="btn btn-sm btn-outline-secondary">Güncelle</button>
                                                </form>
                                            </div>
                                        </td>
                                    </tr>
                                @empty
                                    <tr><td colspan="8" 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">Öncelikli Riskler</h5></div>
                <div class="card-body">
                    @forelse($data['risk_clients'] as $card)
                        <div class="border-bottom pb-3 mb-3">
                            <div class="d-flex justify-content-between">
                                <strong>{{ $card['client']->company_name }}</strong>
                                <span class="badge {{ $card['badge_class'] }}">{{ $card['score'] }}</span>
                            </div>
                            @foreach(array_slice($card['risk_items'], 0, 2) as $risk)
                                <div class="small mt-2">
                                    <span class="text-danger">●</span> {{ $risk['title'] }}
                                </div>
                            @endforeach
                            <a href="{{ route('admin.health-scores.show', $card['client']) }}" class="small">Detaya git</a>
                        </div>
                    @empty
                        <p class="text-muted mb-0">Kritik/riskli müşteri bulunmuyor.</p>
                    @endforelse
                </div>
            </div>

            <div class="card">
                <div class="card-header"><h5 class="mb-0">Son Kaydedilen Skorlar</h5></div>
                <div class="card-body">
                    @forelse($data['recent_scores'] as $score)
                        <div class="d-flex justify-content-between border-bottom py-2">
                            <span>{{ $score->client?->company_name }}</span>
                            <strong>{{ $score->score }}/100</strong>
                        </div>
                    @empty
                        <p class="text-muted mb-0">Henüz kaydedilmiş skor yok. Üstten “Skorları Güncelle” butonuna basın.</p>
                    @endforelse
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
