"""account token

Revision ID: 37606c5a9b9e
Revises: 
Create Date: 2025-01-15 09:57:58.619279

"""
from typing import Sequence, Union

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

# revision identifiers, used by Alembic.
revision: str = '37606c5a9b9e'
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('flow')
    op.drop_table('dialog')
    op.drop_table('apps')
    op.drop_table('user_tenant')
    op.drop_table('knowledgebase')
    op.drop_index('session_id_ix', table_name='chat_sessions')
    op.drop_index('tenant_id_ix', table_name='chat_sessions')
    op.drop_index('update_tine_ix', table_name='chat_sessions')
    op.drop_table('chat_sessions')
    op.alter_column('chat_api_tokens', 'app_id',
               existing_type=mysql.VARCHAR(charset='utf8mb3', collation='utf8mb3_general_ci', length=36),
               nullable=True)
    op.alter_column('chat_api_tokens', 'type',
               existing_type=mysql.VARCHAR(charset='utf8mb3', collation='utf8mb3_general_ci', length=16),
               nullable=True)
    op.drop_index('app_id_ix', table_name='chat_api_tokens')
    op.create_index(op.f('ix_chat_api_tokens_app_id'), 'chat_api_tokens', ['app_id'], unique=False)
    op.drop_index('update_ix_index', table_name='conversation')
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_index('update_ix_index', 'conversation', ['update_time'], unique=False)
    op.drop_index(op.f('ix_chat_api_tokens_app_id'), table_name='chat_api_tokens')
    op.create_index('app_id_ix', 'chat_api_tokens', ['app_id'], unique=False)
    op.alter_column('chat_api_tokens', 'type',
               existing_type=mysql.VARCHAR(charset='utf8mb3', collation='utf8mb3_general_ci', length=16),
               nullable=False)
    op.alter_column('chat_api_tokens', 'app_id',
               existing_type=mysql.VARCHAR(charset='utf8mb3', collation='utf8mb3_general_ci', length=36),
               nullable=False)
    op.create_table('chat_sessions',
    sa.Column('id', mysql.VARCHAR(charset='utf8mb3', collation='utf8mb3_general_ci', length=36), nullable=False),
    sa.Column('name', mysql.VARCHAR(charset='utf8mb3', collation='utf8mb3_general_ci', length=255), nullable=True),
    sa.Column('agent_id', mysql.VARCHAR(charset='utf8mb3', collation='utf8mb3_general_ci', length=255), nullable=True),
    sa.Column('agent_type', mysql.INTEGER(), autoincrement=False, nullable=True),
    sa.Column('create_date', mysql.DATETIME(), nullable=True),
    sa.Column('update_date', mysql.DATETIME(), nullable=True),
    sa.Column('tenant_id', mysql.INTEGER(), autoincrement=False, nullable=True),
    sa.Column('message', mysql.LONGTEXT(charset='utf8mb3', collation='utf8mb3_general_ci'), nullable=True),
    sa.Column('reference', mysql.LONGTEXT(charset='utf8mb3', collation='utf8mb3_general_ci'), nullable=True),
    sa.Column('conversation_id', mysql.VARCHAR(charset='utf8mb3', collation='utf8mb3_general_ci', length=36), nullable=True),
    sa.Column('event_type', mysql.VARCHAR(charset='utf8mb3', collation='utf8mb3_general_ci', length=16), nullable=True),
    sa.Column('session_type', mysql.VARCHAR(charset='utf8mb3', collation='utf8mb3_general_ci', length=16), nullable=True),
    sa.PrimaryKeyConstraint('id'),
    mysql_default_charset='utf8mb3',
    mysql_engine='InnoDB',
    mysql_row_format='DYNAMIC'
    )
    op.create_index('update_tine_ix', 'chat_sessions', ['update_date'], unique=False)
    op.create_index('tenant_id_ix', 'chat_sessions', ['tenant_id'], unique=False)
    op.create_index('session_id_ix', 'chat_sessions', ['conversation_id'], unique=False)
    op.create_table('knowledgebase',
    sa.Column('id', mysql.VARCHAR(length=36), nullable=False),
    sa.Column('name', mysql.VARCHAR(length=128), nullable=True),
    sa.Column('create_date', mysql.DATETIME(), nullable=True),
    sa.Column('update_date', mysql.DATETIME(), nullable=True),
    sa.Column('tenant_id', mysql.VARCHAR(length=32), nullable=True),
    sa.Column('description', mysql.TEXT(), nullable=True),
    sa.Column('status', mysql.VARCHAR(length=1), nullable=True),
    sa.Column('documents', mysql.INTEGER(), autoincrement=False, nullable=True),
    sa.Column('icon', mysql.VARCHAR(length=128), nullable=True),
    sa.Column('knowledge_type', mysql.VARCHAR(length=1), nullable=True),
    sa.Column('permission', mysql.VARCHAR(length=32), nullable=True),
    sa.PrimaryKeyConstraint('id'),
    mysql_collate='utf8mb4_0900_ai_ci',
    mysql_default_charset='utf8mb4',
    mysql_engine='InnoDB'
    )
    op.create_table('user_tenant',
    sa.Column('id', mysql.VARCHAR(length=36), nullable=False),
    sa.Column('tenant_id', mysql.VARCHAR(length=32), nullable=True),
    sa.Column('user_id', mysql.VARCHAR(length=32), nullable=True),
    sa.Column('role', mysql.VARCHAR(length=32), nullable=True),
    sa.PrimaryKeyConstraint('id'),
    mysql_collate='utf8mb4_0900_ai_ci',
    mysql_default_charset='utf8mb4',
    mysql_engine='InnoDB'
    )
    op.create_table('apps',
    sa.Column('id', mysql.VARCHAR(length=36), nullable=False),
    sa.Column('name', mysql.VARCHAR(length=255), nullable=False),
    sa.Column('status', mysql.VARCHAR(length=16), nullable=False),
    sa.Column('description', mysql.TEXT(), nullable=False),
    sa.Column('tenant_id', mysql.VARCHAR(length=36), nullable=False),
    sa.PrimaryKeyConstraint('id'),
    mysql_collate='utf8mb4_0900_ai_ci',
    mysql_default_charset='utf8mb4',
    mysql_engine='InnoDB'
    )
    op.create_table('dialog',
    sa.Column('id', mysql.VARCHAR(length=255), nullable=False),
    sa.Column('name', mysql.VARCHAR(length=255), nullable=False),
    sa.Column('status', mysql.VARCHAR(length=1), nullable=False),
    sa.Column('description', mysql.VARCHAR(length=255), nullable=False),
    sa.Column('tenant_id', mysql.VARCHAR(length=36), nullable=False),
    sa.PrimaryKeyConstraint('id'),
    mysql_collate='utf8mb4_0900_ai_ci',
    mysql_default_charset='utf8mb4',
    mysql_engine='InnoDB'
    )
    op.create_table('flow',
    sa.Column('id', mysql.VARCHAR(length=255), nullable=False),
    sa.Column('name', mysql.VARCHAR(length=255), nullable=False),
    sa.Column('status', mysql.INTEGER(), autoincrement=False, nullable=False),
    sa.Column('description', mysql.VARCHAR(length=255), nullable=False),
    sa.Column('user_id', mysql.INTEGER(), autoincrement=False, nullable=False),
    sa.PrimaryKeyConstraint('id'),
    mysql_collate='utf8mb4_0900_ai_ci',
    mysql_default_charset='utf8mb4',
    mysql_engine='InnoDB'
    )
    # ### end Alembic commands ###
