"""REMOVED: QA PAIR CHUNK

Revision ID: 4d9291a3acb1
Revises: 6abdd3f001ab
Create Date: 2025-12-17 12:20:30.848462

"""
from typing import Sequence, Union

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

contact_role_enum = sa.Enum('ADMIN', 'ACCOUNT_ADMIN', 'MANAGER', 'VIEWER', name='contactrole')

# revision identifiers, used by Alembic.
revision: str = '4d9291a3acb1'
down_revision: Union[str, Sequence[str], None] = '6abdd3f001ab'
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! ###
    # Drop references to qa_pair before removing the table to satisfy FK constraints.
    op.drop_constraint(op.f('vector_qa_pair_id_key'), 'vector', type_='unique')
    op.drop_constraint(op.f('vector_qa_pair_id_fkey'), 'vector', type_='foreignkey')
    op.drop_column('vector', 'qa_pair_id')

    op.drop_table('qa_pair_chunk_association')
    op.drop_index(op.f('qa_pair_document_id_idx'), table_name='qa_pair')
    op.drop_index(op.f('qa_pair_id_idx'), table_name='qa_pair')
    op.drop_table('qa_pair')
    contact_role_enum.create(op.get_bind(), checkfirst=True)
    op.add_column('contact', sa.Column('contact_role', contact_role_enum, server_default='VIEWER', nullable=False))
    op.add_column('contact', sa.Column('manager_contact_id', sa.Integer(), nullable=True))
    op.create_index(op.f('contact_manager_contact_id_idx'), 'contact', ['manager_contact_id'], unique=False)
    op.create_foreign_key(op.f('contact_manager_contact_id_fkey'), 'contact', 'contact', ['manager_contact_id'], ['id'], ondelete='SET NULL')
    op.drop_index(op.f('idx_document_title_trgm'), table_name='document', postgresql_ops={'title': 'gin_trgm_ops'}, postgresql_using='gin', if_exists=True)
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_index(op.f('idx_document_title_trgm'), 'document', ['title'], unique=False, postgresql_ops={'title': 'gin_trgm_ops'}, postgresql_using='gin')
    op.drop_constraint(op.f('contact_manager_contact_id_fkey'), 'contact', type_='foreignkey')
    op.drop_index(op.f('contact_manager_contact_id_idx'), table_name='contact')
    op.drop_column('contact', 'manager_contact_id')
    op.drop_column('contact', 'contact_role')
    contact_role_enum.drop(op.get_bind(), checkfirst=True)
    op.create_table('qa_pair',
    sa.Column('document_id', sa.INTEGER(), autoincrement=False, nullable=False),
    sa.Column('question', sa.TEXT(), autoincrement=False, nullable=False),
    sa.Column('answer', sa.TEXT(), autoincrement=False, nullable=False),
    sa.Column('provenance', sa.VARCHAR(length=50), autoincrement=False, nullable=False),
    sa.Column('confidence_score', sa.DOUBLE_PRECISION(precision=53), autoincrement=False, nullable=True),
    sa.Column('id', sa.INTEGER(), server_default=sa.text("nextval('qa_pair_id_seq'::regclass)"), autoincrement=True, nullable=False),
    sa.Column('created_at', postgresql.TIMESTAMP(timezone=True), server_default=sa.text('now()'), autoincrement=False, nullable=False),
    sa.Column('updated_at', postgresql.TIMESTAMP(timezone=True), server_default=sa.text('now()'), autoincrement=False, nullable=False),
    sa.Column('deleted_at', postgresql.TIMESTAMP(timezone=True), autoincrement=False, nullable=True),
    sa.ForeignKeyConstraint(['document_id'], ['document.id'], name='qa_pair_document_id_fkey'),
    sa.PrimaryKeyConstraint('id', name='qa_pair_pkey'),
    postgresql_ignore_search_path=False
    )
    op.create_index(op.f('qa_pair_id_idx'), 'qa_pair', ['id'], unique=False)
    op.create_index(op.f('qa_pair_document_id_idx'), 'qa_pair', ['document_id'], unique=False)
    op.create_table('qa_pair_chunk_association',
    sa.Column('qa_pair_id', sa.INTEGER(), autoincrement=False, nullable=False),
    sa.Column('chunk_id', sa.INTEGER(), autoincrement=False, nullable=False),
    sa.ForeignKeyConstraint(['chunk_id'], ['chunk.id'], name=op.f('qa_pair_chunk_association_chunk_id_fkey')),
    sa.ForeignKeyConstraint(['qa_pair_id'], ['qa_pair.id'], name=op.f('qa_pair_chunk_association_qa_pair_id_fkey')),
    sa.PrimaryKeyConstraint('qa_pair_id', 'chunk_id', name=op.f('qa_pair_chunk_association_pkey'))
    )
    op.add_column('vector', sa.Column('qa_pair_id', sa.INTEGER(), autoincrement=False, nullable=True))
    op.create_foreign_key(op.f('vector_qa_pair_id_fkey'), 'vector', 'qa_pair', ['qa_pair_id'], ['id'])
    op.create_unique_constraint(op.f('vector_qa_pair_id_key'), 'vector', ['qa_pair_id'], postgresql_nulls_not_distinct=False)
    # ### end Alembic commands ###
