"""rename_rfpqna_to_rfp_document_question

Revision ID: 3f3276aaf0c4
Revises: b86ade02428a
Create Date: 2026-03-01 07:55:42.393782

"""
from typing import Sequence, Union

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

# revision identifiers, used by Alembic.
revision: str = '3f3276aaf0c4'
down_revision: Union[str, Sequence[str], None] = 'b86ade02428a'
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! ###
    op.create_table('rfp_document_question',
    sa.Column('rfp_document_id', sa.Integer(), nullable=False),
    sa.Column('uuid', sa.UUID(), nullable=False),
    sa.Column('question', sa.Text(), nullable=False),
    sa.Column('question_type', sa.String(length=255), nullable=True),
    sa.Column('relevant_department', sa.String(length=255), nullable=True),
    sa.Column('parent_question_id', sa.Integer(), nullable=True),
    sa.Column('workbook_name', sa.String(length=255), nullable=True),
    sa.Column('answer', sa.Text(), nullable=True),
    sa.Column('human_response', sa.Text(), nullable=True),
    sa.Column('confidence', sa.String(length=50), nullable=True),
    sa.Column('citations', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
    sa.Column('raw_llm_response', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.Column('deleted_at', sa.DateTime(timezone=True), nullable=True),
    sa.ForeignKeyConstraint(['parent_question_id'], ['rfp_document_question.id'], name=op.f('rfp_document_question_parent_question_id_fkey'), ondelete='SET NULL'),
    sa.ForeignKeyConstraint(['rfp_document_id'], ['rfp_document.id'], name=op.f('rfp_document_question_rfp_document_id_fkey'), ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id', name=op.f('rfp_document_question_pkey'))
    )
    op.create_index(op.f('rfp_document_question_id_idx'), 'rfp_document_question', ['id'], unique=False)
    op.create_index(op.f('rfp_document_question_rfp_document_id_idx'), 'rfp_document_question', ['rfp_document_id'], unique=False)
    op.create_index(op.f('rfp_document_question_uuid_idx'), 'rfp_document_question', ['uuid'], unique=True)
    op.drop_index(op.f('rfpqna_id_idx'), table_name='rfpqna')
    op.drop_index(op.f('rfpqna_rfp_document_id_idx'), table_name='rfpqna')
    op.drop_index(op.f('rfpqna_uuid_idx'), table_name='rfpqna')
    op.drop_table('rfpqna')
    op.alter_column('psap_boundaries', 'created_at',
               existing_type=postgresql.TIMESTAMP(timezone=True),
               type_=sa.DateTime(),
               existing_nullable=False,
               existing_server_default=sa.text('now()'))
    op.create_index(op.f('rfp_profile_reference_id_idx'), 'rfp_profile_reference', ['id'], unique=False)
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f('rfp_profile_reference_id_idx'), table_name='rfp_profile_reference')
    op.alter_column('psap_boundaries', 'created_at',
               existing_type=sa.DateTime(),
               type_=postgresql.TIMESTAMP(timezone=True),
               existing_nullable=False,
               existing_server_default=sa.text('now()'))
    op.create_table('rfpqna',
    sa.Column('question', sa.TEXT(), autoincrement=False, nullable=False),
    sa.Column('answer', sa.TEXT(), autoincrement=False, nullable=True),
    sa.Column('confidence', sa.VARCHAR(length=50), autoincrement=False, nullable=True),
    sa.Column('citations', postgresql.JSONB(astext_type=sa.Text()), autoincrement=False, nullable=True),
    sa.Column('raw_llm_response', postgresql.JSONB(astext_type=sa.Text()), autoincrement=False, nullable=True),
    sa.Column('id', sa.INTEGER(), 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.Column('human_response', sa.TEXT(), autoincrement=False, nullable=True),
    sa.Column('workbook_name', sa.VARCHAR(length=255), autoincrement=False, nullable=True),
    sa.Column('uuid', sa.UUID(), autoincrement=False, nullable=False),
    sa.Column('question_type', sa.VARCHAR(length=255), autoincrement=False, nullable=True),
    sa.Column('relevant_department', sa.VARCHAR(length=255), autoincrement=False, nullable=True),
    sa.Column('parent_question_id', sa.INTEGER(), autoincrement=False, nullable=True),
    sa.Column('rfp_document_id', sa.INTEGER(), autoincrement=False, nullable=False),
    sa.ForeignKeyConstraint(['parent_question_id'], ['rfpqna.id'], name=op.f('rfpqna_parent_question_id_fkey'), ondelete='SET NULL'),
    sa.ForeignKeyConstraint(['rfp_document_id'], ['rfp_document.id'], name=op.f('rfpqna_rfp_document_id_fkey'), ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id', name=op.f('rfpqna_pkey'))
    )
    op.create_index(op.f('rfpqna_uuid_idx'), 'rfpqna', ['uuid'], unique=True)
    op.create_index(op.f('rfpqna_rfp_document_id_idx'), 'rfpqna', ['rfp_document_id'], unique=False)
    op.create_index(op.f('rfpqna_id_idx'), 'rfpqna', ['id'], unique=False)
    op.drop_index(op.f('rfp_document_question_uuid_idx'), table_name='rfp_document_question')
    op.drop_index(op.f('rfp_document_question_rfp_document_id_idx'), table_name='rfp_document_question')
    op.drop_index(op.f('rfp_document_question_id_idx'), table_name='rfp_document_question')
    op.drop_table('rfp_document_question')
    # ### end Alembic commands ###
