import sqlalchemy as sa

from alembic import op

revision = "006"
down_revision = "005"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "edit_templates",
        sa.Column("id", sa.String(32), primary_key=True),
        sa.Column("workspace_id", sa.String(32), nullable=False, index=True),
        sa.Column("project_id", sa.String(32), nullable=False, index=True),
        sa.Column("name", sa.String(120), nullable=False),
        sa.Column("description", sa.Text(), nullable=False, server_default=""),
        sa.Column("target_duration", sa.Float(), nullable=False, server_default="30"),
        sa.Column("clip_count", sa.Integer(), nullable=False, server_default="3"),
        sa.Column("is_active", sa.Boolean(), nullable=False, server_default=sa.true()),
        sa.Column("created_by_user_id", sa.String(32), 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()),
    )
    op.create_table(
        "edit_plans",
        sa.Column("id", sa.String(32), primary_key=True),
        sa.Column("workspace_id", sa.String(32), nullable=False, index=True),
        sa.Column("project_id", sa.String(32), nullable=False, index=True),
        sa.Column("template_id", sa.String(32), nullable=False, index=True),
        sa.Column("asset_library_id", sa.String(32), nullable=False, index=True),
        sa.Column("title_id", sa.String(32), nullable=False, server_default=""),
        sa.Column("status", sa.String(20), nullable=False, server_default="draft", index=True),
        sa.Column("summary", sa.Text(), nullable=False, server_default=""),
        sa.Column("created_by_user_id", sa.String(32), 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()),
    )
    op.create_table(
        "edit_plan_clips",
        sa.Column("id", sa.String(32), primary_key=True),
        sa.Column("edit_plan_id", sa.String(32), nullable=False, index=True),
        sa.Column("asset_id", sa.String(32), nullable=False, index=True),
        sa.Column("sequence", sa.Integer(), nullable=False),
        sa.Column("start_time", sa.Float(), nullable=False, server_default="0"),
        sa.Column("duration", sa.Float(), nullable=False, server_default="0"),
        sa.Column("reason", sa.Text(), nullable=False, server_default=""),
    )
    op.add_column("generation_tasks", sa.Column("edit_plan_id", sa.String(32), nullable=False, server_default=""))
    op.create_index("ix_generation_tasks_edit_plan_id", "generation_tasks", ["edit_plan_id"])


def downgrade() -> None:
    op.drop_index("ix_generation_tasks_edit_plan_id", table_name="generation_tasks")
    op.drop_column("generation_tasks", "edit_plan_id")
    op.drop_table("edit_plan_clips")
    op.drop_table("edit_plans")
    op.drop_table("edit_templates")
