*/ public function forOrganization(string $ownerRef, int $organizationId, ?int $branchId = null): array { $ticketQuery = Ticket::owned($ownerRef) ->where('organization_id', $organizationId) ->when($branchId, fn ($q) => $q->where('branch_id', $branchId)); $todayQuery = (clone $ticketQuery)->whereDate('issued_at', today()); $waiting = (clone $ticketQuery)->where('status', 'waiting')->count(); $serving = (clone $ticketQuery)->whereIn('status', ['called', 'serving'])->count(); $servedToday = (clone $todayQuery)->where('status', 'completed')->count(); $noShowsToday = (clone $todayQuery)->where('status', 'no_show')->count(); $waitDiff = DatabaseTime::diffSeconds('issued_at', 'called_at'); $avgWait = (clone $todayQuery) ->where('status', 'completed') ->whereNotNull('called_at') ->selectRaw("AVG({$waitDiff}) as avg_wait") ->value('avg_wait'); $activeBranches = Branch::owned($ownerRef) ->where('organization_id', $organizationId) ->where('is_active', true) ->when($branchId, fn ($q) => $q->where('id', $branchId)) ->count(); return [ 'waiting' => $waiting, 'serving' => $serving, 'served_today' => $servedToday, 'no_shows_today' => $noShowsToday, 'avg_wait_seconds' => (int) round((float) $avgWait), 'active_branches' => $activeBranches, ]; } }