"""EditTemplateVersion 单元测试."""

from __future__ import annotations

from datetime import datetime, timezone

import pytest
from domain.template_version import EditTemplateVersion


class TestEditTemplateVersionCreate:
    """create() 工厂方法测试."""

    def test_create_basic(self):
        v = EditTemplateVersion.create("tmpl_001", 1)
        assert v.id is not None
        assert len(v.id) == 32
        assert v.template_id == "tmpl_001"
        assert v.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 == ""
        assert v.created_at is not None

    def test_create_with_all_fields(self):
        v = EditTemplateVersion.create(
            "tmpl_001",
            3,
            name="第三版",
            editing_mode="pip",
            config={"bgm": True},
            clip_configs=[{"id": "c1", "type": "video"}],
            change_note="优化剪辑逻辑",
            published_by="user_123",
        )
        assert v.template_id == "tmpl_001"
        assert v.version == 3
        assert v.name == "第三版"
        assert v.editing_mode == "pip"
        assert v.config == {"bgm": True}
        assert v.clip_configs == [{"id": "c1", "type": "video"}]
        assert v.change_note == "优化剪辑逻辑"
        assert v.published_by == "user_123"

    def test_create_config_none_defaults_to_empty(self):
        v = EditTemplateVersion.create("t1", 1, config=None)
        assert v.config == {}

    def test_create_clip_configs_none_defaults_to_empty(self):
        v = EditTemplateVersion.create("t1", 1, clip_configs=None)
        assert v.clip_configs == []

    def test_create_unique_ids(self):
        v1 = EditTemplateVersion.create("t1", 1)
        v2 = EditTemplateVersion.create("t1", 1)
        assert v1.id != v2.id

    def test_create_sets_created_at(self):
        before = datetime.now(timezone.utc)
        v = EditTemplateVersion.create("t1", 1)
        after = datetime.now(timezone.utc)
        assert before <= v.created_at <= after


class TestEditTemplateVersionConstruction:
    """直接构造测试."""

    def test_direct_construction(self):
        now = datetime(2026, 1, 1, tzinfo=timezone.utc)
        v = EditTemplateVersion(
            id="v1",
            template_id="t1",
            version=5,
            name="v5",
            editing_mode="voice_over",
            config={"key": "value"},
            clip_configs=[{"a": 1}, {"b": 2}],
            change_note="test",
            published_by="admin",
            created_at=now,
        )
        assert v.id == "v1"
        assert v.template_id == "t1"
        assert v.version == 5
        assert v.name == "v5"
        assert v.editing_mode == "voice_over"
        assert v.config == {"key": "value"}
        assert v.clip_configs == [{"a": 1}, {"b": 2}]
        assert v.change_note == "test"
        assert v.published_by == "admin"
        assert v.created_at == now

    def test_default_values(self):
        v = EditTemplateVersion(id="v1", template_id="t1", 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 == ""


class TestEditTemplateVersionSlots:
    """slots 测试."""

    def test_slots_no_extra_attrs(self):
        v = EditTemplateVersion.create("t1", 1)
        with pytest.raises((AttributeError, TypeError)):
            v.new_field = "value"  # type: ignore[attr-defined]


class TestEditTemplateVersionEquality:
    """相等性测试."""

    def test_equal_same_id_and_version(self):
        now = datetime(2026, 1, 1, tzinfo=timezone.utc)
        v1 = EditTemplateVersion(id="same", template_id="t1", version=1, created_at=now)
        v2 = EditTemplateVersion(id="same", template_id="t1", version=1, created_at=now)
        assert v1 == v2

    def test_not_equal_different_id(self):
        now = datetime(2026, 1, 1, tzinfo=timezone.utc)
        v1 = EditTemplateVersion(id="v1", template_id="t1", version=1, created_at=now)
        v2 = EditTemplateVersion(id="v2", template_id="t1", version=1, created_at=now)
        assert v1 != v2

    def test_not_equal_different_version(self):
        now = datetime(2026, 1, 1, tzinfo=timezone.utc)
        v1 = EditTemplateVersion(id="same", template_id="t1", version=1, created_at=now)
        v2 = EditTemplateVersion(id="same", template_id="t1", version=2, created_at=now)
        assert v1 != v2
