"""
Tag 标签领域模型单元测试
"""

import pytest

from packages.domain.tag import Tag


class TestTagCreate:
    """创建标签测试"""

    def test_create_basic(self):
        tag = Tag.create(user_id="user-1", name="风景")
        assert tag.id is not None
        assert len(tag.id) == 32
        assert tag.user_id == "user-1"
        assert tag.name == "风景"

    def test_create_strips_name(self):
        tag = Tag.create(user_id="user-1", name="  风景  ")
        assert tag.name == "风景"

    def test_create_empty_name_raises(self):
        with pytest.raises(ValueError, match="标签名称不能为空"):
            Tag.create(user_id="user-1", name="")

    def test_create_whitespace_name_raises(self):
        with pytest.raises(ValueError, match="标签名称不能为空"):
            Tag.create(user_id="user-1", name="   ")

    def test_create_has_created_at(self):
        tag = Tag.create(user_id="user-1", name="美食")
        assert tag.created_at is not None


class TestTagExtended:
    """Tag 深度补充测试"""

    def test_create_id_is_unique(self):
        """多次创建生成不同的 id"""
        tag1 = Tag.create(user_id="u1", name="标签1")
        tag2 = Tag.create(user_id="u1", name="标签2")
        assert tag1.id != tag2.id
        assert len(tag1.id) == 32
        assert len(tag2.id) == 32

    def test_create_id_is_hex(self):
        """id 是十六进制字符串"""
        tag = Tag.create(user_id="u1", name="测试")
        int(tag.id, 16)  # 不抛错就是合法 hex

    def test_slots_no_extra_attributes(self):
        """slots=True 不能添加新属性"""
        import pytest

        tag = Tag.create(user_id="u1", name="测试")
        with pytest.raises(AttributeError):
            tag.new_attr = "value"  # type: ignore[attr-defined]

    def test_create_very_long_name(self):
        long_name = "标签" * 50
        tag = Tag.create(user_id="u1", name=long_name)
        assert tag.name == long_name
        assert len(tag.name) == 100

    def test_create_unicode_name(self):
        tag = Tag.create(user_id="u1", name="🔥热门标签✨")
        assert tag.name == "🔥热门标签✨"

    def test_create_name_only_spaces_between_text(self):
        """中间有空格的标签名正常保留"""
        tag = Tag.create(user_id="u1", name="  美食 探店  ")
        assert tag.name == "美食 探店"

    def test_create_different_user_ids(self):
        tag1 = Tag.create(user_id="user-001", name="标签")
        tag2 = Tag.create(user_id="user-999", name="标签")
        assert tag1.user_id == "user-001"
        assert tag2.user_id == "user-999"
        assert tag1.id != tag2.id

    def test_name_is_string(self):
        tag = Tag.create(user_id="u1", name="12345")
        assert isinstance(tag.name, str)
        assert tag.name == "12345"

    def test_equality(self):
        """两个相同属性的 tag 不相等（id 不同）"""
        tag1 = Tag.create(user_id="u1", name="同名")
        tag2 = Tag.create(user_id="u1", name="同名")
        assert tag1 != tag2
