                              [table :refer [Table]])
             [metabase.util :refer [assoc*]]))
 
-(defn annotate-column [table column-id]
-  (let [{:keys [name base_type] :as column} (sel :one Field :id column-id)
+(defn annotate-column [table column]
+  (let [{:keys [name base_type]} column
         qualified-name (format "\"%s\".\"%s\"" (:name table) name)
         alias (match base_type
                 "DateTimeField" (format "%s_date" name)

             :id nil
             :description nil}))

(defn get-columns [{:keys [table breakout aggregation] :as query}]
  (->> (concat breakout aggregation)
       (filter identity)
       (map (fn [column]
              (case (integer? column)
                true (annotate-column table column)
                false (annotate-special-column column))))))

(declare build-query
         generate-sql)

(defn process [{:keys [source_table] :as query}]
  (assoc* query
          :table (sel :one Table :id source_table)
          :database (:db (-> (:source_table <>)
                             (hydrate :db)))
          :columns ( get-columns <>)
          :columns-by-name (->> (:columns <>)
                                (map (fn [{:keys [keyword] :as col}]
                                       {keyword col}))

(defn apply-clause [query [clause-name clause-value]]
  (case clause-name
    :filter nil
    :breakout (apply-breakout query clause-value)
    :limit (when clause-value
             {:limit clause-value})
    :aggregation nil))
