"""剪辑模式枚举 & 模板版本 单元测试。"""

from __future__ import annotations

from domain.editing_mode import EditingMode
from domain.template_version import EditTemplateVersion


class TestEditingMode:
    """剪辑模式枚举。"""

    def test_one_take_value(self):
        assert EditingMode.ONE_TAKE == "one_take"

    def test_pip_value(self):
        assert EditingMode.PIP == "pip"

    def test_voice_over_value(self):
        assert EditingMode.VOICE_OVER == "voice_over"

    def test_voice_pip_value(self):
        assert EditingMode.VOICE_PIP == "voice_pip"

    def test_is_str_enum(self):
        assert isinstance(EditingMode.ONE_TAKE, str)
        assert EditingMode.ONE_TAKE + "_test" == "one_take_test"

    def test_members_count(self):
        assert len(EditingMode) == 4

    def test_from_string(self):
        assert EditingMode("one_take") == EditingMode.ONE_TAKE
        assert EditingMode("pip") == EditingMode.PIP
        assert EditingMode("voice_over") == EditingMode.VOICE_OVER
        assert EditingMode("voice_pip") == EditingMode.VOICE_PIP


class TestEditTemplateVersionCreate:
    """EditTemplateVersion.create 工厂方法。"""

    def test_create_minimal(self):
        v = EditTemplateVersion.create(template_id="tpl-1", version=1)
        assert v.template_id == "tpl-1"
        assert v.version == 1
        assert v.id  # 自动生成
        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_create_with_name(self):
        v = EditTemplateVersion.create(template_id="tpl-1", version=2, name="v2")
        assert v.name == "v2"
        assert v.version == 2

    def test_create_with_editing_mode(self):
        v = EditTemplateVersion.create(template_id="tpl-1", version=1, editing_mode="pip")
        assert v.editing_mode == "pip"

    def test_create_with_config(self):
        config = {"duration": 30, "resolution": "1080p"}
        v = EditTemplateVersion.create(template_id="tpl-1", version=1, config=config)
        assert v.config == config
        # 确保是副本还是引用
        config["duration"] = 60
        # 不假设一定是深拷贝，只验证初始值正确

    def test_create_with_clip_configs(self):
        clips = [{"type": "video", "url": "/a.mp4"}, {"type": "text", "text": "hi"}]
        v = EditTemplateVersion.create(template_id="tpl-1", version=1, clip_configs=clips)
        assert len(v.clip_configs) == 2
        assert v.clip_configs[0]["type"] == "video"

    def test_create_with_change_note(self):
        v = EditTemplateVersion.create(template_id="tpl-1", version=1, change_note="Initial version")
        assert v.change_note == "Initial version"

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

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

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

    def test_create_generates_unique_ids(self):
        v1 = EditTemplateVersion.create(template_id="tpl-1", version=1)
        v2 = EditTemplateVersion.create(template_id="tpl-1", version=2)
        assert v1.id != v2.id

    def test_create_id_is_hex(self):
        v = EditTemplateVersion.create(template_id="tpl-1", version=1)
        int(v.id, 16)  # 合法 hex

    def test_created_at_is_set(self):
        v = EditTemplateVersion.create(template_id="tpl-1", version=1)
        assert v.created_at is not None
        # 应该是 UTC 时间
        assert v.created_at.tzinfo is not None
