guard( 'manage_woocommerce', 'manage_options', 'wpcafe_manage_reservations', 'wpcafe_view_all_reservations', 'wpcafe_view_own_reservations' ); 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 ( ! empty( $status ) ) { $params['status'] = $status; } if ( null !== $branch ) { $params['branch'] = $branch; } $request = new \WP_REST_Request( 'GET', '/wpcafe/v2/reservations' ); $request->set_query_params( $params ); $controller = new \WpCafe\Reservation\Controllers\Reservation_Controller(); $result = $this->extract_response( $controller->get_items( $request ), 'Failed to retrieve reservations.' ); if ( isset( $result['error'] ) ) { return $result; } $shaped = $this->shape_paginated( $result, fn( $reservation ) => [ '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'] ?? '', 'total_price' => $reservation['total_price'] ?? '', 'currency' => $reservation['currency'] ?? '', 'food_items_count' => is_array( $reservation['food_items'] ?? null ) ? count( $reservation['food_items'] ) : 0, ] ); return $shaped; } catch ( \Throwable $e ) { Log::error( 'Wpcafe_List_Reservations_Tool error', [ 'error' => $e->getMessage() ] ); return [ 'error' => 'Failed to retrieve reservations: ' . $e->getMessage() ]; } } }