"""add topic summaries and growth profiles

Revision ID: 0015_growth_profiles
Revises: 0014_entitlements_topics
"""

from alembic import context, op
import sqlalchemy as sa


revision = "0015_growth_profiles"
down_revision = "0014_entitlements_topics"
branch_labels = None
depends_on = None

PRIMARY_KEY_TYPE = sa.BigInteger().with_variant(sa.Integer(), "sqlite")


def upgrade() -> None:
    inspector = None if context.is_offline_mode() else sa.inspect(op.get_bind())
    tables = set() if inspector is None else set(inspector.get_table_names())

    if "sys_topic_summary" not in tables:
        op.create_table(
            "sys_topic_summary",
            sa.Column("id", PRIMARY_KEY_TYPE, primary_key=True, autoincrement=True),
            sa.Column("topic_session_id", sa.BigInteger(), sa.ForeignKey("sys_topic_session.id"), nullable=False),
            sa.Column("user_id", sa.BigInteger(), sa.ForeignKey("sys_user.id"), nullable=False),
            sa.Column("summary", sa.Text(), nullable=False),
            sa.Column("main_events", sa.Text(), nullable=True),
            sa.Column("emotions", sa.Text(), nullable=True),
            sa.Column("body_feelings", sa.Text(), nullable=True),
            sa.Column("beliefs", sa.Text(), nullable=True),
            sa.Column("recommended_homework", sa.Text(), nullable=True),
            sa.Column("insights", sa.Text(), nullable=True),
            sa.Column("next_observation", sa.Text(), nullable=True),
            sa.Column("model_name", sa.String(100), nullable=True),
            sa.Column("status", sa.String(20), nullable=False, server_default="success"),
            sa.Column("error_message", sa.Text(), nullable=True),
            sa.Column("generated_at", sa.DateTime(), server_default=sa.func.now(), nullable=False),
            sa.Column("created_at", sa.DateTime(), server_default=sa.func.now(), nullable=False),
            sa.Column("updated_at", sa.DateTime(), server_default=sa.func.now(), nullable=False),
            sa.UniqueConstraint("topic_session_id", name="uq_sys_topic_summary_topic"),
        )
        op.create_index("ix_sys_topic_summary_topic_session_id", "sys_topic_summary", ["topic_session_id"])
        op.create_index("ix_sys_topic_summary_user_id", "sys_topic_summary", ["user_id"])
        op.create_index("ix_sys_topic_summary_status", "sys_topic_summary", ["status"])

    if "sys_user_growth_profile" not in tables:
        op.create_table(
            "sys_user_growth_profile",
            sa.Column("id", PRIMARY_KEY_TYPE, primary_key=True, autoincrement=True),
            sa.Column("user_id", sa.BigInteger(), sa.ForeignKey("sys_user.id"), nullable=False),
            sa.Column("profile_text", sa.Text(), nullable=False),
            sa.Column("recurring_topics", sa.Text(), nullable=True),
            sa.Column("common_emotions", sa.Text(), nullable=True),
            sa.Column("body_patterns", sa.Text(), nullable=True),
            sa.Column("relation_patterns", sa.Text(), nullable=True),
            sa.Column("homework_done", sa.Text(), nullable=True),
            sa.Column("effective_homework", sa.Text(), nullable=True),
            sa.Column("recent_progress", sa.Text(), nullable=True),
            sa.Column("last_topic_summary_id", sa.BigInteger(), nullable=True),
            sa.Column("updated_by", sa.String(30), nullable=False, server_default="system"),
            sa.Column("created_at", sa.DateTime(), server_default=sa.func.now(), nullable=False),
            sa.Column("updated_at", sa.DateTime(), server_default=sa.func.now(), nullable=False),
            sa.UniqueConstraint("user_id", name="uq_sys_user_growth_profile_user"),
        )
        op.create_index("ix_sys_user_growth_profile_user_id", "sys_user_growth_profile", ["user_id"])

    if "sys_growth_profile_revision" not in tables:
        op.create_table(
            "sys_growth_profile_revision",
            sa.Column("id", PRIMARY_KEY_TYPE, primary_key=True, autoincrement=True),
            sa.Column("user_id", sa.BigInteger(), sa.ForeignKey("sys_user.id"), nullable=False),
            sa.Column("profile_id", sa.BigInteger(), sa.ForeignKey("sys_user_growth_profile.id"), nullable=False),
            sa.Column("topic_summary_id", sa.BigInteger(), nullable=True),
            sa.Column("before_json", sa.Text(), nullable=True),
            sa.Column("after_json", sa.Text(), nullable=False),
            sa.Column("reason", sa.String(50), nullable=False, server_default="topic_summary"),
            sa.Column("created_at", sa.DateTime(), server_default=sa.func.now(), nullable=False),
        )
        op.create_index("ix_sys_growth_profile_revision_user_id", "sys_growth_profile_revision", ["user_id"])
        op.create_index("ix_sys_growth_profile_revision_profile_id", "sys_growth_profile_revision", ["profile_id"])
        op.create_index("ix_sys_growth_profile_revision_topic_summary_id", "sys_growth_profile_revision", ["topic_summary_id"])


def downgrade() -> None:
    inspector = None if context.is_offline_mode() else sa.inspect(op.get_bind())
    tables = {
        "sys_growth_profile_revision",
        "sys_user_growth_profile",
        "sys_topic_summary",
    } if inspector is None else set(inspector.get_table_names())
    if "sys_growth_profile_revision" in tables:
        op.drop_index("ix_sys_growth_profile_revision_topic_summary_id", table_name="sys_growth_profile_revision")
        op.drop_index("ix_sys_growth_profile_revision_profile_id", table_name="sys_growth_profile_revision")
        op.drop_index("ix_sys_growth_profile_revision_user_id", table_name="sys_growth_profile_revision")
        op.drop_table("sys_growth_profile_revision")
    if "sys_user_growth_profile" in tables:
        op.drop_index("ix_sys_user_growth_profile_user_id", table_name="sys_user_growth_profile")
        op.drop_table("sys_user_growth_profile")
    if "sys_topic_summary" in tables:
        op.drop_index("ix_sys_topic_summary_status", table_name="sys_topic_summary")
        op.drop_index("ix_sys_topic_summary_user_id", table_name="sys_topic_summary")
        op.drop_index("ix_sys_topic_summary_topic_session_id", table_name="sys_topic_summary")
        op.drop_table("sys_topic_summary")
