{:tasks
 {;; ---------------------------------------------------------------------------
  ;; Dev infrastructure (Docker)
  ;; ---------------------------------------------------------------------------
  infra:dev
  {:doc  "Start dev infrastructure (Postgres, NATS, MinIO)"
   :task (shell "docker compose -f docker-compose.dev.yml up -d")}

  infra:dev:stop
  {:doc  "Stop dev infrastructure"
   :task (shell "docker compose -f docker-compose.dev.yml down")}

  infra:test
  {:doc  "Start test infrastructure (Postgres, NATS, MinIO on test ports)"
   :task (shell "docker compose -f docker-compose.test.yml up -d")}

  infra:test:stop
  {:doc  "Stop test infrastructure"
   :task (shell "docker compose -f docker-compose.test.yml down")}

  ;; ---------------------------------------------------------------------------
  ;; Development
  ;; ---------------------------------------------------------------------------
  dev
  {:doc  "Start dev infra + nREPL with all modules on the classpath"
   :task (do (run 'infra:dev)
             (shell "clj -M:dev -m nrepl.cmdline --middleware '[cider.nrepl/cider-middleware refactor-nrepl.middleware/wrap-refactor]'"))}

  ;; ---------------------------------------------------------------------------
  ;; Testing
  ;; ---------------------------------------------------------------------------
  test
  {:doc  "Run all tests (starts test infra if needed)"
   :task (do (run 'infra:test)
             (shell "clj -M:test -m kaocha.runner"))}

  test:unit
  {:doc  "Run unit tests only (no Docker needed)"
   :task (shell "clj -M:test -m kaocha.runner --focus unit")}

  test:integration
  {:doc  "Run integration tests (starts test infra if needed)"
   :task (do (run 'infra:test)
             (shell "clj -M:test -m kaocha.runner --focus integration"))}

  test:e2e
  {:doc  "Run e2e tests (starts test infra if needed)"
   :task (do (run 'infra:test)
             (shell "clj -M:test -m kaocha.runner --focus e2e"))}

  test:e2e:browser
  {:doc  "Run Playwright browser E2E tests (nbb + ClojureScript)"
   :task (do
           ;; Start full e2e stack (infra + app services + Gitea)
           (shell "docker compose -f docker-compose.test.yml --profile e2e up -d --build")
           ;; Install npm deps and Playwright browser
           (shell "cd e2e && npm install && npx playwright install chromium")
           ;; Run the nbb test suite
           (let [result (shell {:continue true} "cd e2e && npx nbb -cp src -m ajet-chat.e2e.runner")]
             ;; Tear down the stack
             (shell "docker compose -f docker-compose.test.yml --profile e2e down -v")
             (System/exit (:exit result))))}

  ;; Per-module test tasks
  test:shared
  {:doc  "Run all shared module tests"
   :task (do (run 'infra:test)
             (shell "clj -M:test -m kaocha.runner --focus shared"))}

  test:shared:unit
  {:doc  "Run shared unit tests only"
   :task (shell "clj -M:test -m kaocha.runner --focus shared-unit")}

  test:shared:integration
  {:doc  "Run shared integration tests"
   :task (do (run 'infra:test)
             (shell "clj -M:test -m kaocha.runner --focus shared-integration"))}

  test:api
  {:doc  "Run all API module tests"
   :task (do (run 'infra:test)
             (shell "clj -M:test -m kaocha.runner --focus api"))}

  test:api:unit
  {:doc  "Run API unit tests only"
   :task (shell "clj -M:test -m kaocha.runner --focus api-unit")}

  test:api:integration
  {:doc  "Run API integration tests"
   :task (do (run 'infra:test)
             (shell "clj -M:test -m kaocha.runner --focus api-integration"))}

  test:auth-gw
  {:doc  "Run all auth-gw module tests"
   :task (do (run 'infra:test)
             (shell "clj -M:test -m kaocha.runner --focus auth-gw"))}

  test:auth-gw:integration
  {:doc  "Run auth-gw integration tests"
   :task (do (run 'infra:test)
             (shell "clj -M:test -m kaocha.runner --focus auth-gw-integration"))}

  ;; ---------------------------------------------------------------------------
  ;; Build
  ;; ---------------------------------------------------------------------------
  build
  {:doc  "Build uberjar for a module. Usage: bb build <module>"
   :task (let [module (first *command-line-args*)]
           (when-not module
             (println "Usage: bb build <module>  (api | auth-gw | web-sm | tui-sm | cli)")
             (System/exit 1))
           (shell (str "clj -T:build uber :module " module)))}

  clean
  {:doc  "Clean build artifacts for a module. Usage: bb clean <module>"
   :task (let [module (first *command-line-args*)]
           (if module
             (shell (str "clj -T:build clean :module " module))
             (shell "clj -T:build clean-all")))}

  ;; ---------------------------------------------------------------------------
  ;; Production (Docker Compose)
  ;; ---------------------------------------------------------------------------
  prod
  {:doc  "Start production stack (docker compose)"
   :task (shell "docker compose up -d --build")}

  prod:stop
  {:doc  "Stop production stack"
   :task (shell "docker compose down")}

  prod:logs
  {:doc  "Tail production logs"
   :task (shell "docker compose logs -f")}

  ;; ---------------------------------------------------------------------------
  ;; Database utilities
  ;; ---------------------------------------------------------------------------
  db:reset-test
  {:doc  "Reset test database (drop & recreate public schema)"
   :task (shell "docker exec ajet-chat-postgres-test-1 psql -U ajet -d ajet_chat_test -c 'DROP SCHEMA public CASCADE; CREATE SCHEMA public;'")}}}
