also get intermediate stops between departure and destinaton

class Api::V1::SchedulesController < ApplicationController
  def index
    departure_station = params[:departure]
    destination_station = params[:destination]

    if departure_station.present? && destination_station.present?
      # Start by fetching schedules that include both departure and destination stations
      schedules = Schedule.joins(:stations, route: :bus_schedule)
                          .where(stations: { name: [departure_station, destination_station] })
                          .distinct
                          .includes(:stations, route: :bus_schedule)

      # Iterate through schedules to filter valid ones and prepare the response data
      vehicle_trips = schedules.each_with_object({}) do |schedule, acc|
        # Extract station names to check the order without triggering extra queries
        station_names = schedule.stations.map(&:name)

        # Ensure that the destination comes after the departure
        if station_names.include?(departure_station) &&
           station_names.include?(destination_station) &&
           station_names.index(departure_station) < station_names.index(destination_station)

          vehicle_number = schedule.route.bus_schedule.vehicle_number
          trip_data = {
            trip: schedule.trip,
            stations: schedule.stations.map do |station|
              {
                station: station.name,
                arrivalTime: station.arrival_time,
                departureTime: station.departure_time
              }
            end
          }

          # Accumulate trips for each vehicle_number
          acc[vehicle_number] ||= []
          acc[vehicle_number] << trip_data
        end
      end

      # Render the final JSON response
      render json: vehicle_trips.map { |vehicle_number, trips|
        {
          vehicle_number: vehicle_number,
          trips: trips
        }
      }, status: :ok
    else
      render json: { error: 'Departure and destination stations are required.' }, status: :unprocessable_entity
    end
  end
end


here is the reference code that fetch the complte details

# class Api::V1::SchedulesController < ApplicationController
#   def index
#     departure_station = params[:departure]
#     destination_station = params[:destination]

#     if departure_station.present? && destination_station.present?
#       valid_schedules = Schedule.joins(route: :bus_schedule)
#                                 .joins(:stations)
#                                 .where(stations: { name: [departure_station, destination_station] })
#                                 .distinct
#                                 .group_by { |schedule| schedule.route.bus_schedule.vehicle_number }

#       vehicle_trips = valid_schedules.each_with_object({}) do |(vehicle_number, schedules), acc|
#         trips = schedules.select do |schedule|
#           dep_index = schedule.stations.index { |s| s.name == departure_station }
#           dest_index = schedule.stations.index { |s| s.name == destination_station }
#           dep_index.present? && dest_index.present? && dep_index < dest_index
#         end.map do |schedule|
#           {
#             trip: schedule.trip,
#             stations: schedule.stations.map { |station|
#               { 
#                 station: station.name,
#                 arrivalTime: station.arrival_time,
#                 departureTime: station.departure_time
#               }
#             }
#           }
#         end

#         # Only include vehicles that have at least one valid trip.
#         acc[vehicle_number] = trips if trips.any?
#       end

#       render json: vehicle_trips.map { |vehicle_number, trips|
#         {
#           vehicle_number: vehicle_number,
#           trips: trips
#         }
#       }, status: :ok
#     else
#       render json: { error: 'Departure and destination stations are required.' }, status: :unprocessable_entity
#     end
#   end
# end