(require "libs/conimo/src/server.coni" :as conimo)
(require "libs/conimo/src/html.coni" :as html)
(require "libs/store/src/patom.coni" :as patom)
(require "libs/os/src/io.coni" :as io)

(io/mkdir-p "data")

;; ── Database (Persistent EDN Atom) ──────────────────────────────────
(def db (patom/patom "data/store.edn"
          {:version 1
           :next-id 2
           :items [{:id 1 :title "Welcome to Conimo!" :done false :priority "high"}]}
          {:watch true}))

;; Reactive watcher: broadcast to all WS clients whenever db changes
(add-watch db :ws-broadcast
  (fn [key ref old-state new-state]
    (when (not (= old-state new-state))
      (conimo/broadcast! (pr-str {:type :state-update :items (:items new-state)})))))

(println "[DB] Loaded" (count (:items (deref db))) "items from store")

;; ── Route Handlers ──────────────────────────────────────────────────

(defn handle-get-items [req]
  {:status 200
   :headers {"Content-Type" "application/edn"}
   :body (pr-str {:items (:items (deref db))})})

(defn handle-create-item [req]
  (let [body (:edn-body req)
        title (:title body)]
    (if (nil? title)
      {:status 400
       :headers {"Content-Type" "application/edn"}
       :body (pr-str {:error "Missing :title"})}
      (do
        (swap! db (fn [state]
          (let [new-id (:next-id state)
                new-item {:id new-id :title title :done false :priority (or (:priority body) "medium")}]
            (-> state
                (assoc :next-id (inc new-id))
                (assoc :items (conj (:items state) new-item))))))
        {:status 201
         :headers {"Content-Type" "application/edn"}
         :body (pr-str {:ok true})}))))

(defn handle-update-item [req]
  (let [body (:edn-body req)
        id (:id body)]
    (if (nil? id)
      {:status 400
       :headers {"Content-Type" "application/edn"}
       :body (pr-str {:error "Missing :id"})}
      (do
        (swap! db (fn [state]
          (let [items (:items state)
                updated-items (loop [i 0 acc []]
                                (if (< i (count items))
                                  (let [item (items i)]
                                    (if (= (:id item) id)
                                      (let [new-title (if (contains? body :title) (:title body) (:title item))
                                            new-done (if (contains? body :done) (:done body) (:done item))]
                                        (recur (inc i) (conj acc (-> item (assoc :title new-title) (assoc :done new-done)))))
                                      (recur (inc i) (conj acc item))))
                                  acc))]
            (assoc state :items updated-items))))
        {:status 200
         :headers {"Content-Type" "application/edn"}
         :body (pr-str {:ok true})}))))

(defn handle-delete-item [req]
  (let [body (:edn-body req)
        id (:id body)]
    (if (nil? id)
      {:status 400
       :headers {"Content-Type" "application/edn"}
       :body (pr-str {:error "Missing :id"})}
      (do
        (swap! db (fn [state]
          (let [items (:items state)
                filtered-items (loop [i 0 acc []]
                                 (if (< i (count items))
                                   (let [item (items i)]
                                     (if (= (:id item) id)
                                       (recur (inc i) acc)
                                       (recur (inc i) (conj acc item))))
                                   acc))]
            (assoc state :items filtered-items))))
        {:status 200
         :headers {"Content-Type" "application/edn"}
         :body (pr-str {:ok true})}))))

;; ── API Router (data-driven routes + middleware) ────────────────────
(def api-handler
  (-> (conimo/router
        [{:method "GET"  :path "/api/items"  :handler handle-get-items}
         {:method "POST" :path "/api/items"  :handler handle-create-item}
         {:method "POST" :path "/api/items/update" :handler handle-update-item}
         {:method "POST" :path "/api/items/delete" :handler handle-delete-item}])
      (conimo/wrap-edn-body)))

;; ── SSR Template ────────────────────────────────────────────────────
(defn app-page [req]
  [:html {:lang "en"}
    [:head
      [:title "Conimo Realtime App"]
      [:meta {:charset "utf-8"}]
      [:meta {:name "viewport" :content "width=device-width, initial-scale=1.0"}]
      [:link {:rel "preconnect" :href "https://fonts.googleapis.com"}]
      [:link {:rel "preconnect" :href "https://fonts.gstatic.com" :crossorigin "true"}]
      [:link {:rel "stylesheet" :href "https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap"}]
      [:script {:src "/wasm_exec.js"}]

      [:link {:rel "stylesheet" :href "/style.css"}]
      [:script {:type "module"} "initWasm(['/main.coni']);"]]
    [:body
      [:div {:id "app"}
        [:div {:style "display:flex;align-items:center;justify-content:center;min-height:100vh;"}
          [:p {:style "color:#94a3b8;font-family:Inter,sans-serif;"} "Loading..."]]]]])

;; ── Boot ────────────────────────────────────────────────────────────
(conimo/start-app
  {:port 3000
   :ws-port 3001
   :static-dir "frontend/"
   :ssr-component app-page
   :api-router api-handler})
