"""phase8 edit template plan

Revision ID: 016
Revises: 015
Create Date: 2026-07-01
"""

import sqlalchemy as sa

from alembic import op

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


def upgrade() -> None:
    # --- edit_templates: 替换为 Phase 8 新 schema ---
    # 删除旧列
    op.drop_column("edit_templates", "project_id")
    op.drop_column("edit_templates", "target_duration")
    op.drop_column("edit_templates", "clip_count")
    op.drop_column("edit_templates", "is_active")
    op.drop_column("edit_templates", "created_by_user_id")
    op.drop_column("edit_templates", "metadata")

    # 添加新列
    op.add_column(
        "edit_templates",
        sa.Column("template_type", sa.String(50), nullable=False, server_default="default"),
    )
    op.add_column(
        "edit_templates",
        sa.Column("config", sa.JSON(), nullable=False, server_default="{}"),
    )
    op.add_column(
        "edit_templates",
        sa.Column("preview_url", sa.String(1000), nullable=False, server_default=""),
    )
    op.add_column(
        "edit_templates",
        sa.Column("sort_weight", sa.Integer(), nullable=False, server_default="0"),
    )
    op.add_column(
        "edit_templates",
        sa.Column("status", sa.String(20), nullable=False, server_default="active"),
    )

    # 添加索引
    op.create_index("ix_edit_templates_template_type", "edit_templates", ["template_type"])
    op.create_index("ix_edit_templates_sort_weight", "edit_templates", ["sort_weight"])
    op.create_index("ix_edit_templates_status", "edit_templates", ["status"])

    # --- edit_plans: 重建表（在 011 中被删除） ---
    op.create_table(
        "edit_plans",
        sa.Column("id", sa.String(32), primary_key=True),
        sa.Column("template_id", sa.String(32), nullable=False, index=True),
        sa.Column("name", sa.String(200), nullable=False),
        sa.Column("status", sa.String(20), nullable=False, server_default="draft", index=True),
        sa.Column("total_duration", sa.Float(), nullable=False, server_default="0"),
        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_plans")

    op.drop_index("ix_edit_templates_status", "edit_templates")
    op.drop_index("ix_edit_templates_sort_weight", "edit_templates")
    op.drop_index("ix_edit_templates_template_type", "edit_templates")

    op.drop_column("edit_templates", "status")
    op.drop_column("edit_templates", "sort_weight")
    op.drop_column("edit_templates", "preview_url")
    op.drop_column("edit_templates", "config")
    op.drop_column("edit_templates", "template_type")

    # 恢复旧列
    op.add_column(
        "edit_templates",
        sa.Column("project_id", sa.String(32), nullable=False, server_default=""),
    )
    op.add_column(
        "edit_templates",
        sa.Column("target_duration", sa.Float(), nullable=False, server_default="30"),
    )
    op.add_column(
        "edit_templates",
        sa.Column("clip_count", sa.Integer(), nullable=False, server_default="3"),
    )
    op.add_column(
        "edit_templates",
        sa.Column("is_active", sa.Boolean(), nullable=False, server_default=sa.true()),
    )
    op.add_column(
        "edit_templates",
        sa.Column("created_by_user_id", sa.String(32), nullable=False, server_default=""),
    )
    op.add_column(
        "edit_templates",
        sa.Column("metadata", sa.JSON(), nullable=False, server_default="{}"),
    )
