@extends('admin.layouts.app')
@section('title', 'Mobil Uygulama')
@section('page-title', 'Mobil Uygulama Yönetimi')

@section('content')

{{-- Bakım modu uyarısı --}}
@if($settings['mobile_maintenance_mode'] == '1')
<div class="alert alert-danger d-flex align-items-center gap-10 mb-4" style="gap:10px">
    <i class="bi bi-tools" style="font-size:18px"></i>
    <div style="font-size:13.5px;font-weight:700">Bakım Modu AKTİF — Kullanıcılar uygulamaya giremez!</div>
</div>
@endif

{{-- Stats --}}
<div class="row g-3 mb-4">
    <div class="col-6 col-md-2">
        <div class="stat-card">
            <div class="stat-value" style="font-size:22px">{{ number_format($stats['total_users']) }}</div>
            <div class="stat-label mt-1">Kayıtlı Kullanıcı</div>
        </div>
    </div>
    <div class="col-6 col-md-2">
        <div class="stat-card">
            <div class="stat-value" style="font-size:22px;color:var(--success)">{{ number_format($stats['active_30d']) }}</div>
            <div class="stat-label mt-1">Aktif (30 gün)</div>
        </div>
    </div>
    <div class="col-6 col-md-2">
        <div class="stat-card">
            <div class="stat-value" style="font-size:22px;color:var(--info)">{{ number_format($stats['fcm_tokens']) }}</div>
            <div class="stat-label mt-1">Push Token</div>
        </div>
    </div>
    <div class="col-6 col-md-2">
        <div class="stat-card">
            <div class="stat-value" style="font-size:22px;color:var(--warning)">{{ number_format($stats['notifs_unread']) }}</div>
            <div class="stat-label mt-1">Okunmamış</div>
        </div>
    </div>
    <div class="col-6 col-md-2">
        <div class="stat-card">
            <div class="stat-value" style="font-size:22px">{{ number_format($stats['notifications_sent']) }}</div>
            <div class="stat-label mt-1">Toplam Bildirim</div>
        </div>
    </div>
    <div class="col-6 col-md-2">
        <div class="stat-card">
            <div class="stat-value" style="font-size:22px;color:var(--accent-lt)">{{ number_format($stats['notifs_today']) }}</div>
            <div class="stat-label mt-1">Bugün Gönderilen</div>
        </div>
    </div>
</div>

