"""Add model_identifier to Agente model

Revision ID: 09304430a911
Revises: 0260553121e7
Create Date: 2025-06-06 15:47:49.260398

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

# revision identifiers, used by Alembic.
revision = '09304430a911'
down_revision = '0260553121e7'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('agente', schema=None) as batch_op:
        batch_op.alter_column('nome',
               existing_type=mysql.VARCHAR(length=100),
               type_=sa.String(length=120),
               existing_nullable=False)
        batch_op.alter_column('model_identifier',
               existing_type=mysql.VARCHAR(length=100),
               type_=sa.String(length=120),
               nullable=True)
        batch_op.drop_constraint(batch_op.f('agente_ibfk_1'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'login', ['user_id'], ['id'], ondelete='CASCADE')
        batch_op.drop_column('top_p')
        batch_op.drop_column('frequency_penalty')

    with op.batch_alter_table('agente_treinos', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('agente_treinos_ibfk_1'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'agente', ['agente_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('chat_messages', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('chat_messages_ibfk_1'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'chats', ['chat_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('chats', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('chats_ibfk_2'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('chats_ibfk_1'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'agente', ['agent_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(None, 'login', ['user_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('login', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('email'))
        batch_op.drop_column('email')

    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('login', schema=None) as batch_op:
        batch_op.add_column(sa.Column('email', mysql.VARCHAR(length=255), nullable=False))
        batch_op.create_index(batch_op.f('email'), ['email'], unique=True)

    with op.batch_alter_table('chats', schema=None) as batch_op:
        batch_op.drop_constraint(None, type_='foreignkey')
        batch_op.drop_constraint(None, type_='foreignkey')
        batch_op.create_foreign_key(batch_op.f('chats_ibfk_1'), 'agente', ['agent_id'], ['id'])
        batch_op.create_foreign_key(batch_op.f('chats_ibfk_2'), 'login', ['user_id'], ['id'])

    with op.batch_alter_table('chat_messages', schema=None) as batch_op:
        batch_op.drop_constraint(None, type_='foreignkey')
        batch_op.create_foreign_key(batch_op.f('chat_messages_ibfk_1'), 'chats', ['chat_id'], ['id'])

    with op.batch_alter_table('agente_treinos', schema=None) as batch_op:
        batch_op.drop_constraint(None, type_='foreignkey')
        batch_op.create_foreign_key(batch_op.f('agente_treinos_ibfk_1'), 'agente', ['agente_id'], ['id'])

    with op.batch_alter_table('agente', schema=None) as batch_op:
        batch_op.add_column(sa.Column('frequency_penalty', mysql.FLOAT(), nullable=True))
        batch_op.add_column(sa.Column('top_p', mysql.FLOAT(), nullable=True))
        batch_op.drop_constraint(None, type_='foreignkey')
        batch_op.create_foreign_key(batch_op.f('agente_ibfk_1'), 'login', ['user_id'], ['id'])
        batch_op.alter_column('model_identifier',
               existing_type=sa.String(length=120),
               type_=mysql.VARCHAR(length=100),
               nullable=False)
        batch_op.alter_column('nome',
               existing_type=sa.String(length=120),
               type_=mysql.VARCHAR(length=100),
               existing_nullable=False)

    # ### end Alembic commands ###
