(ns ajet.chat.shared.schema-unit-test
  (:require [clojure.test :refer [deftest testing is]]
            [ajet.chat.shared.schema :as schema]))

;;; ---------------------------------------------------------------------------
;;; Predicate tests
;;; ---------------------------------------------------------------------------

(deftest uuid-str?-valid
  (is (schema/uuid-str? "12345678-1234-1234-1234-123456789abc")))

(deftest uuid-str?-invalid
  (is (not (schema/uuid-str? "not-a-uuid")))
  (is (not (schema/uuid-str? "")))
  (is (not (schema/uuid-str? nil))))

(deftest uuid?-java-uuid
  (is (schema/uuid? (java.util.UUID/randomUUID))))

(deftest uuid?-string-uuid
  (is (schema/uuid? "12345678-1234-1234-1234-123456789abc")))

(deftest slug?-valid
  (is (schema/slug? "my-community"))
  (is (schema/slug? "test-123")))

(deftest slug?-invalid
  (is (not (schema/slug? "My-Community")))  ;; uppercase
  (is (not (schema/slug? "-bad")))           ;; starts with hyphen
  (is (not (schema/slug? "a")))              ;; too short for regex
  (is (not (schema/slug? nil))))

(deftest non-blank-string?-valid
  (is (schema/non-blank-string? "hello")))

(deftest non-blank-string?-invalid
  (is (not (schema/non-blank-string? "")))
  (is (not (schema/non-blank-string? "   ")))
  (is (not (schema/non-blank-string? nil))))

;;; ---------------------------------------------------------------------------
;;; validate / valid? tests
;;; ---------------------------------------------------------------------------

(deftest validate-community-valid
  (let [data {:id   (java.util.UUID/randomUUID)
              :name "Test Community"
              :slug "test-comm"}]
    (is (= data (schema/validate ::schema/community data)))))

(deftest validate-community-invalid-throws
  (is (thrown-with-msg? clojure.lang.ExceptionInfo #"Validation failed"
        (schema/validate ::schema/community {:id "not-a-uuid" :name "" :slug "BAD"}))))

(deftest valid?-message-valid
  (is (true? (schema/valid? ::schema/message
               {:id         (java.util.UUID/randomUUID)
                :channel-id (java.util.UUID/randomUUID)
                :user-id    (java.util.UUID/randomUUID)
                :body-md    "hello world"}))))

(deftest valid?-message-missing-body
  (is (false? (schema/valid? ::schema/message
                {:id         (java.util.UUID/randomUUID)
                 :channel-id (java.util.UUID/randomUUID)
                 :user-id    (java.util.UUID/randomUUID)}))))
