@extends('admin.layouts.app')
@section('title', 'Kullanıcı Aktivitesi — ' . $user->name)

@section('content')
<div class="d-flex align-items-center gap-3 mb-4">
  <a href="{{ route('admin.analytics.users', ['tab'=>'activity']) }}" class="btn btn-sm btn-outline-secondary"><i class="bi bi-arrow-left"></i></a>
  <div class="d-flex align-items-center gap-3">
    @if($user->avatar)
      <img src="{{ $user->avatar }}" class="rounded-circle" width="48" height="48" style="object-fit:cover">
    @else
      <div class="rounded-circle bg-info d-flex align-items-center justify-content-center text-dark fw-bold" style="width:48px;height:48px;font-size:18px">{{ strtoupper(substr($user->name,0,1)) }}</div>
    @endif
    <div>
      <h4 class="mb-0">{{ $user->name }} <span class="badge {{ $user->role==='admin'?'bg-danger':($user->role==='moderator'?'bg-warning text-dark':'bg-secondary') }}">{{ $user->role }}</span></h4>
      <small class="text-muted">{{ $user->email }} · Üyelik: {{ $user->created_at->format('d.m.Y') }}</small>
    </div>
  </div>
  <div class="ms-auto d-flex gap-2">
    <form class="d-flex gap-2">
      <input type="hidden" name="period" value="{{ $period }}">
      <select name="period" class="form-select form-select-sm" onchange="this.form.submit()">
        @foreach([7=>'Son 7 Gün',14=>'Son 14 Gün',30=>'Son 30 Gün',90=>'Son 90 Gün'] as $d=>$l)
          <option value="{{ $d }}" {{ $period==$d?'selected':'' }}>{{ $l }}</option>
        @endforeach
      </select>
    </form>
    <a href="{{ route('admin.users.show', $user) }}" class="btn btn-sm btn-outline-info">
      <i class="bi bi-person me-1"></i>Profili Gör
    </a>
  </div>
</div>

<div class="row g-4">
  {{-- Aksiyon Özeti --}}
  <div class="col-md-4">
    <div class="card">
      <div class="card-header"><strong>Aksiyon Özeti</strong></div>
      <div class="card-body">
        @forelse($actBreakdown as $ab)
        @php $labels = \App\Models\UserActivityLog::$actionLabels; @endphp
        <div class="d-flex justify-content-between mb-2">
          <span style="font-size:12px">{{ $labels[$ab->action] ?? $ab->action }}</span>
          <span class="badge bg-info text-dark">{{ $ab->cnt }}</span>
        </div>
        @empty
        <p class="text-muted text-center small">Bu dönemde aktivite yok</p>
        @endforelse
      </div>
    </div>

    {{-- Son izleme --}}
    <div class="card mt-3">
      <div class="card-header"><strong><i class="bi bi-play-circle me-1"></i>Son İzlemeler</strong></div>
      <div class="list-group list-group-flush">
        @forelse($watchEvents as $w)
        <div class="list-group-item py-2 px-3">
          <div class="fw-semibold" style="font-size:12px">{{ $w->anime_title }}</div>
          <div class="d-flex justify-content-between">
            <small class="text-muted">Bölüm {{ $w->episode_number }}</small>
            <small class="text-{{ $w->percent_complete >= 80 ? 'success' : 'warning' }}">%{{ $w->percent_complete }}</small>
          </div>
          <div style="height:3px;background:#333;border-radius:2px;margin-top:4px;overflow:hidden">
            <div style="width:{{ $w->percent_complete }}%;height:100%;background:var(--bs-info)"></div>
          </div>
          <small class="text-muted" style="font-size:10px">{{ \Carbon\Carbon::parse($w->created_at)->diffForHumans() }}</small>
        </div>
        @empty
        <div class="list-group-item text-muted small text-center py-3">İzleme verisi yok</div>
        @endforelse
      </div>
    </div>
  </div>

  {{-- Aktivite Log Tablosu --}}
  <div class="col-md-8">
    <div class="card">
      <div class="card-header"><strong>Detaylı Aktivite Günlüğü</strong></div>
      <div class="table-responsive">
        <table class="table table-sm table-hover mb-0">
          <thead class="table-dark">
            <tr><th>Aksiyon</th><th>Konu</th><th>IP</th><th>Ülke</th><th>Cihaz</th><th>Zaman</th></tr>
          </thead>
          <tbody>
            @forelse($logs as $log)
            @php $labels = \App\Models\UserActivityLog::$actionLabels; @endphp
            <tr>
              <td><span class="badge bg-info text-dark" style="font-size:10px">{{ $labels[$log->action] ?? $log->action }}</span></td>
              <td><small class="text-muted">{{ $log->subject_type ? class_basename($log->subject_type).'#'.$log->subject_id : '—' }}</small></td>
              <td><code style="font-size:10px">{{ $log->ip }}</code></td>
              <td><small class="text-muted">{{ $log->country ?: '—' }}</small></td>
              <td><small class="text-muted text-capitalize">{{ $log->device ?: '—' }}</small></td>
              <td><small class="text-muted">{{ \Carbon\Carbon::parse($log->created_at)->format('d.m.Y H:i') }}</small></td>
            </tr>
            @empty
            <tr><td colspan="6" class="text-center text-muted py-4">Aktivite bulunamadı</td></tr>
            @endforelse
          </tbody>
        </table>
      </div>
      <div class="card-footer">{{ $logs->links() }}</div>
    </div>

    {{-- Son Sayfa Görüntülemeleri --}}
    <div class="card mt-3">
      <div class="card-header"><strong><i class="bi bi-eye me-1"></i>Son Sayfa Görüntülemeleri</strong></div>
      <div class="table-responsive">
        <table class="table table-sm mb-0">
          <thead class="table-dark"><tr><th>URL</th><th>Sayfa Tipi</th><th>Süre</th><th>Zaman</th></tr></thead>
          <tbody>
            @forelse($pageviews as $pv)
            <tr>
              <td><small class="text-muted" style="max-width:200px;display:block;overflow:hidden;text-overflow:ellipsis;white-space:nowrap" title="{{ $pv->url }}">{{ $pv->url }}</small></td>
              <td><span class="badge bg-secondary" style="font-size:9px">{{ $pv->page_type }}</span></td>
              <td><small class="text-muted">{{ isset($pv->time_on_page) && $pv->time_on_page > 0 ? $pv->time_on_page.'s' : '—' }}</small></td>
              <td><small class="text-muted">{{ \Carbon\Carbon::parse($pv->created_at)->format('d.m H:i') }}</small></td>
            </tr>
            @empty
            <tr><td colspan="4" class="text-center text-muted py-3">Sayfa görüntüleme yok</td></tr>
            @endforelse
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>
@endsection
