(ns ajet-chat.e2e.tests.channels-test
  "Workflow 2: Create channels and navigate between them."
  (:require [ajet-chat.e2e.config :as config]
            [ajet-chat.e2e.helpers :as h]
            [ajet-chat.e2e.setup :as setup]
            [promesa.core :as p]))

(defn create-channel-via-ui
  "User can create a new channel from the sidebar."
  []
  (p/let [oauth-creds (setup/setup-gitea-and-provider!)
          browser     (h/launch-browser!)
          context     (h/new-context! browser)
          page        (h/new-page! context)]
    (-> (p/do
          ;; Login and get to app
          (setup/login-and-setup! page config/test-user-a "Channel Test Community" "channel-test")
          (h/wait-for-url page #"/app" {:timeout 15000})

          ;; Wait for app to load
          (h/wait-for-selector page "#sidebar" {:timeout 15000})

          ;; Try clicking the "+" button for channel creation
          (p/let [add-btn (p/catch
                            (.waitForSelector
                             page
                             "#sidebar button:has-text('+'), #sidebar a:has-text('+')"
                             #js {:timeout 5000})
                            (fn [_] nil))]
            (when add-btn
              (p/do
                (.click add-btn)
                (h/wait-for-selector page "input[name='name']" {:timeout 5000})
                (h/fill page "input[name='name']" "test-channel")
                (h/click page "button[type='submit']")
                (h/wait-ms 2000))))

          ;; Verify sidebar has channel entries
          (h/wait-for-selector page "#channel-list, #sidebar" {:timeout 10000})
          (p/let [visible (h/is-visible? page "#sidebar")]
            (assert visible "sidebar should be visible"))

          ;; Click a channel and verify it loads
          (p/let [channel-link (p/catch
                                 (.waitForSelector
                                  page
                                  "[id^='sidebar-channel-']"
                                  #js {:timeout 5000})
                                 (fn [_] nil))]
            (when channel-link
              (p/do
                (.click channel-link)
                (h/wait-ms 1000)
                (h/wait-for-selector page "#channel-header" {:timeout 5000})
                (p/let [visible (h/is-visible? page "#channel-header")]
                  (assert visible "channel header should be visible after clicking channel"))))))

        (p/catch (fn [err]
                   (p/do
                     (h/screenshot! page "channels-create-failure.png")
                     (throw err))))
        (p/finally (fn []
                     (h/close-all! browser))))))

(defn navigate-between-channels
  "User can switch between channels and see correct content."
  []
  (p/let [oauth-creds (setup/setup-gitea-and-provider!)
          browser     (h/launch-browser!)
          context     (h/new-context! browser)
          page        (h/new-page! context)]
    (-> (p/do
          ;; Login
          (setup/login-and-setup! page config/test-user-a "Nav Test Community" "nav-test")
          (h/wait-for-url page #"/app" {:timeout 15000})

          ;; Wait for sidebar
          (h/wait-for-selector page "#sidebar" {:timeout 15000})

          ;; Get all channel links
          (p/let [channels (.locator page "[id^='sidebar-channel-']")
                  count    (.count channels)]
            (when (> count 1)
              (p/let [;; Click first channel
                      _ (.click (.nth channels 0))
                      _ (h/wait-ms 500)
                      _ (h/wait-for-selector page "#channel-header")
                      header1 (h/text-content page "#channel-header")
                      ;; Click second channel
                      _ (.click (.nth channels 1))
                      _ (h/wait-ms 500)
                      _ (h/wait-for-selector page "#channel-header")
                      header2 (h/text-content page "#channel-header")]
                (assert (not= header1 header2)
                        "different channels should show different headers")))))

        (p/catch (fn [err]
                   (p/do
                     (h/screenshot! page "channels-nav-failure.png")
                     (throw err))))
        (p/finally (fn []
                     (h/close-all! browser))))))

(def tests
  [{:name "create-channel-via-ui"
    :test-fn create-channel-via-ui}
   {:name "navigate-between-channels"
    :test-fn navigate-between-channels}])
