 (ns api.core
   (:import [com.gallatinsystems.common Constants]
-           [org.akvo.flow.api.dao FolderDAO SurveyDAO]
+           [com.gallatinsystems.survey.dao.SurveyDAO]
            [com.gallatinsystems.user.dao UserDao]
            [com.google.appengine.tools.remoteapi RemoteApiInstaller RemoteApiOptions]
            [com.google.apphosting.utils.config AppEngineWebXmlReader]
            [java.io ByteArrayInputStream]
-           [org.apache.commons.codec.binary Base64]
-           [java.time.format DateTimeFormatter])
+           [java.time.format DateTimeFormatter]
+           [org.akvo.flow.api.dao FolderDAO SurveyDAO]
+           [org.apache.commons.codec.binary Base64])
   (:require [clj-http.client :as http]
             [clojure.java.io :as io]
             [clojure.string :as str]))

      file
      (fetch-p12 instance-id auth-token))))

#_(let [auth-token ""
        email ""
        ;;instance-map (get-instance-map auth-token)
        alias-map (get-alias-map instance-map)
        instance-id (get alias-map "uat1")
        host (str instance-id ".appspot.com")


      (->> user-surveys
           (map (fn [survey]
                  {:id (str (id survey))
                   :name (.getName survey)
                   :folder-id (str (.getParentId survey))
                   :created-at (created-at survey)
                   :modified-at (modified-at survey)}))
           (filter #(= (:folder-id %) folder-id))))))

(defn ->question [question]
  {:id (str (id question))
   :name (.getText question)
   :type (str (.getType question))
   :order (.getOrder question)
   :created-at (created-at question)
   :modified-at (modified-at question)})



(defn question-group-definition [question-group questions]
  (let [qs (sort-by :order (map ->question questions))]
    {:id (str (id question-group))
     :name (.getName question-group)
     :repeatable? (.getRepeatable question-group)
     :questions qs
     :created-at (created-at question-group)
     :modified-at (modified-at question-group)}))

(defn get-form-definition [email form-id]
  (let [form-dao (com.gallatinsystems.survey.dao.SurveyDAO.)
        ;; Includes question groups, but contrary to docstring does not contain questions
        form (.loadFullSurvey form-dao form-id)
        question-dao (com.gallatinsystems.survey.dao.QuestionDao.)
        questions (group-by #(.getQuestionGroupId %)
                            (.listQuestionsBySurvey question-dao form-id))]
    {:id (str (id form))
     :name (.getName form)
     :question-groups (mapv (fn [question-group]
                              (question-group-definition question-group
                                                         (get questions
                                                              (id question-group))))
                            (.values (.getQuestionGroupMap form)))
     :created-at (created-at form)
     :modified-at (modified-at form)}))

(defn get-survey-definition [email survey-id]
  (let [user-dao (UserDao.)
        user (.findUserByEmail user-dao email)
        survey-dao (com.gallatinsystems.survey.dao.SurveyGroupDAO.)
        survey (.getByKey survey-dao (Long/parseLong survey-id))
        form-dao (com.gallatinsystems.survey.dao.SurveyDAO.)
        forms (.filterByUserAuthorizationObjectId form-dao
                                                  (.listSurveysByGroup form-dao (Long/parseLong survey-id))
                                                  (id user))]
    {:id survey-id
     :name (.getName survey)
     :forms (mapv #(get-form-definition email (id %))
                  forms)
     :created-at (created-at survey)
     :modified-at (modified-at survey)}))

(defn contents-url [path]
  (format "https://api.github.com/repos/akvo/akvo-flow-server-config/contents%s" path))