<div class="row g-4">

    {{-- ── Sol: Ayarlar ── --}}
    <div class="col-md-8">
        <div class="card">
            <div class="card-header">
                <i class="bi bi-sliders" style="color:var(--accent-lt)"></i>
                <h6>Uygulama Ayarları</h6>
            </div>
            <div class="card-body">
                <form method="POST" action="{{ route('admin.mobile.update') }}">
                    @csrf

                    {{-- Bakım Modu --}}
                    <div class="p-4 rounded mb-4" style="background:var(--danger-dim);border:1px solid rgba(239,68,68,.3)">
                        <div class="d-flex align-items-center justify-content-between gap-3 mb-3">
                            <div>
                                <div style="font-size:14px;font-weight:700;color:var(--danger);display:flex;align-items:center;gap:7px">
                                    <i class="bi bi-tools"></i> Bakım Modu
                                </div>
                                <div style="font-size:12.5px;color:var(--text);margin-top:3px">
                                    Açıldığında tüm kullanıcılar uygulamaya giremez.
                                </div>
                            </div>
                            <div class="form-check form-switch mb-0">
                                <input type="hidden" name="mobile_maintenance_mode" value="0">
                                <input class="form-check-input" type="checkbox" name="mobile_maintenance_mode" value="1"
                                       id="maintenanceToggle" role="switch"
                                       {{ $settings['mobile_maintenance_mode']=='1'?'checked':'' }}
                                       style="width:2.5rem;height:1.3rem;cursor:pointer">
                            </div>
                        </div>
                        <label class="form-label">Bakım Mesajı</label>
                        <textarea class="form-control" name="mobile_maintenance_message" rows="2">{{ $settings['mobile_maintenance_message'] }}</textarea>
                    </div>

                    {{-- Zorla Güncelleme --}}
                    <div class="p-4 rounded mb-4" style="background:var(--warning-dim);border:1px solid rgba(245,158,11,.3)">
                        <div style="font-size:14px;font-weight:700;color:var(--warning);display:flex;align-items:center;gap:7px;margin-bottom:4px">
                            <i class="bi bi-arrow-repeat"></i> Zorla Güncelleme
                        </div>
                        <div style="font-size:12.5px;color:var(--text);margin-bottom:16px">
                            Minimum sürümü geçemeyen kullanıcılar zorunlu güncelleme ekranı görür.
                        </div>
                        <div class="row g-3">
                            <div class="col-md-6">
                                <label class="form-label">Minimum Sürüm <span style="color:var(--danger)">*</span></label>
                                <input type="text" class="form-control" name="mobile_min_version"
                                       value="{{ $settings['mobile_min_version'] }}" placeholder="1.0.2">
                                <div class="form-text">Bu sürümden eskiler güncellemeye zorlanır</div>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label">Güncel Sürüm <span style="color:var(--danger)">*</span></label>
                                <input type="text" class="form-control" name="mobile_current_version"
                                       value="{{ $settings['mobile_current_version'] }}" placeholder="1.0.5">
                                <div class="form-text">Kullanıcılara gösterilen güncel sürüm</div>
                            </div>
                            <div class="col-12">
                                <label class="form-label">APK İndirme Linki</label>
                                <input type="url" class="form-control" name="mobile_apk_url"
                                       value="{{ $settings['mobile_apk_url'] }}"
                                       placeholder="https://animexe.com/app/animexe.apk">
                                <div class="form-text">Boş bırakılırsa Play Store'a yönlendirir</div>
                            </div>
                            <div class="col-12">
                                <label class="form-label">Güncelleme Mesajı</label>
                                <textarea class="form-control" name="mobile_force_update_msg" rows="2">{{ $settings['mobile_force_update_msg'] }}</textarea>
                            </div>
                        </div>
                    </div>

                    <button type="submit" class="btn btn-primary">
                        <i class="bi bi-floppy"></i> Ayarları Kaydet
                    </button>
                </form>
            </div>
        </div>
    </div>

    {{-- ── Sağ: Durum & API ── --}}
    <div class="col-md-4">

        <div class="card mb-3">
            <div class="card-header">
                <i class="bi bi-activity" style="color:var(--success)"></i>
                <h6>Anlık Durum</h6>
            </div>
            <div class="card-body p-0">
                @foreach([
                    ['Uygulama', $settings['mobile_maintenance_mode']=='1' ? '<span class="badge bg-danger">Bakımda</span>' : '<span class="badge bg-success">Çalışıyor</span>'],
                    ['Minimum Sürüm', '<code>'.$settings['mobile_min_version'].'</code>'],
                    ['Güncel Sürüm', '<code>'.$settings['mobile_current_version'].'</code>'],
                    ['Push Token\'lı Cihaz', '<strong>'.number_format($stats['fcm_tokens']).'</strong>'],
                ] as [$lbl, $val])
                <div style="display:flex;justify-content:space-between;align-items:center;padding:10px 16px;border-bottom:1px solid var(--border)">
                    <span style="font-size:12.5px;color:var(--text2)">{{ $lbl }}</span>
                    <span style="font-size:13px;color:var(--text)">{!! $val !!}</span>
                </div>
                @endforeach
            </div>
        </div>

        <div class="card">
            <div class="card-header">
                <i class="bi bi-code-slash" style="color:var(--info)"></i>
                <h6>API Endpoint</h6>
            </div>
            <div class="card-body">
                <div style="font-size:12.5px;color:var(--text2);margin-bottom:8px">Uygulama başlangıcında kontrol:</div>
                <code style="display:block;background:var(--bg);border:1px solid var(--border);border-radius:8px;padding:8px 12px;font-size:12px;color:var(--info);word-break:break-all">GET /api/app-status</code>
                <div style="font-size:12.5px;color:var(--text2);margin:12px 0 6px">Örnek yanıt:</div>
                <pre style="background:var(--bg);border:1px solid var(--border);border-radius:8px;padding:10px 12px;font-size:11.5px;color:var(--text);overflow-x:auto;margin:0">{
  "maintenance": false,
  "min_version": "1.0.0",
  "current_version": "1.0.0",
  "force_update": false
}</pre>
            </div>
        </div>

    </div>
</div>

@endsection
