"""user role

Revision ID: 15cc32aebd22
Revises: 2681575163c0
Create Date: 2025-07-11 06:30:16.251203

"""

from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = "15cc32aebd22"
down_revision = "2681575163c0"
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table("user_info", schema=None) as batch_op:
        batch_op.add_column(
            sa.Column(
                "is_admin",
                sa.Boolean(),
                nullable=False,
                server_default="0",
                comment="is admin",
            )
        )
        batch_op.add_column(
            sa.Column(
                "is_creator",
                sa.Boolean(),
                nullable=False,
                server_default="0",
                comment="is creator",
            )
        )
    # ### end Alembic commands ###

    conn = op.get_bind()
    result = conn.execute(
        sa.text("SELECT created_user_id FROM ai_course GROUP BY created_user_id")
    )
    creator_user_ids = [row[0] for row in result if row[0]]
    if creator_user_ids:
        update_sql = sa.text(
            "UPDATE user_info SET is_creator = 1 WHERE user_id IN :user_ids"
        )
        conn.execute(update_sql, {"user_ids": tuple(creator_user_ids)})


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table("user_info", schema=None) as batch_op:
        batch_op.drop_column("is_creator")
        batch_op.drop_column("is_admin")
    # ### end Alembic commands ###
