# # app/controllers/api/v1/schedules_controller.rb
# class Api::V1::SchedulesController < ApplicationController
#   def index
#     departure_station = params[:departure]
#     destination_station = params[:destination]

#     if departure_station.present? && destination_station.present?
#       schedules = Schedule.joins(stations: { schedule: { route: :bus_schedule } })
#                           .where(stations: { name: departure_station })
#                           .where(id: Schedule.joins(stations: { schedule: { route: :bus_schedule } })
#                                               .where(stations: { name: destination_station })
#                                               .pluck(:id))
#                           .group(:trip)
#                           .order(:trip)

#       render json: schedules.map { |schedule|
#         {
#           trip: schedule.trip,
#           stations: schedule.stations.map { |station|
#             {
#               station: station.name,
#               arrivalTime: station.arrival_time,
#               departureTime: station.departure_time
#             }
#           }
#         }
#       }, status: :ok
#     else
#       render json: { error: 'Departure and destination stations are required.' }, status: :unprocessable_entity
#     end
#   end
# end
class Api::V1::SchedulesController < ApplicationController
  def index
    departure_station = params[:departure]
    destination_station = params[:destination]

    if departure_station.present? && destination_station.present?
      schedules = Schedule.joins(stations: { schedule: { route: :bus_schedule } })
                          .where(stations: { name: departure_station })
                          .where(id: Schedule.joins(stations: { schedule: { route: :bus_schedule } })
                                            .where(stations: { name: destination_station })
                                            .pluck(:id))
                          .group(:trip)
                          .order(:trip)

      render json: schedules.map { |schedule|
        {
          trip: schedule.trip,
          stations: schedule.stations.map { |station|
            {
              station: station.name,
              arrivalTime: station.arrival_time,
              departureTime: station.departure_time
            }
          }.reject { |s| s[:station] == departure_station || s[:station] == destination_station || station_sequence(s[:station], schedule.route_id) < station_sequence(departure_station, schedule.route_id) }
        }
      }, status: :ok
    else
      render json: { error: 'Departure and destination stations are required.' }, status: :unprocessable_entity
    end
  end

  private

  def station_sequence(station_name, route_id)
    Station.where(name: station_name, route_id: route_id).pluck(:sequence).first
  end
end
this returns below when i want manjali to athani
[{"trip":1,"stations":[{"station":"ANGAMALY PRIVATE BUS STATION","arrivalTime":"06:35 am","departureTime":"06:35 am"},{"station":"ATHANI","arrivalTime":"06:53 am","departureTime":"06:53 am"},{"station":"MANJALY","arrivalTime":"07:15 am","departureTime":"07:15 am"},{"station":"NORTH PARAVUR","arrivalTime":"07:25 am","departureTime":"07:25 am"}]},{"trip":2,"stations":[{"station":"NORTH PARAVUR","arrivalTime":"07:30 am","departureTime":"07:30 am"},{"station":"MANJALY","arrivalTime":"07:40 am","departureTime":"07:40 am"},{"station":"ATHANI","arrivalTime":"08:02 am","departureTime":"08:02 am"},{"station":"ANGAMALY PRIVATE BUS STATION","arrivalTime":"08:20 am","departureTime":"08:20 am"}]},{"trip":3,"stations":[{"station":"ANGAMALY PRIVATE BUS STATION","arrivalTime":"08:38 am","departureTime":"08:38 am"},{"station":"ATHANI","arrivalTime":"08:56 am","departureTime":"08:56 am"},{"station":"MANJALY","arrivalTime":"09:18 am","departureTime":"09:18 am"},{"station":"NORTH PARAVUR","arrivalTime":"09:28 am","departureTime":"09:28 am"}]}]

but i only want 

[{"trip":2,"stations":[{"station":"NORTH PARAVUR","arrivalTime":"07:30 am","departureTime":"07:30 am"},{"station":"MANJALY","arrivalTime":"07:40 am","departureTime":"07:40 am"},{"station":"ATHANI","arrivalTime":"08:02 am","departureTime":"08:02 am"},{"station":"ANGAMALY PRIVATE BUS STATION","arrivalTime":"08:20 am","departureTime":"08:20 am"}]}]

because other two are athani to manjali