(ns ajet-chat.e2e.tests.setup-test
  "Workflow 1: First-time setup wizard — add OAuth provider, login via Gitea,
   create first community."
  (:require [ajet-chat.e2e.config :as config]
            [ajet-chat.e2e.helpers :as h]
            [ajet-chat.e2e.setup :as setup]
            [promesa.core :as p]))

(defn setup-wizard-full-flow
  "First user can configure Gitea OAuth, log in, and create a community."
  []
  (p/let [;; -- Gitea prep (via API, outside browser) --
          oauth-creds (setup/setup-gitea-and-provider!)
          _           (assert (some? (:client-id oauth-creds))
                              "OAuth app should have a client_id")
          ;; -- Launch browser --
          browser (h/launch-browser!)
          context (h/new-context! browser)
          page    (h/new-page! context)]
    (-> (p/do
          ;; Step 1: Navigate to root — should redirect to /setup
          (h/goto page "/")
          (h/wait-for-url page #"/setup" {:timeout 10000})
          (assert (re-find #"/setup" (h/page-url page))
                  "should redirect to setup wizard")

          ;; Step 2: Configure OAuth provider
          (h/wait-for-selector page "select[name='provider-type']")
          (h/select-option page "select[name='provider-type']" "gitea")
          (h/fill page "input[name='display-name']" "Gitea Test")
          (h/fill page "input[name='slug']" "gitea-test")
          (h/fill page "input[name='client-id']" (:client-id oauth-creds))
          (h/fill page "input[name='client-secret']" (:client-secret oauth-creds))
          ;; Base URL uses localhost:4080 — auth-gw has extra_hosts to reach host
          (h/fill page "input[name='base-url']" config/gitea-url)
          ;; Submit provider form — redirects to /auth/login
          (h/click page "button:has-text('Add Provider')")
          (h/wait-for-url page #"/auth/login" {:timeout 10000})

          ;; Step 3: Login page — click Gitea provider
          (h/wait-for-selector page "a[href*='provider=gitea-test']")
          (h/click page "a[href*='provider=gitea-test']")

          ;; Step 5: Gitea login form (on localhost:4080)
          (h/wait-for-selector page "#user_name" {:timeout 15000})
          (h/fill page "#user_name" (:username config/test-user-a))
          (h/fill page "#password" (:password config/test-user-a))
          (h/click page "button:has-text('Sign In')")

          ;; Step 5b: Authorize OAuth app if prompted
          (p/let [auth-btn (p/catch
                             (.waitForSelector
                              page
                              "button:has-text('Authorize Application'), button.ui.green.button"
                              #js {:timeout 5000})
                             (fn [_] nil))]
            (when auth-btn
              (.click auth-btn)))

          ;; Step 6: Redirected back to ajet-chat
          (h/wait-for-url page #"/(setup|app)" {:timeout 30000})

          ;; Step 7: If on create-community page, fill it in
          (p/let [url (h/page-url page)]
            (when (re-find #"/setup" url)
              (p/do
                (h/wait-for-selector page "input[name='name']")
                (h/fill page "input[name='name']" "Test Community")
                (h/fill page "input[name='slug']" "test-community")
                (h/click page "button[type='submit']")
                (h/wait-ms 2000))))

          ;; Step 8: Verify we're on the app page
          (h/wait-for-url page #"/app" {:timeout 15000})
          (assert (re-find #"/app" (h/page-url page))
                  "should land on the main app"))

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

(def tests
  [{:name "setup-wizard-full-flow"
    :test-fn setup-wizard-full-flow}])
