   :handle-ok (pretty-json 
                (-> (otp-request-live get-params)
                ;; (-> (simple-otp-request-live)
-                   otp-response->itinerary
+                   otp-response->plan
                    plan->add-text2go
                    plan->add-route-url
                    plan->add-route-span

    (http-client/get test-otp-url {:as :json})))


(when false ;; for debugging
  (defn demo-itineraries
    []
    (-> test-data/otp-response-2
        otp-response->remove-trace
        otp-response->plan
        plan->merge-similar
        :itineraries))

  ;; reduce example 
  (reduce (fn [acc i] 
            (update-in acc 
                       [(even? i)] 
                       #((fnil conj []) % i))) 
          {} 
          [1 2 3 4])
  ;(map :walkDistance (demo-plan->merge-similar))
  ;(map count-legs (demo-plan->merge-similar))
  ;(map transit-routes (demo-itineraries))

  (for [a [[1] [2 3 4] [5 6 7 8 9]]] (let [b (count a)] b))

  (defn count-legs 
    [itin]
    (let [legs (:legs itin)]
      {:count-legs (count legs)}))


  (defn transit-routes
    "transit routes for each leg of itinerary itin, in order.
    non-transit legs are ignored."
    [itin]
    (let [legs (:legs itin)
          legs-with-routeid (remove #(nil? (:routeId %))
                                    legs)]
      (into [] (map :routeId legs-with-routeid))))

  (map (juxt :duration :minDuration :maxDuration :countWithThisRouteSequence)
       (summarize-collection (collect-by-route-sequence (demo-itineraries)))))


(defn collect-by-route-sequence [itins]
  (reduce (fn [acc itin]
            (update-in acc 
                       [(transit-routes itin)]
                       (fn [itins] 
                         ((fnil conj [])
                          itins itin))))


 (comment see java.util.Comparator docs ) 

(when false ;; for debugging
  (defn demo-itineraries
    []
    (-> test-data/otp-response-2
        otp-response->remove-trace
        otp-response->plan
        plan->merge-similar
        :itineraries))

  ;; reduce example 
  (reduce (fn [acc i] 
            (update-in acc 
                       [(even? i)] 
                       #((fnil conj []) % i))) 
          {} 
          [1 2 3 4])
  ;(map :walkDistance (demo-plan->merge-similar))
  ;(map count-legs (demo-plan->merge-similar))
  ;(map transit-routes (demo-itineraries))

  (for [a [[1] [2 3 4] [5 6 7 8 9]]] (let [b (count a)] b))

  (defn count-legs 
    [itin]
    (let [legs (:legs itin)]
      {:count-legs (count legs)}))



  (map (juxt :duration :minDuration :maxDuration :countWithThisRouteSequence)
       (summarize-collection (collect-by-route-sequence (demo-itineraries)))))



;; Fixme: verify agency-id along with stop code
(defn plan->add-text2go
  "add text2go codes into the stopCode field of an OTP Itinerary"
