(ns auto-ap.routes.yodlee
  (:require 
            [auto-ap.graphql :as graphql]
            [clj-http.client :as http]

            [auto-ap.yodlee.core :as yodlee]
            [auto-ap.graphql.utils :refer [->graphql assert-admin]]
            [auto-ap.routes.utils :refer [wrap-secure]]
            [clj-time.coerce :refer [to-date]]
            [ring.middleware.json :refer [wrap-json-response]]
            [compojure.core :refer [GET POST context defroutes wrap-routes]]
            [clojure.string :as str]
            [config.core :refer [env]]
            ))

(defroutes routes
  (wrap-routes
   (context "/yodlee" []
            (GET "/fastlink" {:keys [query-params identity] :as request}
                 (assert-admin identity)
                 (let [[session token] (yodlee/get-access-token)]
                   
                   
                   {:status 200 
                    :headers {"Content-Type" "application/edn"}
                    :body (pr-str {:session session
                                   :token token
                                   :app (:yodlee-app env)
                                   
                                   :url (:yodlee-fastlink env) 

                                   }) }))
            (GET "/accounts" {:keys [query-params identity] :as request}
                 (assert-admin identity)
                 (let [[session token] (yodlee/get-access-token)]
                   {:status 200 
                    :headers {"Content-Type" "application/edn"}
                    :body (pr-str (yodlee/get-accounts)) }))
            (POST "/accounts/:id" {:keys [query-params identity] {:keys [id]} :route-params :as request}
                 (assert-admin identity)
                 (let [[session token] (yodlee/get-access-token)]
                   {:status 200 
                    :headers {"Content-Type" "application/edn"}
                    :body (pr-str (yodlee/update-yodlee (Long/parseLong id))) })))
   wrap-secure))
