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

   [auto-ap.yodlee.core2 :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]]

   [clojure.tools.logging :as log]
   [auto-ap.datomic.clients :as d-clients]))

(defroutes routes
  (wrap-routes
   (context "/yodlee2" []
            (GET "/fastlink" {:keys [query-params identity] :as request}
                 (assert-admin identity)
                 (let [token (yodlee/get-access-token (get query-params "client"))]
                   {:status 200 
                    :headers {"Content-Type" "application/edn"}
                    :body (pr-str {:token token
                                   :url (:yodlee2-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 "/reauthenticate/:id" {:keys [query-params identity] {:keys [id]} :route-params
                                         data  :edn-params
                                         :as request}
                  (assert-admin identity)
                  (try 
                    (let [[session token] (yodlee/get-access-token)]
                      {:status 200 
                       :headers {"Content-Type" "application/edn"}
                       :body (pr-str (yodlee/reauthenticate (Long/parseLong id) data)) })
                    (catch Exception e
                      (log/error e)
                      {:status 500
                       :headers {"Content-Type" "application/edn"}
                       :body (pr-str {:message (.getMessage e)
                                      :error (.toString e)})})))
            (POST "/provider-accounts/refresh/" {:keys [query-params identity edn-params]
                                                    {:keys [id]} :route-params
                                                    :as request}
                  (assert-admin identity)
                  (log/info "refreshing " edn-params)
                  (try 
                    (yodlee/refresh-provider-account (-> (:client-id edn-params)
                                                         Long/parseLong
                                                         d-clients/get-by-id
                                                         :client/code)
                                                     (:provider-account-id edn-params))
                    {:status 200 
                     :headers {"Content-Type" "application/edn"}
                     :body "{}" }
                    (catch Exception e
                      (log/error e)
                      {:status 400
                       :headers {"Content-Type" "application/edn"}
                       :body (pr-str {:message (.getMessage e)
                                      :error (.toString e)})})))
            (POST "/provider-accounts/delete/" {:keys [query-params edn-params identity] {:keys [id]} :route-params :as request}
                  (assert-admin identity)
                  (try 
                    (yodlee/delete-provider-account (-> (:client-id edn-params)
                                                         Long/parseLong
                                                         d-clients/get-by-id
                                                         :client/code)
                                                     (:provider-account-id edn-params))
                    {:status 200 
                     :headers {"Content-Type" "application/edn"}
                     :body (pr-str {}) }
                    (catch Exception e
                      (log/error e)
                      {:status 400
                       :headers {"Content-Type" "application/edn"}
                       :body (pr-str {:message (.getMessage e)
                                      :error (.toString e)})})))
            (POST "/provider-accounts/:id" {:keys [query-params identity] {:keys [id]} :route-params :as request}
                 (assert-admin identity)
                 (try 
                   (let [[session token] (yodlee/get-access-token)]
                     {:status 200 
                      :headers {"Content-Type" "application/edn"}
                      :body (pr-str (yodlee/update-yodlee (Long/parseLong id))) })
                   (catch Exception e
                     {:status 400
                      :headers {"Content-Type" "application/edn"}
                      :body (pr-str e)}))))
   wrap-secure))
