"""Add tags for html embeddings and simplify model usage

Revision ID: ac7753aa8fa3
Revises: ecf1215f61da
Create Date: 2024-05-13 11:17:35.151639

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = 'ac7753aa8fa3'
down_revision = 'ecf1215f61da'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('roles_users', schema=None) as batch_op:
        batch_op.drop_constraint('roles_users_user_id_fkey', type_='foreignkey')
        batch_op.drop_constraint('roles_users_role_id_fkey', type_='foreignkey')
        batch_op.create_foreign_key(None, 'user', ['user_id'], ['id'], referent_schema='public', ondelete='CASCADE')
        batch_op.create_foreign_key(None, 'role', ['role_id'], ['id'], referent_schema='public', ondelete='CASCADE')

    with op.batch_alter_table('tenant', schema=None) as batch_op:
        batch_op.add_column(sa.Column('embedding_model', sa.String(length=50), nullable=True))
        batch_op.add_column(sa.Column('llm_model', sa.String(length=50), nullable=True))
        batch_op.add_column(sa.Column('html_end_tags', postgresql.ARRAY(sa.String(length=10)), nullable=True))
        batch_op.add_column(sa.Column('html_included_elements', postgresql.ARRAY(sa.String(length=50)), nullable=True))
        batch_op.add_column(sa.Column('html_excluded_elements', postgresql.ARRAY(sa.String(length=50)), nullable=True))
        batch_op.drop_column('allowed_embedding_models')
        batch_op.drop_column('default_llm_model')
        batch_op.drop_column('default_embedding_model')
        batch_op.drop_column('allowed_llm_models')

    with op.batch_alter_table('user', schema=None) as batch_op:
        batch_op.drop_constraint('user_tenant_id_fkey', type_='foreignkey')
        batch_op.create_foreign_key(None, 'tenant', ['tenant_id'], ['id'], referent_schema='public')

    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('user', schema=None) as batch_op:
        batch_op.drop_constraint(None, type_='foreignkey')
        batch_op.create_foreign_key('user_tenant_id_fkey', 'tenant', ['tenant_id'], ['id'])

    with op.batch_alter_table('tenant', schema=None) as batch_op:
        batch_op.add_column(sa.Column('allowed_llm_models', postgresql.ARRAY(sa.VARCHAR(length=50)), autoincrement=False, nullable=True))
        batch_op.add_column(sa.Column('default_embedding_model', sa.VARCHAR(length=50), autoincrement=False, nullable=True))
        batch_op.add_column(sa.Column('default_llm_model', sa.VARCHAR(length=50), autoincrement=False, nullable=True))
        batch_op.add_column(sa.Column('allowed_embedding_models', postgresql.ARRAY(sa.VARCHAR(length=50)), autoincrement=False, nullable=True))
        batch_op.drop_column('html_excluded_elements')
        batch_op.drop_column('html_included_elements')
        batch_op.drop_column('html_end_tags')
        batch_op.drop_column('llm_model')
        batch_op.drop_column('embedding_model')

    with op.batch_alter_table('roles_users', schema=None) as batch_op:
        batch_op.drop_constraint(None, type_='foreignkey')
        batch_op.drop_constraint(None, type_='foreignkey')
        batch_op.create_foreign_key('roles_users_role_id_fkey', 'role', ['role_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key('roles_users_user_id_fkey', 'user', ['user_id'], ['id'], ondelete='CASCADE')

    # ### end Alembic commands ###
