@extends('admin.layouts.app')
@section('title', $user->name)
@section('page-title', $user->name)

@push('styles')
<style>
.info-row {
    display:flex; justify-content:space-between; align-items:center;
    padding:11px 0;
    border-bottom:1px solid var(--border);
    font-size:13.5px;
}
.info-row:last-child { border-bottom:none; }
.info-label { color:var(--text2); font-weight:500; }
.info-value { color:var(--text); font-weight:600; text-align:right; }
</style>
@endpush

@section('content')

<div class="mb-4">
    <a href="{{ route('admin.users.index') }}" class="btn btn-secondary btn-sm">
        <i class="bi bi-arrow-left"></i> Kullanıcılara Dön
    </a>
</div>

<div class="row g-4">

    {{-- ── Sol Kolon ── --}}
    <div class="col-lg-4">

        {{-- Profil Kartı --}}
        <div class="card mb-3">
            <div class="card-body text-center" style="padding:28px 20px">
                <div class="user-avatar lg mx-auto mb-3">
                    @if($user->avatar)
                        <img src="{{ \App\Support\MediaUrl::fromStoragePath($user->avatar) }}" alt="">
                    @else
                        {{ strtoupper(substr($user->name, 0, 1)) }}
                    @endif
                </div>
                <div style="font-size:17px;font-weight:800;color:var(--text);margin-bottom:4px">{{ $user->name }}</div>
                <div style="font-size:13px;color:var(--text2);margin-bottom:14px">{{ $user->email }}</div>
                <div class="d-flex justify-content-center gap-2 flex-wrap">
                    @if($user->role === 'admin')
                        <span class="badge bg-danger">Admin</span>
                    @elseif($user->role === 'moderator')
                        <span class="badge bg-info" style="color:#000">Moderatör</span>
                    @else
                        <span class="badge bg-secondary">Kullanıcı</span>
                    @endif
                    @if($user->membership === 'premium')
                        <span class="badge badge-premium"><i class="bi bi-star-fill" style="font-size:9px;margin-right:3px"></i>Premium</span>
                    @else
                        <span class="badge badge-free">Ücretsiz</span>
                    @endif
                    @if($user->is_banned)
                        <span class="badge bg-danger"><i class="bi bi-slash-circle-fill" style="font-size:9px;margin-right:3px"></i>Banlı</span>
                    @endif
                </div>
            </div>
        </div>

        {{-- Bilgiler --}}
        <div class="card mb-3">
            <div class="card-header">
                <i class="bi bi-info-circle" style="color:var(--accent-lt)"></i>
                <h6>Hesap Bilgileri</h6>
            </div>
            <div class="card-body">
                <div class="info-row">
                    <span class="info-label">Kayıt Tarihi</span>
                    <span class="info-value">{{ $user->created_at->format('d.m.Y') }}</span>
                </div>
                <div class="info-row">
                    <span class="info-label">Son Giriş</span>
                    <span class="info-value">{{ $user->last_login_at ? $user->last_login_at->format('d.m.Y H:i') : '—' }}</span>
                </div>
                @if($user->membership === 'premium')
                <div class="info-row">
                    <span class="info-label">Premium Bitiş</span>
                    <span class="info-value" style="color:var(--warning)">
                        {{ $user->premium_expires_at ? $user->premium_expires_at->format('d.m.Y') : 'Süresiz' }}
                    </span>
                </div>
                @endif
                @if($user->is_banned)
                <div class="info-row">
                    <span class="info-label">Ban Tarihi</span>
                    <span class="info-value" style="color:var(--danger)">{{ $user->banned_at?->format('d.m.Y') ?? '—' }}</span>
                </div>
                @if($user->ban_reason)
                <div class="info-row" style="flex-direction:column;align-items:flex-start;gap:4px">
                    <span class="info-label">Ban Sebebi</span>
                    <span style="font-size:13px;color:var(--text)">{{ $user->ban_reason }}</span>
                </div>
                @endif
                @endif
            </div>
        </div>

        {{-- İşlemler --}}
        <div class="card mb-3">
            <div class="card-header">
                <i class="bi bi-sliders" style="color:var(--info)"></i>
                <h6>İşlemler</h6>
            </div>
            <div class="card-body d-flex flex-column gap-2">
                <a href="{{ route('admin.users.edit', $user) }}" class="btn btn-outline-primary btn-sm">
                    <i class="bi bi-pencil"></i> Düzenle
                </a>

                @if($user->is_banned)
                    <form method="POST" action="{{ route('admin.users.unban', $user) }}">
                        @csrf
                        <button type="submit" class="btn btn-success btn-sm w-100">
                            <i class="bi bi-check-circle"></i> Banı Kaldır
                        </button>
                    </form>
                @else
                    <button class="btn btn-outline-warning btn-sm" data-bs-toggle="modal" data-bs-target="#banModal">
                        <i class="bi bi-slash-circle"></i> Banla
                    </button>
                @endif

                @if($user->id !== auth()->id())
                    <form method="POST" action="{{ route('admin.users.destroy', $user) }}"
                          onsubmit="return confirm('{{ addslashes($user->name) }} kullanıcısını kalıcı olarak silmek istediğinize emin misiniz?')">
                        @csrf @method('DELETE')
                        <button type="submit" class="btn btn-outline-danger btn-sm w-100">
                            <i class="bi bi-trash3"></i> Hesabı Sil
                        </button>
                    </form>
                @endif
            </div>
        </div>

        {{-- Premium Yönetimi --}}
        <div class="card">
            <div class="card-header">
                <i class="bi bi-star" style="color:var(--warning)"></i>
                <h6>Premium Yönetimi</h6>
            </div>
            <div class="card-body d-flex flex-column gap-3">
                @if($user->membership === 'premium')
                    <form method="POST" action="{{ route('admin.users.remove-premium', $user) }}">
                        @csrf
                        <button type="submit" class="btn btn-outline-warning btn-sm w-100">
                            <i class="bi bi-x-circle"></i> Premium'u Kaldır
                        </button>
                    </form>
                @endif
                <form method="POST" action="{{ route('admin.users.give-premium', $user) }}">
                    @csrf
                    <div class="mb-2">
                        <select name="plan_id" class="form-select" required>
                            <option value="">— Plan Seç —</option>
                            @foreach($plans as $plan)
                                <option value="{{ $plan->id }}">{{ $plan->name }} ({{ $plan->duration_days }} gün)</option>
                            @endforeach
                        </select>
                    </div>
                    <button type="submit" class="btn btn-primary btn-sm w-100">
                        <i class="bi bi-star-fill"></i> Premium Ver
                    </button>
                </form>
            </div>
        </div>

    </div>

    {{-- ── Sağ Kolon ── --}}
    <div class="col-lg-8">

        {{-- Abonelik Geçmişi --}}
        <div class="card mb-4">
            <div class="card-header">
                <i class="bi bi-credit-card" style="color:var(--success)"></i>
                <h6>Abonelik Geçmişi</h6>
            </div>
            <div class="card-body p-0">
                @forelse($user->subscriptions as $sub)
                    <div class="d-flex align-items-center justify-content-between px-4 py-3" style="border-bottom:1px solid var(--border)">
                        <div>
                            <div style="font-size:13.5px;font-weight:700;color:var(--text)">{{ $sub->plan->name ?? '—' }}</div>
                            <div style="font-size:12px;color:var(--text2)">
                                {{ $sub->starts_at?->format('d.m.Y') }} — {{ $sub->expires_at?->format('d.m.Y') }}
                            </div>
                        </div>
                        <span class="badge {{ $sub->status === 'active' ? 'bg-success' : ($sub->status === 'expired' ? 'bg-secondary' : 'bg-danger') }}">
                            {{ match($sub->status) { 'active'=>'Aktif', 'expired'=>'Sona Erdi', default=>'İptal' } }}
                        </span>
                    </div>
                @empty
                    <div class="empty-state py-5">
                        <i class="bi bi-credit-card"></i>
                        <p>Abonelik geçmişi yok.</p>
                    </div>
                @endforelse
            </div>
        </div>

        {{-- Son Yorumlar --}}
        <div class="card">
            <div class="card-header">
                <i class="bi bi-chat-dots" style="color:var(--info)"></i>
                <h6>Son Yorumlar</h6>
            </div>
            <div class="card-body p-0">
                @forelse($user->comments->take(10) as $comment)
                    <div class="px-4 py-3" style="border-bottom:1px solid var(--border)">
                        <div class="d-flex justify-content-between align-items-center gap-2 mb-1">
                            <span style="font-size:12px;color:var(--text2)">{{ $comment->created_at->format('d.m.Y H:i') }}</span>
                            <span class="badge {{ $comment->status === 'approved' ? 'bg-success' : ($comment->status === 'pending' ? 'bg-warning' : 'bg-secondary') }}"
                                  style="{{ $comment->status==='pending'?'color:#000':'' }}">
                                {{ match($comment->status) { 'approved'=>'Onaylı', 'pending'=>'Bekliyor', default=>'Reddedildi' } }}
                            </span>
                        </div>
                        <div style="font-size:13px;color:var(--text);line-height:1.5">{{ Str::limit($comment->content, 120) }}</div>
                    </div>
                @empty
                    <div class="empty-state py-5">
                        <i class="bi bi-chat-dots"></i>
                        <p>Henüz yorum yapılmamış.</p>
                    </div>
                @endforelse
            </div>
        </div>

    </div>
