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

(def test-uuid "12345678-1234-1234-1234-123456789abc")
(def test-uuid2 "abcdef01-2345-6789-abcd-ef0123456789")

;;; ---------------------------------------------------------------------------
;;; parse tests
;;; ---------------------------------------------------------------------------

(deftest parse-user-mention
  (let [result (mentions/parse (str "hello @<user:" test-uuid "> world"))]
    (is (= 1 (count result)))
    (is (= {:type :user :id test-uuid} (first result)))))

(deftest parse-here-mention
  (let [result (mentions/parse "hey @<here> everyone")]
    (is (= 1 (count result)))
    (is (= {:type :here} (first result)))))

(deftest parse-channel-link
  (let [result (mentions/parse (str "check #<channel:" test-uuid ">"))]
    (is (= 1 (count result)))
    (is (= {:type :channel :id test-uuid} (first result)))))

(deftest parse-multiple-mentions
  (let [body (str "@<user:" test-uuid "> and @<user:" test-uuid2 "> and @<here>")
        result (mentions/parse body)]
    (is (= 3 (count result)))
    (is (= #{:user :here} (set (map :type result))))))

(deftest parse-no-mentions
  (is (= [] (mentions/parse "just a plain message"))))

(deftest parse-ignores-code-blocks
  (testing "mentions inside code blocks are not parsed"
    (let [result (mentions/parse (str "`@<user:" test-uuid ">`"))]
      (is (= [] result)))))

(deftest parse-ignores-fenced-code-blocks
  (let [result (mentions/parse (str "```\n@<user:" test-uuid ">\n```"))]
    (is (= [] result))))

;;; ---------------------------------------------------------------------------
;;; render tests
;;; ---------------------------------------------------------------------------

(deftest render-user-mention
  (let [body   (str "hello @<user:" test-uuid ">")
        lookup (fn [type id] (when (= type :user) "Alice"))
        result (mentions/render body lookup)]
    (is (= "hello @Alice" result))))

(deftest render-unknown-user
  (let [body   (str "@<user:" test-uuid ">")
        lookup (constantly nil)
        result (mentions/render body lookup)]
    (is (= "@unknown-user" result))))

(deftest render-channel-link
  (let [body   (str "#<channel:" test-uuid ">")
        lookup (fn [type id] (when (= type :channel) "general"))
        result (mentions/render body lookup)]
    (is (= "#general" result))))

(deftest render-here-mention
  (is (= "hey @here" (mentions/render "hey @<here>" (constantly nil)))))
