"""replace long-term growth profiling with recent practice review

Revision ID: 0025_recent_practice_review
Revises: 0024_topic_settlement_jobs
"""

from __future__ import annotations

from alembic import op
import sqlalchemy as sa


revision = "0025_recent_practice_review"
down_revision = "0024_topic_settlement_jobs"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.add_column(
        "sys_topic_summary",
        sa.Column("schema_version", sa.Integer(), nullable=False, server_default="1"),
    )
    op.alter_column("sys_topic_summary", "schema_version", server_default="2")

    op.add_column(
        "sys_user_growth_profile",
        sa.Column("schema_version", sa.Integer(), nullable=False, server_default="1"),
    )
    op.add_column("sys_user_growth_profile", sa.Column("recent_review", sa.Text(), nullable=True))
    op.add_column("sys_user_growth_profile", sa.Column("current_focus", sa.Text(), nullable=True))
    op.add_column("sys_user_growth_profile", sa.Column("source_summary_ids", sa.Text(), nullable=True))
    op.alter_column("sys_user_growth_profile", "schema_version", server_default="2")

    op.add_column(
        "sys_periodic_report",
        sa.Column("schema_version", sa.Integer(), nullable=False, server_default="1"),
    )
    op.alter_column("sys_periodic_report", "schema_version", server_default="2")

    # 仅替换系统初始深度版的旧说明，避免覆盖管理员已经自定义的文案。
    op.execute(
        sa.text(
            "UPDATE sys_entitlement_plan "
            "SET description = :new_description "
            "WHERE plan_type = 'deep' AND description = :old_description"
        ).bindparams(
            new_description="支持近期实修回顾、周期实修回顾和更高主题会话额度。",
            old_description="支持长期成长档案、阶段报告和更高主题会话额度。",
        )
    )


def downgrade() -> None:
    op.execute(
        sa.text(
            "UPDATE sys_entitlement_plan "
            "SET description = :old_description "
            "WHERE plan_type = 'deep' AND description = :new_description"
        ).bindparams(
            old_description="支持长期成长档案、阶段报告和更高主题会话额度。",
            new_description="支持近期实修回顾、周期实修回顾和更高主题会话额度。",
        )
    )
    op.drop_column("sys_periodic_report", "schema_version")
    op.drop_column("sys_user_growth_profile", "source_summary_ids")
    op.drop_column("sys_user_growth_profile", "current_focus")
    op.drop_column("sys_user_growth_profile", "recent_review")
    op.drop_column("sys_user_growth_profile", "schema_version")
    op.drop_column("sys_topic_summary", "schema_version")
