"""feat: remove RAG and tag related tables

Revision ID: 0cbd676b770e
Revises: bd4a3ef686c3
Create Date: 2025-08-18 09:59:07.281606

"""

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

# revision identifiers, used by Alembic.
revision = "0cbd676b770e"
down_revision = "bd4a3ef686c3"
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table("tag")
    op.drop_table("knowledge_chunk")
    op.drop_table("knowledge_file")
    op.drop_table("knowledge_base")
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "knowledge_base",
        sa.Column("id", mysql.BIGINT(), autoincrement=True, nullable=False),
        sa.Column(
            "kb_id",
            mysql.VARCHAR(collation="utf8mb4_unicode_ci", length=36),
            nullable=False,
            comment="Knowledge Base ID",
        ),
        sa.Column(
            "kb_name",
            mysql.VARCHAR(collation="utf8mb4_unicode_ci", length=255),
            nullable=False,
            comment="Knowledge Base name",
        ),
        sa.Column(
            "kb_description",
            mysql.TEXT(collation="utf8mb4_unicode_ci"),
            nullable=True,
            comment="Knowledge Base description",
        ),
        sa.Column(
            "embedding_model",
            mysql.VARCHAR(collation="utf8mb4_unicode_ci", length=255),
            nullable=False,
            comment="Embedding Model name",
        ),
        sa.Column(
            "dim",
            mysql.INTEGER(),
            autoincrement=False,
            nullable=False,
            comment="VectorDB dim",
        ),
        sa.Column(
            "tag_ids",
            mysql.TEXT(collation="utf8mb4_unicode_ci"),
            nullable=True,
            comment="Knowledge Base Tag IDs, separated by ','",
        ),
        sa.Column(
            "course_ids",
            mysql.TEXT(collation="utf8mb4_unicode_ci"),
            nullable=True,
            comment="Course UUIDs, separated by ','",
        ),
        sa.Column(
            "meta_data",
            mysql.TEXT(collation="utf8mb4_unicode_ci"),
            nullable=True,
            comment="Meta Data",
        ),
        sa.Column(
            "extra_data",
            mysql.TEXT(collation="utf8mb4_unicode_ci"),
            nullable=True,
            comment="Extra Data",
        ),
        sa.Column(
            "created_user_id",
            mysql.VARCHAR(collation="utf8mb4_unicode_ci", length=36),
            nullable=True,
            comment="created user ID",
        ),
        sa.Column(
            "updated_user_id",
            mysql.VARCHAR(collation="utf8mb4_unicode_ci", length=36),
            nullable=True,
            comment="updated user ID",
        ),
        sa.Column(
            "created", mysql.TIMESTAMP(), nullable=False, comment="Creation time"
        ),
        sa.Column("updated", mysql.TIMESTAMP(), nullable=False, comment="Update time"),
        sa.PrimaryKeyConstraint("id"),
        mysql_collate="utf8mb4_unicode_ci",
        mysql_default_charset="utf8mb4",
        mysql_engine="InnoDB",
    )
    with op.batch_alter_table("knowledge_base", schema=None) as batch_op:
        batch_op.create_index("ix_knowledge_base_kb_id", ["kb_id"], unique=False)

    op.create_table(
        "knowledge_file",
        sa.Column("id", mysql.BIGINT(), autoincrement=True, nullable=False),
        sa.Column(
            "kb_id",
            mysql.VARCHAR(collation="utf8mb4_unicode_ci", length=36),
            nullable=False,
            comment="Knowledge Base ID",
        ),
        sa.Column(
            "file_tag_id",
            mysql.TEXT(collation="utf8mb4_unicode_ci"),
            nullable=True,
            comment="File Tag ID",
        ),
        sa.Column(
            "file_id",
            mysql.VARCHAR(collation="utf8mb4_unicode_ci", length=36),
            nullable=False,
            comment="File ID",
        ),
        sa.Column(
            "file_key",
            mysql.VARCHAR(collation="utf8mb4_unicode_ci", length=255),
            nullable=False,
            comment="File oss key",
        ),
        sa.Column(
            "file_name",
            mysql.VARCHAR(collation="utf8mb4_unicode_ci", length=255),
            nullable=True,
            comment="File name",
        ),
        sa.Column(
            "file_text",
            mysql.TEXT(collation="utf8mb4_unicode_ci"),
            nullable=True,
            comment="File text",
        ),
        sa.Column(
            "meta_data",
            mysql.TEXT(collation="utf8mb4_unicode_ci"),
            nullable=True,
            comment="Meta Data",
        ),
        sa.Column(
            "extra_data",
            mysql.TEXT(collation="utf8mb4_unicode_ci"),
            nullable=True,
            comment="Extra Data",
        ),
        sa.Column(
            "created_user_id",
            mysql.VARCHAR(collation="utf8mb4_unicode_ci", length=36),
            nullable=True,
            comment="created user ID",
        ),
        sa.Column(
            "updated_user_id",
            mysql.VARCHAR(collation="utf8mb4_unicode_ci", length=36),
            nullable=True,
            comment="updated user ID",
        ),
        sa.Column(
            "created", mysql.TIMESTAMP(), nullable=False, comment="Creation time"
        ),
        sa.Column("updated", mysql.TIMESTAMP(), nullable=False, comment="Update time"),
        sa.PrimaryKeyConstraint("id"),
        mysql_collate="utf8mb4_unicode_ci",
        mysql_default_charset="utf8mb4",
        mysql_engine="InnoDB",
    )
    with op.batch_alter_table("knowledge_file", schema=None) as batch_op:
        batch_op.create_index("ix_knowledge_file_kb_id", ["kb_id"], unique=False)
        batch_op.create_index("ix_knowledge_file_file_id", ["file_id"], unique=False)

    op.create_table(
        "knowledge_chunk",
        sa.Column("id", mysql.BIGINT(), autoincrement=True, nullable=False),
        sa.Column(
            "kb_id",
            mysql.VARCHAR(collation="utf8mb4_unicode_ci", length=36),
            nullable=False,
            comment="Knowledge Base ID",
        ),
        sa.Column(
            "file_id",
            mysql.VARCHAR(collation="utf8mb4_unicode_ci", length=36),
            nullable=False,
            comment="File ID",
        ),
        sa.Column(
            "chunk_id",
            mysql.VARCHAR(collation="utf8mb4_unicode_ci", length=64),
            nullable=False,
            comment="Chunk ID",
        ),
        sa.Column(
            "chunk_index",
            mysql.INTEGER(),
            autoincrement=False,
            nullable=True,
            comment="Chunk index",
        ),
        sa.Column(
            "chunk_text",
            mysql.TEXT(collation="utf8mb4_unicode_ci"),
            nullable=True,
            comment="Chunk text",
        ),
        sa.Column(
            "chunk_vector",
            mysql.TEXT(collation="utf8mb4_unicode_ci"),
            nullable=True,
            comment="Chunk vector",
        ),
        sa.Column(
            "meta_data",
            mysql.TEXT(collation="utf8mb4_unicode_ci"),
            nullable=True,
            comment="Meta Data",
        ),
        sa.Column(
            "extra_data",
            mysql.TEXT(collation="utf8mb4_unicode_ci"),
            nullable=True,
            comment="Extra Data",
        ),
        sa.Column(
            "created_user_id",
            mysql.VARCHAR(collation="utf8mb4_unicode_ci", length=36),
            nullable=True,
            comment="created user ID",
        ),
        sa.Column(
            "updated_user_id",
            mysql.VARCHAR(collation="utf8mb4_unicode_ci", length=36),
            nullable=True,
            comment="updated user ID",
        ),
        sa.Column(
            "created", mysql.TIMESTAMP(), nullable=False, comment="Creation time"
        ),
        sa.Column("updated", mysql.TIMESTAMP(), nullable=False, comment="Update time"),
        sa.PrimaryKeyConstraint("id"),
        mysql_collate="utf8mb4_unicode_ci",
        mysql_default_charset="utf8mb4",
        mysql_engine="InnoDB",
    )
    with op.batch_alter_table("knowledge_chunk", schema=None) as batch_op:
        batch_op.create_index("ix_knowledge_chunk_kb_id", ["kb_id"], unique=False)
        batch_op.create_index("ix_knowledge_chunk_file_id", ["file_id"], unique=False)
        batch_op.create_index("ix_knowledge_chunk_chunk_id", ["chunk_id"], unique=False)

    op.create_table(
        "tag",
        sa.Column("id", mysql.BIGINT(), autoincrement=True, nullable=False),
        sa.Column(
            "tag_id",
            mysql.VARCHAR(collation="utf8mb4_unicode_ci", length=36),
            nullable=False,
            comment="Tag ID",
        ),
        sa.Column(
            "tag_domain",
            mysql.VARCHAR(collation="utf8mb4_unicode_ci", length=64),
            nullable=False,
            comment="Tag domain",
        ),
        sa.Column(
            "tag_type",
            mysql.VARCHAR(collation="utf8mb4_unicode_ci", length=64),
            nullable=False,
            comment="Tag type",
        ),
        sa.Column(
            "tag_name",
            mysql.VARCHAR(collation="utf8mb4_unicode_ci", length=255),
            nullable=False,
            comment="Tag name",
        ),
        sa.Column(
            "meta_data",
            mysql.TEXT(collation="utf8mb4_unicode_ci"),
            nullable=True,
            comment="Meta Data",
        ),
        sa.Column(
            "extra_data",
            mysql.TEXT(collation="utf8mb4_unicode_ci"),
            nullable=True,
            comment="Extra Data",
        ),
        sa.Column(
            "created_user_id",
            mysql.VARCHAR(collation="utf8mb4_unicode_ci", length=36),
            nullable=True,
            comment="created user ID",
        ),
        sa.Column(
            "updated_user_id",
            mysql.VARCHAR(collation="utf8mb4_unicode_ci", length=36),
            nullable=True,
            comment="updated user ID",
        ),
        sa.Column(
            "created", mysql.TIMESTAMP(), nullable=False, comment="Creation time"
        ),
        sa.Column("updated", mysql.TIMESTAMP(), nullable=False, comment="Update time"),
        sa.PrimaryKeyConstraint("id"),
        mysql_collate="utf8mb4_unicode_ci",
        mysql_default_charset="utf8mb4",
        mysql_engine="InnoDB",
    )
    with op.batch_alter_table("tag", schema=None) as batch_op:
        batch_op.create_index("ix_tag_tag_id", ["tag_id"], unique=False)

    # ### end Alembic commands ###
