id(); $user = auth()->user(); $stats = [ 'projects' => $this->promptProjectService->index()['total'] ?? 0, 'generations' => $this->promptGenerationService->index()->total() ?? 0, 'providers' => AiProvider::count(), 'tokens' => $user && $user->role === 'superadmin' ? \App\Models\PromptGeneration::sum('tokens_used') ?? 0 : \App\Models\PromptGeneration::where('user_id', $userId)->sum('tokens_used') ?? 0, ]; $recentGenerationsQuery = \App\Models\PromptGeneration::with(['project', 'model']) ->latest() ->limit(5); if (!$user || $user->role !== 'superadmin') { $recentGenerationsQuery->where('user_id', $userId); } $recentGenerations = $recentGenerationsQuery->get(); return view('dashboard', compact('stats', 'recentGenerations')); } }