* * @throws \InvalidArgumentException */ public function handle(int $workspaceId, string $agentId): array { $nodeId = FleetNode::query() ->where('workspace_id', $workspaceId) ->where('agent_id', $agentId) ->value('id'); $query = CreditEntry::query() ->where('workspace_id', $workspaceId) ->where(function (Builder $builder) use ($agentId, $nodeId): void { $builder->where('agent_id', $agentId); if ($nodeId !== null) { $builder->orWhere(function (Builder $legacy) use ($nodeId): void { $legacy->whereNull('agent_id') ->where('fleet_node_id', $nodeId); }); } }); if ($nodeId === null && ! $query->exists()) { throw new \InvalidArgumentException('Fleet node not found'); } $balance = (int) (clone $query) ->latest('id') ->value('balance_after'); $totalEarned = (int) (clone $query)->where('amount', '>', 0)->sum('amount'); $totalSpent = (int) abs((int) (clone $query)->where('amount', '<', 0)->sum('amount')); $entries = (clone $query)->count(); return [ 'agent_id' => $agentId, 'balance' => $balance, 'total_earned' => $totalEarned, 'total_spent' => $totalSpent, 'entries' => $entries, ]; } }