attributes->get('supervisor') ?? $request->user(); $today = Carbon::today(); $statsQuery = EnsureSupervisorScope::dailyStatsQuery($supervisor) ->where('stat_date', $today); $todayStats = $statsQuery->get(); $machinesQuery = EnsureSupervisorScope::machinesQuery($supervisor); $offlineThreshold = now()->subMinutes((int) config('laverie.machine.offline_threshold_minutes', 10)); return $this->success([ 'summary' => [ 'machines_total' => (clone $machinesQuery)->count(), 'machines_available' => (clone $machinesQuery)->where('status', 'available')->count(), 'machines_running' => (clone $machinesQuery)->where('status', 'running')->count(), 'machines_offline' => (clone $machinesQuery)->where(function ($q) use ($offlineThreshold) { $q->where('status', 'offline') ->orWhere(fn ($q) => $q->where('last_heartbeat_at', '<', $offlineThreshold)); })->count(), 'bookings_today' => EnsureSupervisorScope::bookingsQuery($supervisor) ->whereDate('slot_start', $today) ->count(), 'washes_today' => EnsureSupervisorScope::washesQuery($supervisor) ->whereDate('started_at', $today) ->count(), 'revenue_today' => (float) $todayStats->sum('total_revenue'), 'top_up_today' => (float) $todayStats->sum('top_up_amount'), ], 'establishments' => $todayStats->map(fn ($stat) => [ 'establishment_id' => $stat->establishment_id, 'establishment_name' => $stat->establishment?->name, 'total_washes' => $stat->total_washes, 'total_revenue' => (float) $stat->total_revenue, 'bookings_count' => $stat->bookings_count, 'no_show_count' => $stat->no_show_count, 'occupancy_rate' => (float) $stat->occupancy_rate, ]), ]); } }