"""
TitleLibraryItem 标题库领域模型单元测试
"""

from packages.domain.title_library import TitleLibraryItem


class TestTitleLibraryItem:
    """TitleLibraryItem 测试"""

    def test_create_minimal(self):
        item = TitleLibraryItem(id="t1", user_id="u1", name="标题1", text="这是标题文本")
        assert item.id == "t1"
        assert item.user_id == "u1"
        assert item.name == "标题1"
        assert item.text == "这是标题文本"

    def test_default_values(self):
        item = TitleLibraryItem(id="t1", user_id="u1", name="n", text="t")
        assert item.category == "default"
        assert item.description == ""
        assert item.tags == []
        assert item.usage_count == 0
        assert item.is_active is True
        assert item.metadata_ == {}

    def test_with_category(self):
        item = TitleLibraryItem(
            id="t1",
            user_id="u1",
            name="n",
            text="t",
            category="美食",
        )
        assert item.category == "美食"

    def test_with_tags(self):
        item = TitleLibraryItem(
            id="t1",
            user_id="u1",
            name="n",
            text="t",
            tags=["爆款", "美食"],
        )
        assert len(item.tags) == 2
        assert "爆款" in item.tags

    def test_usage_count(self):
        item = TitleLibraryItem(id="t1", user_id="u1", name="n", text="t")
        assert item.usage_count == 0
        item.usage_count = 10
        assert item.usage_count == 10

    def test_inactive(self):
        item = TitleLibraryItem(
            id="t1",
            user_id="u1",
            name="n",
            text="t",
            is_active=False,
        )
        assert item.is_active is False

    def test_with_metadata(self):
        meta = {"source": "import", "quality": "high"}
        item = TitleLibraryItem(
            id="t1",
            user_id="u1",
            name="n",
            text="t",
            metadata_=meta,
        )
        assert item.metadata_["source"] == "import"

    def test_has_timestamps(self):
        item = TitleLibraryItem(id="t1", user_id="u1", name="n", text="t")
        assert item.created_at is not None
        assert item.updated_at is not None

    def test_tags_independence_between_instances(self):
        """不同实例的 tags 列表互不影响"""
        item1 = TitleLibraryItem(id="t1", user_id="u1", name="n1", text="t")
        item2 = TitleLibraryItem(id="t2", user_id="u1", name="n2", text="t")
        item1.tags.append("新标签")
        assert "新标签" not in item2.tags
        assert len(item2.tags) == 0

    def test_metadata_independence_between_instances(self):
        """不同实例的 metadata_ 字典互不影响"""
        item1 = TitleLibraryItem(id="t1", user_id="u1", name="n1", text="t")
        item2 = TitleLibraryItem(id="t2", user_id="u1", name="n2", text="t")
        item1.metadata_["key"] = "value"
        assert "key" not in item2.metadata_

    def test_zero_usage_count(self):
        item = TitleLibraryItem(id="t1", user_id="u1", name="n", text="t", usage_count=0)
        assert item.usage_count == 0

    def test_large_usage_count(self):
        item = TitleLibraryItem(id="t1", user_id="u1", name="n", text="t", usage_count=999999)
        assert item.usage_count == 999999

    def test_negative_usage_count(self):
        """负数使用次数也能存（领域层不做业务校验）"""
        item = TitleLibraryItem(id="t1", user_id="u1", name="n", text="t", usage_count=-1)
        assert item.usage_count == -1

    def test_empty_text(self):
        item = TitleLibraryItem(id="t1", user_id="u1", name="n", text="")
        assert item.text == ""

    def test_long_text(self):
        long_text = "标题" * 1000
        item = TitleLibraryItem(id="t1", user_id="u1", name="n", text=long_text)
        assert item.text == long_text
        assert len(item.text) == 2000

    def test_empty_name(self):
        item = TitleLibraryItem(id="t1", user_id="u1", name="", text="t")
        assert item.name == ""

    def test_special_characters_in_name_and_text(self):
        special = "!@#$%^&*()_+-=[]{}|;':\",./<>?\n\t"
        item = TitleLibraryItem(id="t1", user_id="u1", name=special, text=special)
        assert item.name == special
        assert item.text == special

    def test_unicode_in_name_and_text(self):
        item = TitleLibraryItem(
            id="t1",
            user_id="u1",
            name="中文标题🔥emoji",
            text="支持各种文字：中文、English、日本語、한국어",
        )
        assert "中文" in item.name
        assert "🔥" in item.name
        assert "日本語" in item.text

    def test_many_tags(self):
        tags = [f"tag_{i}" for i in range(100)]
        item = TitleLibraryItem(id="t1", user_id="u1", name="n", text="t", tags=tags)
        assert len(item.tags) == 100
        assert item.tags[0] == "tag_0"
        assert item.tags[99] == "tag_99"

    def test_is_active_toggle(self):
        item = TitleLibraryItem(id="t1", user_id="u1", name="n", text="t", is_active=True)
        item.is_active = False
        assert item.is_active is False
        item.is_active = True
        assert item.is_active is True

    def test_empty_description(self):
        item = TitleLibraryItem(id="t1", user_id="u1", name="n", text="t", description="")
        assert item.description == ""

    def test_custom_category(self):
        item = TitleLibraryItem(id="t1", user_id="u1", name="n", text="t", category="自定义分类")
        assert item.category == "自定义分类"
