"""Phase 8: Create template_clip_configs and edit_plan_clips tables

Revision ID: 017
Revises: 016
Create Date: 2026-07-01

新增两张表：
- template_clip_configs: 模板片段配置（定义模板中每个片段的规则）
- edit_plan_clips: 剪辑计划片段（剪辑计划中的具体片段实例）
"""

import sqlalchemy as sa

from alembic import op

revision = "017"
down_revision = "016"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # template_clip_configs: 模板片段配置表
    op.create_table(
        "template_clip_configs",
        sa.Column("id", sa.String(32), primary_key=True),
        sa.Column("template_id", sa.String(32), nullable=False, index=True),
        sa.Column("clip_type", sa.String(20), nullable=False, index=True),
        sa.Column("order", sa.Integer, nullable=False),
        sa.Column("min_duration", sa.Float, nullable=False, server_default="0.0"),
        sa.Column("max_duration", sa.Float, nullable=False, server_default="0.0"),
        sa.Column("text_template", sa.Text, nullable=False, server_default=""),
        sa.Column("material_requirements", sa.JSON, nullable=False, server_default="{}"),
        sa.Column("transition_effect", sa.String(20), nullable=False, server_default="cut"),
        sa.Column("config", sa.JSON, nullable=False, server_default="{}"),
        sa.Column(
            "created_at",
            sa.DateTime,
            nullable=False,
            server_default=sa.func.now(),
        ),
        sa.Column(
            "updated_at",
            sa.DateTime,
            nullable=False,
            server_default=sa.func.now(),
        ),
    )

    # edit_plan_clips: 剪辑计划片段表
    op.create_table(
        "edit_plan_clips",
        sa.Column("id", sa.String(32), primary_key=True),
        sa.Column("plan_id", sa.String(32), nullable=False, index=True),
        sa.Column("clip_type", sa.String(20), nullable=False, index=True),
        sa.Column("order", sa.Integer, nullable=False),
        sa.Column("template_clip_config_id", sa.String(32), nullable=False, server_default="", index=True),
        sa.Column("asset_id", sa.String(32), nullable=False, server_default="", index=True),
        sa.Column("text_content", sa.Text, nullable=False, server_default=""),
        sa.Column("start_time", sa.Float, nullable=False, server_default="0.0"),
        sa.Column("duration", sa.Float, nullable=False, server_default="0.0"),
        sa.Column("transition_effect", sa.String(20), nullable=False, server_default="cut"),
        sa.Column("status", sa.String(20), nullable=False, server_default="pending", index=True),
        sa.Column("config", sa.JSON, nullable=False, server_default="{}"),
        sa.Column(
            "created_at",
            sa.DateTime,
            nullable=False,
            server_default=sa.func.now(),
        ),
        sa.Column(
            "updated_at",
            sa.DateTime,
            nullable=False,
            server_default=sa.func.now(),
        ),
    )


def downgrade() -> None:
    op.drop_table("edit_plan_clips")
    op.drop_table("template_clip_configs")
