'object', 'properties' => [ 'customer_id' => [ 'type' => 'integer', 'description' => 'The customer ID.', ], ], 'required' => ['customer_id'], ]; } public function execute(array $arguments, int $companyId, int $userId): mixed { $customer = Customer::query() ->where('company_id', $companyId) ->where('id', (int) ($arguments['customer_id'] ?? 0)) ->with(['billingAddress', 'shippingAddress']) ->first(); if (! $customer) { return ['error' => 'customer_not_found']; } // Aggregate totals — done with lightweight queries rather than loading every invoice. $invoiceCount = Invoice::query() ->where('company_id', $companyId) ->where('customer_id', $customer->id) ->count(); $outstanding = (float) Invoice::query() ->where('company_id', $companyId) ->where('customer_id', $customer->id) ->whereIn('paid_status', ['UNPAID', 'PARTIALLY_PAID']) ->sum('due_amount'); return [ 'customer' => [ 'id' => $customer->id, 'name' => $customer->name, 'display_name' => $customer->display_name, 'email' => $customer->email, 'phone' => $customer->phone, 'contact_name' => $customer->contact_name, 'company_name' => $customer->company_name, 'website' => $customer->website, 'enable_portal' => (bool) $customer->enable_portal, 'billing_address' => $customer->billingAddress, 'shipping_address' => $customer->shippingAddress, 'totals' => [ 'invoice_count' => $invoiceCount, 'outstanding_amount' => $outstanding, ], ], ]; } }