-- name: GetStationsByName :many
SELECT s.id as "id",
       s.name as "name",
       sl.latitude as "latitude",
       sl.longitude as "longitude",
       bus as "bus",
       taxi as "taxi",
       tram as "tram",
       ferry as "ferry",
       subway as "subway",
       national as "national",
       regional as "regional",
       suburban as "suburban",
       regionalexp as "regionalexp",
       nationalexp as "nationalexp"
FROM stops s
    JOIN public.stat_locations sl
        on sl.id = s.location_id
    JOIN public.stat_services ss
        on ss.id = s.service_id
    JOIN public.station_rank sr
            on sr.stop = s.id
WHERE s.id
          in (SELECT id
              FROM stops s2
              WHERE s2.name ILIKE ALL (@search_params::text[])
              )
    AND CASE WHEN sqlc.narg(long_distance)::bool THEN (ss.national = sqlc.narg(long_distance)::bool OR ss.nationalexp = sqlc.narg(long_distance)::bool) ELSE TRUE END
ORDER BY sr.count DESC;

-- name: GetStationById :one
SELECT s.id as "id",
       s.name as "name",
       sl.latitude as "latitude",
       sl.longitude as "longitude",
       bus,
       taxi,
       tram,
       ferry,
       subway,
       national,
       regional,
       suburban,
       regionalexp,
       nationalexp
FROM stops s
         JOIN public.stat_locations sl
              on sl.id = s.location_id
         JOIN public.stat_services ss
              on ss.id = s.service_id
WHERE s.id = @stop_id;

-- name: GetDeparturesById :many
SELECT DISTINCT (t.id) as "id",
                t.line_name as "lineName",
                t.line_productname as "lineProductName",
                so.departuredelay as "departureDelay",
                jsonb_build_object('id', sd.id, 'name', sd.name) as "direction",
                ARRAY((SELECT jsonb_build_object('stop', row('id', sd.id, 'name', sd.name),
                                                   'arrival', so2.arrival,
                                                   'plannedArrival', so2.plannedarrival,
                                                   'arrivalDelay', so2.arrivaldelay,
                                                   'arrivalPlatform', so2.arrivalplatform,
                                                   'plannedArrivalPlatform', so2.plannedarrivalplatform,
                                                   'cancelled', so2.cancelled)
                        FROM stopovers so2
                            JOIN public.stops s2
                                ON so2.stop = s2.id
                        WHERE so2.id = t.id
                          AND so2.plannedarrival > so.planneddeparture
                          AND (so2.stop, so2.realtimedataupdatedat) IN (SELECT DISTINCT (stop), max(realtimedataupdatedat)
                                                                 FROM stopovers so3
                                                                 WHERE (so3.id = t.id
                                                                        AND so3.realtimedataupdatedat <= @rt_updated_at)
                                                                 GROUP BY so3.stop)))
                    AS "stopovers",
                so.departure as "departure",
                so.planneddeparture as "plannedDeparture",
                so.departureplatform as "departurePlatform",
                so.planneddepartureplatform as "plannedDeparturePlatform",
                so.cancelled as "cancelled",
                so.realtimedataupdatedat as "realtimeDataUpdatedAt"
FROM stopovers so
         JOIN trips t ON so.id = t.id
         JOIN public.trip_meta tm on t.id = tm.id
         JOIN public.stops sd on t.destination = sd.id
WHERE so.stop = @stop_id
  AND (so.id, so.realtimedataupdatedat) IN (SELECT DISTINCT (id), max(realtimedataupdatedat)
                                            FROM stopovers so3
                                            WHERE so3.stop = @stop_id
                                              AND (so3.departure > @departure_from)
                                              AND (so3.departure < @departure_to)
                                              AND (so3.realtimedataupdatedat <= @rt_updated_at)
                                            GROUP BY so3.id)
ORDER BY so.departure ASC;
