"""quick experience-environment: Add system embedding model and llm model, Adjust user

Revision ID: 26f51e9528f1
Revises: 9fa66e01df94
Create Date: 2026-02-03 15:45:58.499129

"""
from typing import Sequence, Union

from alembic import op
from openjiuwen_studio.core.database.migration_utils import table_exists, column_exists, index_exists
import sqlalchemy as sa
from sqlalchemy.dialects import mysql
from sqlalchemy import inspect

# revision identifiers, used by Alembic.
revision: str = '26f51e9528f1'
down_revision: Union[str, Sequence[str], None] = '9fa66e01df94'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    """Upgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    if not table_exists('system_embedding_model'):
        op.create_table('system_embedding_model',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('model_name', sa.String(length=100), nullable=False),
        sa.Column('protocol', sa.String(length=50), nullable=False),
        sa.Column('model_id', sa.String(length=100), nullable=False),
        sa.Column('api_key', sa.Text(), nullable=True),
        sa.Column('api_base', sa.String(length=500), nullable=False),
        sa.Column('max_batch_size', sa.Integer(), nullable=False),
        sa.Column('is_active', sa.Boolean(), server_default='1', nullable=True),
        sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
        sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
        sa.PrimaryKeyConstraint('id'),
        sa.UniqueConstraint('model_name')
        )

    if not index_exists('system_embedding_model', 'ix_system_embedding_model_id'):
        op.create_index(op.f('ix_system_embedding_model_id'), 'system_embedding_model', ['id'], unique=False)
    if not index_exists('system_embedding_model',
                      'ix_system_embedding_model_is_active'):
        op.create_index(op.f('ix_system_embedding_model_is_active'),
                        'system_embedding_model', ['is_active'], unique=False)
    if not index_exists('system_embedding_model',
                      'ix_system_embedding_model_protocol'):
        op.create_index(op.f('ix_system_embedding_model_protocol'),
                        'system_embedding_model', ['protocol'], unique=False)
    
    if not table_exists('system_llm_model'):
        op.create_table('system_llm_model',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('name', sa.String(length=100), nullable=False),
        sa.Column('provider', sa.String(length=50), nullable=False),
        sa.Column('model_type', sa.String(length=100), nullable=False),
        sa.Column('description', sa.Text(), nullable=True),
        sa.Column('api_key', sa.Text(), nullable=True),
        sa.Column('base_url', sa.String(length=500), nullable=True),
        sa.Column('is_active', sa.Boolean(), server_default='1', nullable=True),
        sa.Column('parameters', sa.JSON(), nullable=True),
        sa.Column('timeout', sa.Integer(), nullable=True),
        sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
        sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
        sa.PrimaryKeyConstraint('id'),
        sa.UniqueConstraint('name')
        )
    
    if not index_exists('system_llm_model', 'ix_system_llm_model_id'):
        op.create_index(op.f('ix_system_llm_model_id'), 'system_llm_model', ['id'], unique=False)
    if not index_exists('system_llm_model', 'ix_system_llm_model_is_active'):
        op.create_index(op.f('ix_system_llm_model_is_active'), 'system_llm_model', ['is_active'], unique=False)
    if not index_exists('system_llm_model', 'ix_system_llm_model_provider'):
        op.create_index(op.f('ix_system_llm_model_provider'), 'system_llm_model', ['provider'], unique=False)
    
    if not column_exists('embedding_model_configs', 'is_system_model'):
        op.add_column('embedding_model_configs', sa.Column('is_system_model', sa.Boolean(),
                                                           server_default='0', nullable=True))
    if not column_exists('embedding_model_configs', 'system_model_id'):
        op.add_column('embedding_model_configs', sa.Column('system_model_id', sa.Integer(), nullable=True))
    
    if not index_exists('embedding_model_configs', 'ix_embedding_model_configs_system_model_id'):
        op.create_index(op.f('ix_embedding_model_configs_system_model_id'), 'embedding_model_configs',
                        ['system_model_id'], unique=False)
    
    if not column_exists('model_configs', 'is_system_model'):
        op.add_column('model_configs', sa.Column('is_system_model', sa.Boolean(), server_default='0', nullable=True))
    if not column_exists('model_configs', 'system_model_id'):
        op.add_column('model_configs', sa.Column('system_model_id', sa.Integer(), nullable=True))
    
    if not index_exists('model_configs', 'ix_model_configs_system_model_id'):
        op.create_index(op.f('ix_model_configs_system_model_id'), 'model_configs', ['system_model_id'], unique=False)
    
    if not column_exists('user', 'refresh_token'):
        op.add_column('user', sa.Column('refresh_token', sa.String(length=512), server_default='', nullable=True))
    
    if index_exists('user', 'idx_session_key'):
        op.drop_index('idx_session_key', table_name='user')
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    if not index_exists('user', 'idx_session_key'):
        op.create_index('idx_session_key', 'user', ['session_key'], unique=True)
    
    if column_exists('user', 'refresh_token'):
        op.drop_column('user', 'refresh_token')
    
    if index_exists('model_configs', 'ix_model_configs_system_model_id'):
        op.drop_index(op.f('ix_model_configs_system_model_id'), table_name='model_configs')
    
    if column_exists('model_configs', 'system_model_id'):
        op.drop_column('model_configs', 'system_model_id')
    if column_exists('model_configs', 'is_system_model'):
        op.drop_column('model_configs', 'is_system_model')
    
    if index_exists('embedding_model_configs', 'ix_embedding_model_configs_system_model_id'):
        op.drop_index(op.f('ix_embedding_model_configs_system_model_id'), table_name='embedding_model_configs')
    
    if column_exists('embedding_model_configs', 'system_model_id'):
        op.drop_column('embedding_model_configs', 'system_model_id')
    if column_exists('embedding_model_configs', 'is_system_model'):
        op.drop_column('embedding_model_configs', 'is_system_model')
    
    if index_exists('system_llm_model', 'ix_system_llm_model_provider'):
        op.drop_index(op.f('ix_system_llm_model_provider'), table_name='system_llm_model')
    if index_exists('system_llm_model', 'ix_system_llm_model_is_active'):
        op.drop_index(op.f('ix_system_llm_model_is_active'), table_name='system_llm_model')
    if index_exists('system_llm_model', 'ix_system_llm_model_id'):
        op.drop_index(op.f('ix_system_llm_model_id'), table_name='system_llm_model')
    
    if table_exists('system_llm_model'):
        op.drop_table('system_llm_model')
    
    if index_exists('system_embedding_model', 'ix_system_embedding_model_protocol'):
        op.drop_index(op.f('ix_system_embedding_model_protocol'), table_name='system_embedding_model')
    if index_exists('system_embedding_model', 'ix_system_embedding_model_is_active'):
        op.drop_index(op.f('ix_system_embedding_model_is_active'), table_name='system_embedding_model')
    if index_exists('system_embedding_model', 'ix_system_embedding_model_id'):
        op.drop_index(op.f('ix_system_embedding_model_id'), table_name='system_embedding_model')
    
    if table_exists('system_embedding_model'):
        op.drop_table('system_embedding_model')
    # ### end Alembic commands ###