</div>

{{-- Ban Modal --}}
<div class="modal fade" id="banModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h6 class="modal-title" style="color:var(--warning)">
                    <i class="bi bi-slash-circle me-2"></i>Kullanıcıyı Banla
                </h6>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST" action="{{ route('admin.users.ban', $user) }}">
                @csrf
                <div class="modal-body">
                    <div class="p-3 rounded mb-3" style="background:var(--warning-dim);border:1px solid rgba(245,158,11,.3)">
                        <div style="font-size:13px;font-weight:700;color:var(--warning)">
                            <i class="bi bi-exclamation-triangle-fill me-2"></i>Dikkat
                        </div>
                        <div style="font-size:12.5px;color:var(--text);margin-top:4px">
                            <strong>{{ $user->name }}</strong> adlı kullanıcının siteye erişimi engellenecek.
                        </div>
                    </div>
                    <label class="form-label">Ban Sebebi <span style="color:var(--text2)">(opsiyonel)</span></label>
                    <textarea name="ban_reason" class="form-control" rows="3"
                              placeholder="Neden banlıyorsunuz? Kullanıcıya gösterilmez."></textarea>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">İptal</button>
                    <button type="submit" class="btn btn-warning"><i class="bi bi-slash-circle"></i> Banla</button>
                </div>
            </form>
        </div>
    </div>
</div>

@endsection
