"""
EditTemplateVersion 模板版本领域模型单元测试
"""

import pytest
from domain.template_version import EditTemplateVersion


class TestEditTemplateVersionCreate:
    """创建模板版本测试"""

    def test_create_required_fields(self):
        v = EditTemplateVersion.create(template_id="tpl-1", version=1)
        assert v.id is not None
        assert len(v.id) == 32
        assert v.template_id == "tpl-1"
        assert v.version == 1

    def test_default_values(self):
        v = EditTemplateVersion.create(template_id="tpl-1", version=1)
        assert v.name == ""
        assert v.editing_mode == "one_take"
        assert v.config == {}
        assert v.clip_configs == []
        assert v.change_note == ""
        assert v.published_by == ""

    def test_with_name_and_mode(self):
        v = EditTemplateVersion.create(
            template_id="tpl-1",
            version=2,
            name="风景Vlog模板",
            editing_mode="voice_over",
        )
        assert v.name == "风景Vlog模板"
        assert v.editing_mode == "voice_over"

    def test_with_config(self):
        config = {
            "title": {"font_size": 24},
            "subtitle": {"style": "bottom"},
            "bgm": {"volume": 0.5},
        }
        v = EditTemplateVersion.create(
            template_id="tpl-1",
            version=1,
            config=config,
        )
        assert v.config == config
        assert v.config["title"]["font_size"] == 24

    def test_with_clip_configs(self):
        clips = [
            {"clip_id": 1, "duration": 3.0, "transition": "fade"},
            {"clip_id": 2, "duration": 5.0, "transition": "slide"},
        ]
        v = EditTemplateVersion.create(
            template_id="tpl-1",
            version=1,
            clip_configs=clips,
        )
        assert len(v.clip_configs) == 2
        assert v.clip_configs[0]["clip_id"] == 1

    def test_config_none_defaults_to_empty_dict(self):
        v = EditTemplateVersion.create(template_id="tpl-1", version=1, config=None)
        assert v.config == {}

    def test_clip_configs_none_defaults_to_empty_list(self):
        v = EditTemplateVersion.create(template_id="tpl-1", version=1, clip_configs=None)
        assert v.clip_configs == []

    def test_with_change_note(self):
        v = EditTemplateVersion.create(
            template_id="tpl-1",
            version=3,
            change_note="优化转场效果，新增滤镜",
        )
        assert v.change_note == "优化转场效果，新增滤镜"

    def test_with_published_by(self):
        v = EditTemplateVersion.create(
            template_id="tpl-1",
            version=1,
            published_by="user-123",
        )
        assert v.published_by == "user-123"

    def test_full_version(self):
        config = {"bgm": {"volume": 0.3}}
        clips = [{"clip_id": 1, "duration": 2.5}]
        v = EditTemplateVersion.create(
            template_id="tpl-abc",
            version=5,
            name="正式版v5",
            editing_mode="one_take",
            config=config,
            clip_configs=clips,
            change_note="第五次发布",
            published_by="admin",
        )
        assert v.template_id == "tpl-abc"
        assert v.version == 5
        assert v.name == "正式版v5"
        assert v.editing_mode == "one_take"
        assert v.config == config
        assert v.clip_configs == clips
        assert v.change_note == "第五次发布"
        assert v.published_by == "admin"

    def test_version_number(self):
        for ver in [1, 2, 5, 10, 99]:
            v = EditTemplateVersion.create(template_id="t1", version=ver)
            assert v.version == ver

    def test_has_created_at(self):
        v = EditTemplateVersion.create(template_id="t1", version=1)
        assert v.created_at is not None


class TestEditTemplateVersionSlots:
    """slots 模式属性测试"""

    def test_cannot_add_new_attribute(self):
        v = EditTemplateVersion.create(template_id="t1", version=1)
        with pytest.raises(AttributeError):
            v.nonexistent_field = "value"  # type: ignore[attr-defined]


class TestEditTemplateVersionExtended:
    """EditTemplateVersion 深度补充测试"""

    def test_id_is_hex(self):
        """id 是十六进制字符串"""
        v = EditTemplateVersion.create(template_id="t1", version=1)
        int(v.id, 16)  # 不抛错就是合法 hex

    def test_ids_are_unique(self):
        """不同版本的 id 不同"""
        v1 = EditTemplateVersion.create(template_id="t1", version=1)
        v2 = EditTemplateVersion.create(template_id="t1", version=2)
        assert v1.id != v2.id

    def test_zero_version(self):
        """version=0 也能创建"""
        v = EditTemplateVersion.create(template_id="t1", version=0)
        assert v.version == 0

    def test_negative_version(self):
        """负数 version 也能创建（领域层不做业务校验）"""
        v = EditTemplateVersion.create(template_id="t1", version=-1)
        assert v.version == -1

    def test_large_version(self):
        v = EditTemplateVersion.create(template_id="t1", version=9999)
        assert v.version == 9999

    def test_empty_template_id(self):
        v = EditTemplateVersion.create(template_id="", version=1)
        assert v.template_id == ""

    def test_empty_name(self):
        v = EditTemplateVersion.create(template_id="t1", version=1, name="")
        assert v.name == ""

    def test_empty_change_note(self):
        v = EditTemplateVersion.create(template_id="t1", version=1, change_note="")
        assert v.change_note == ""

    def test_empty_published_by(self):
        v = EditTemplateVersion.create(template_id="t1", version=1, published_by="")
        assert v.published_by == ""

    def test_unicode_name(self):
        v = EditTemplateVersion.create(template_id="t1", version=1, name="🎨 V5 优化版")
        assert "🎨" in v.name
        assert "V5" in v.name

    def test_long_name(self):
        long_name = "版本" * 50
        v = EditTemplateVersion.create(template_id="t1", version=1, name=long_name)
        assert v.name == long_name
        assert len(v.name) == 100

    def test_config_complex_nested(self):
        config = {
            "layer1": {
                "layer2": {
                    "layer3": [1, 2, 3],
                }
            }
        }
        v = EditTemplateVersion.create(template_id="t1", version=1, config=config)
        assert v.config["layer1"]["layer2"]["layer3"] == [1, 2, 3]

    def test_clip_configs_empty_list(self):
        v = EditTemplateVersion.create(template_id="t1", version=1, clip_configs=[])
        assert v.clip_configs == []

    def test_clip_configs_many(self):
        clips = [{"clip_id": i, "duration": float(i)} for i in range(50)]
        v = EditTemplateVersion.create(template_id="t1", version=1, clip_configs=clips)
        assert len(v.clip_configs) == 50
        assert v.clip_configs[0]["clip_id"] == 0
        assert v.clip_configs[49]["clip_id"] == 49
