guard_staff(); if ( $check ) { return $check; } try { $params = [ 'per_page' => min( 50, max( 1, (int) ( $per_page ?? 10 ) ) ), 'paged' => max( 1, (int) ( $paged ?? 1 ) ), ]; if ( ! empty( $search ) ) { $params['search'] = $search; } if ( null !== $category_id ) { $params['category_id'] = $category_id; } // Team members only ever see the services assigned to them. $scoped_team_member = $this->enforced_team_member_id(); if ( null !== $scoped_team_member ) { $params['team_member_id'] = $scoped_team_member; } $request = new \WP_REST_Request( 'GET', '/booktics/v1/services' ); $request->set_query_params( $params ); $controller = new \Booktics\Service\Controllers\Service_Controller(); $result = $this->extract_response( $controller->get_items( $request ), 'Failed to retrieve services.' ); if ( isset( $result['error'] ) ) { return $result; } // A genuinely empty result already comes back as a 200 with items:[], // so it flows through the shaping path below — no special-casing needed. return $this->shape_paginated( $result, fn( $service ) => $this->shape_service( $service ) ); } catch ( \Throwable $e ) { Log::error( 'Booktics_List_Services_Tool error', [ 'error' => $e->getMessage() ] ); return [ 'error' => 'Failed to retrieve services: ' . $e->getMessage() ]; } } }