"""feat: update courese model

Revision ID: 766207a9c1b1
Revises: c5672bafb160
Create Date: 2025-03-05 05:15:44.228276

"""

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql

# revision identifiers, used by Alembic.
revision = "766207a9c1b1"
down_revision = "c5672bafb160"
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "resource",
        sa.Column("id", mysql.BIGINT(), autoincrement=True, nullable=False),
        sa.Column(
            "resource_id", sa.String(length=36), nullable=False, comment="Resource UUID"
        ),
        sa.Column(
            "name", sa.String(length=255), nullable=False, comment="Resource name"
        ),
        sa.Column("type", sa.Integer(), nullable=False, comment="Resource type"),
        sa.Column(
            "oss_bucket", sa.String(length=255), nullable=False, comment="OSS bucket"
        ),
        sa.Column(
            "oss_name", sa.String(length=255), nullable=False, comment="OSS name"
        ),
        sa.Column("url", sa.String(length=255), nullable=False, comment="Resource URL"),
        sa.Column("status", sa.Integer(), nullable=False, comment="Resource status"),
        sa.Column("is_deleted", sa.Integer(), nullable=False, comment="Is deleted"),
        sa.Column(
            "created_by", sa.String(length=36), nullable=False, comment="Created by"
        ),
        sa.Column(
            "updated_by", sa.String(length=36), nullable=False, comment="Updated by"
        ),
        sa.Column(
            "created_at", sa.TIMESTAMP(), nullable=False, comment="Creation time"
        ),
        sa.Column("updated_at", sa.TIMESTAMP(), nullable=False, comment="Update time"),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "resource_usage",
        sa.Column("id", mysql.BIGINT(), autoincrement=True, nullable=False),
        sa.Column(
            "usage_id", sa.String(length=36), nullable=False, comment="Usage UUID"
        ),
        sa.Column(
            "resource_id", sa.String(length=36), nullable=False, comment="Resource UUID"
        ),
        sa.Column("usage_type", sa.Integer(), nullable=False, comment="Usage type"),
        sa.Column("usage_value", sa.Integer(), nullable=False, comment="Usage value"),
        sa.Column("is_deleted", sa.Integer(), nullable=False, comment="Is deleted"),
        sa.Column(
            "created_by", sa.String(length=36), nullable=False, comment="Created by"
        ),
        sa.Column(
            "updated_by", sa.String(length=36), nullable=False, comment="Updated by"
        ),
        sa.Column(
            "created_at", sa.TIMESTAMP(), nullable=False, comment="Creation time"
        ),
        sa.Column("updated_at", sa.TIMESTAMP(), nullable=False, comment="Update time"),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "scenario_resource",
        sa.Column("id", mysql.BIGINT(), autoincrement=True, nullable=False),
        sa.Column(
            "resource_resource_id",
            sa.String(length=36),
            nullable=False,
            comment="Resource UUID",
        ),
        sa.Column(
            "scenario_id", sa.String(length=36), nullable=False, comment="Scenario UUID"
        ),
        sa.Column(
            "chapter_id", sa.String(length=36), nullable=False, comment="Chapter UUID"
        ),
        sa.Column(
            "resource_type", sa.Integer(), nullable=False, comment="Resource type"
        ),
        sa.Column(
            "resource_id", sa.String(length=36), nullable=False, comment="Resource UUID"
        ),
        sa.Column("is_deleted", sa.Integer(), nullable=False, comment="Is deleted"),
        sa.Column(
            "created_at", sa.TIMESTAMP(), nullable=False, comment="Creation time"
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    with op.batch_alter_table("scenario_resource", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_scenario_resource_chapter_id"), ["chapter_id"], unique=False
        )
        batch_op.create_index(
            batch_op.f("ix_scenario_resource_resource_id"),
            ["resource_id"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_scenario_resource_resource_resource_id"),
            ["resource_resource_id"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_scenario_resource_scenario_id"),
            ["scenario_id"],
            unique=False,
        )

    with op.batch_alter_table("ai_lesson", schema=None) as batch_op:
        batch_op.add_column(
            sa.Column(
                "parent_id",
                sa.String(length=36),
                nullable=True,
                comment="Parent lesson UUID",
            )
        )
        batch_op.create_index(
            batch_op.f("ix_ai_lesson_parent_id"), ["parent_id"], unique=False
        )

    with op.batch_alter_table("user_profile", schema=None) as batch_op:
        batch_op.alter_column(
            "profile_type",
            existing_type=mysql.INTEGER(display_width=11),
            comment="0 default, 1 system configuration, 2 user configuration, 3 course configuration",
            existing_nullable=False,
            existing_server_default=sa.text("'0'"),
        )

    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table("user_profile", schema=None) as batch_op:
        batch_op.alter_column(
            "profile_type",
            existing_type=mysql.INTEGER(display_width=11),
            comment=None,
            existing_comment="0 default, 1 system configuration, 2 user configuration, 3 course configuration",
            existing_nullable=False,
            existing_server_default=sa.text("'0'"),
        )

    with op.batch_alter_table("ai_lesson", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_ai_lesson_parent_id"))
        batch_op.drop_column("parent_id")

    with op.batch_alter_table("scenario_resource", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_scenario_resource_scenario_id"))
        batch_op.drop_index(batch_op.f("ix_scenario_resource_resource_resource_id"))
        batch_op.drop_index(batch_op.f("ix_scenario_resource_resource_id"))
        batch_op.drop_index(batch_op.f("ix_scenario_resource_chapter_id"))

    op.drop_table("scenario_resource")
    op.drop_table("resource_usage")
    op.drop_table("resource")
    # ### end Alembic commands ###
