"""
EditTemplate editing_mode 字段单元测试

覆盖：
- EditTemplate.create() 带 editing_mode
- 默认值 "one_take"
- 无效 editing_mode 抛 ValueError
- EditingMode 枚举值完整性
- config_schemas 中 editing_mode 和 transition_enabled 字段
"""

from __future__ import annotations

import os
import sys
from datetime import datetime, timezone
from pathlib import Path

os.environ.setdefault("JWT_SECRET_KEY", "unit-test-secret-key-for-testing")
os.environ.setdefault("DATABASE_URL", "sqlite:///test.db")

import pytest

sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "apps" / "api"))

from packages.domain.config_schemas import (
    DEFAULT_EDIT_PLAN_CONFIG,
    DEFAULT_EDIT_TEMPLATE_CONFIG,
    normalize_plan_config,
    normalize_template_config,
)
from packages.domain.edit_template import EditTemplate, EditTemplateStatus
from packages.domain.editing_mode import EditingMode

# ---------------------------------------------------------------------------
# 测试：EditTemplate.create() 带 editing_mode
# ---------------------------------------------------------------------------


class TestEditTemplateEditingMode:
    """EditTemplate editing_mode 字段测试"""

    def test_create_with_editing_mode(self):
        """create() 指定 editing_mode"""
        tpl = EditTemplate.create(
            name="测试模板",
            editing_mode=EditingMode.PIP.value,
        )
        assert tpl.editing_mode == "pip"

    def test_create_default_editing_mode(self):
        """create() 不指定 editing_mode → 默认 one_take"""
        tpl = EditTemplate.create(name="默认模式模板")
        assert tpl.editing_mode == "one_take"

    def test_create_all_editing_modes(self):
        """所有 EditingMode 枚举值均可创建"""
        for mode in EditingMode:
            tpl = EditTemplate.create(
                name=f"模板-{mode.value}",
                editing_mode=mode.value,
            )
            assert tpl.editing_mode == mode.value

    def test_create_invalid_editing_mode(self):
        """无效 editing_mode 抛 ValueError"""
        with pytest.raises(ValueError, match="无效的 editing_mode"):
            EditTemplate.create(
                name="无效模板",
                editing_mode="invalid_mode",
            )

    def test_editing_mode_case_sensitive(self):
        """editing_mode 大小写敏感"""
        with pytest.raises(ValueError):
            EditTemplate.create(
                name="大写模式",
                editing_mode="ONE_TAKE",  # 应小写
            )


# ---------------------------------------------------------------------------
# 测试：EditingMode 枚举完整性
# ---------------------------------------------------------------------------


class TestEditingModeEnum:
    """EditingMode 枚举值测试"""

    def test_enum_values(self):
        """枚举包含4种模式"""
        assert EditingMode.ONE_TAKE.value == "one_take"
        assert EditingMode.PIP.value == "pip"
        assert EditingMode.VOICE_OVER.value == "voice_over"
        assert EditingMode.VOICE_PIP.value == "voice_pip"

    def test_enum_count(self):
        """枚举共4个成员"""
        assert len(EditingMode) == 4

    def test_str_enum(self):
        """EditingMode 是 StrEnum"""
        assert isinstance(EditingMode.ONE_TAKE, str)
        assert EditingMode.ONE_TAKE == "one_take"


# ---------------------------------------------------------------------------
# 测试：config_schemas 中的 editing_mode
# ---------------------------------------------------------------------------


class TestConfigSchemasEditingMode:
    """config_schemas editing_mode 和 transition_enabled 字段测试"""

    def test_default_plan_config_has_editing_mode(self):
        """DEFAULT_EDIT_PLAN_CONFIG 包含 editing_mode"""
        assert "editing_mode" in DEFAULT_EDIT_PLAN_CONFIG
        assert DEFAULT_EDIT_PLAN_CONFIG["editing_mode"] == "one_take"

    def test_default_template_config_has_editing_mode(self):
        """DEFAULT_EDIT_TEMPLATE_CONFIG 包含 editing_mode"""
        assert "editing_mode" in DEFAULT_EDIT_TEMPLATE_CONFIG
        assert DEFAULT_EDIT_TEMPLATE_CONFIG["editing_mode"] == "one_take"

    def test_default_template_config_has_transition_enabled(self):
        """DEFAULT_EDIT_TEMPLATE_CONFIG 包含 transition_enabled"""
        assert "transition_enabled" in DEFAULT_EDIT_TEMPLATE_CONFIG
        assert DEFAULT_EDIT_TEMPLATE_CONFIG["transition_enabled"] is True

    def test_normalize_plan_config_editing_mode(self):
        """normalize_plan_config 处理 editing_mode"""
        config = normalize_plan_config({"editing_mode": "pip"})
        assert config["editing_mode"] == "pip"

    def test_normalize_plan_config_default_editing_mode(self):
        """normalize_plan_config 空输入 → 默认 editing_mode"""
        config = normalize_plan_config({})
        assert config["editing_mode"] == "one_take"

    def test_normalize_template_config_editing_mode(self):
        """normalize_template_config 处理 editing_mode"""
        config = normalize_template_config({"editing_mode": "voice_pip"})
        assert config["editing_mode"] == "voice_pip"

    def test_normalize_template_config_transition_enabled(self):
        """normalize_template_config 处理 transition_enabled"""
        config = normalize_template_config({"transition_enabled": False})
        assert config["transition_enabled"] is False

    def test_normalize_template_config_defaults(self):
        """normalize_template_config 空输入 → 默认值"""
        config = normalize_template_config({})
        assert config["editing_mode"] == "one_take"
        assert config["transition_enabled"] is True


# ---------------------------------------------------------------------------
# 测试：EditTemplate 实体直接构造
# ---------------------------------------------------------------------------


class TestEditTemplateEntityDirect:
    """直接构造 EditTemplate 实体测试 editing_mode"""

    def test_direct_construction(self):
        """直接构造带 editing_mode 的实体"""
        now = datetime.now(timezone.utc)
        tpl = EditTemplate(
            id="tpl-test",
            name="直接构造",
            description="",
            template_type="default",
            editing_mode="voice_over",
            config={},
            preview_url="",
            sort_weight=0,
            status=EditTemplateStatus.ACTIVE,
            created_at=now,
            updated_at=now,
        )
        assert tpl.editing_mode == "voice_over"

    def test_all_enum_values_accepted(self):
        """所有 EditingMode 枚举值均可通过 create() 验证"""
        for mode in EditingMode:
            tpl = EditTemplate.create(
                name=f"模板-{mode.value}",
                editing_mode=mode.value,
            )
            assert tpl.editing_mode == mode.value
