photos() ->selectRaw('DATE_FORMAT(created_at, "%Y-%m-%d %H:00:00") as hour, count(*) as count') ->groupBy('hour') ->orderBy('hour') ->get(); return $stats->map(fn ($item) => [ 'timestamp' => $item->hour, 'count' => (int) $item->count, ])->toArray(); } /** * Get top contributors (users with most uploads). */ public function getTopContributors(Event $event, int $limit = 5): array { $stats = $event->photos() ->select('guest_name', DB::raw('count(*) as count'), DB::raw('sum(likes_count) as likes')) ->whereNotNull('guest_name') ->groupBy('guest_name') ->orderByDesc('count') ->limit($limit) ->get(); return $stats->map(fn ($item) => [ 'name' => $item->guest_name, 'count' => (int) $item->count, 'likes' => (int) $item->likes, ])->toArray(); } /** * Get task completion stats. */ public function getTaskStats(Event $event, int $limit = 5): array { $stats = $event->photos() ->whereNotNull('task_id') ->select('task_id', DB::raw('count(*) as count')) ->groupBy('task_id') ->with('task:id,name,name_translations') // Eager load task name ->orderByDesc('count') ->limit($limit) ->get(); return $stats->map(fn ($item) => [ 'task_id' => $item->task_id, 'task_name' => $item->task ? $item->task->getNameForLocale() : 'Unknown Task', // Assuming getNameForLocale exists or similar 'count' => (int) $item->count, ])->toArray(); } }