guard( 'manage_woocommerce', 'manage_options', 'wpcafe_manage_reservations', 'wpcafe_view_all_reservations', 'wpcafe_view_own_reservations' ); if ( $check ) { return $check; } if ( $reservation_id <= 0 ) { return [ 'error' => 'reservation_id must be a positive integer.' ]; } try { $request = new \WP_REST_Request( 'GET', '/wpcafe/v2/reservations/' . $reservation_id ); $request->set_url_params( [ 'id' => $reservation_id ] ); $controller = new \WpCafe\Reservation\Controllers\Reservation_Controller(); $result = $this->extract_response( $controller->get_item( $request ), 'Reservation not found.' ); if ( isset( $result['error'] ) ) { return $result; } return $this->shape_reservation( $result ); } catch ( \Throwable $e ) { Log::error( 'Wpcafe_Get_Reservation_Tool error', [ 'error' => $e->getMessage() ] ); return [ 'error' => 'Failed to retrieve reservation: ' . $e->getMessage() ]; } } /** * Curate a Reservation_Resource record down to the fields the LLM needs, * reducing the food-order line items to a count. * * @param array $reservation Raw reservation record. * @return array */ private function shape_reservation( array $reservation ): array { return [ 'id' => $reservation['id'] ?? null, 'invoice' => $reservation['invoice'] ?? '', 'name' => $reservation['name'] ?? '', 'email' => $reservation['email'] ?? '', 'phone' => $reservation['phone'] ?? '', 'date' => $reservation['date'] ?? '', 'start_time' => $reservation['start_time'] ?? '', 'end_time' => $reservation['end_time'] ?? '', 'total_guest' => $reservation['total_guest'] ?? '', 'status' => $reservation['status'] ?? '', 'branch_name' => $reservation['branch_name'] ?? '', 'table_name' => $reservation['table_name'] ?? '', 'total_price' => $reservation['total_price'] ?? '', 'currency' => $reservation['currency'] ?? '', 'payment_method' => $reservation['payment_method'] ?? '', 'notes' => $reservation['notes'] ?? '', 'food_items_count' => is_array( $reservation['food_items'] ?? null ) ? count( $reservation['food_items'] ) : 0, ]; } }