"""initial_schema

Revision ID: b88b5d952ac3
Revises: 
Create Date: 2025-11-26 21:15:54.386887

"""
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 = 'b88b5d952ac3'
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('api_keys',
    sa.Column('id', sa.UUID(), nullable=False),
    sa.Column('key_hash', sa.String(length=255), nullable=False),
    sa.Column('name', sa.String(length=255), nullable=True),
    sa.Column('rate_limit_per_minute', sa.Integer(), nullable=False),
    sa.Column('is_active', sa.Boolean(), nullable=False),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.Column('last_used_at', sa.DateTime(timezone=True), nullable=True),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('key_hash')
    )
    op.create_table('documents',
    sa.Column('id', sa.UUID(), nullable=False),
    sa.Column('filename', sa.String(length=255), nullable=False),
    sa.Column('file_path', sa.String(length=500), nullable=False),
    sa.Column('file_size_bytes', sa.Integer(), nullable=True),
    sa.Column('status', sa.String(length=50), nullable=False),
    sa.Column('error_message', sa.Text(), nullable=True),
    sa.Column('sheet_count', sa.Integer(), nullable=True),
    sa.Column('record_count', sa.Integer(), nullable=True),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.Column('processed_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('api_key_id', sa.UUID(), nullable=True),
    sa.ForeignKeyConstraint(['api_key_id'], ['api_keys.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index('idx_documents_api_key_id', 'documents', ['api_key_id'], unique=False)
    op.create_index('idx_documents_status', 'documents', ['status'], unique=False)
    op.create_table('query_logs',
    sa.Column('id', sa.UUID(), nullable=False),
    sa.Column('api_key_id', sa.UUID(), nullable=True),
    sa.Column('query_text', sa.Text(), nullable=False),
    sa.Column('answer_text', sa.Text(), nullable=True),
    sa.Column('confidence', sa.Float(), nullable=True),
    sa.Column('sources', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
    sa.Column('processing_time_ms', sa.Integer(), nullable=True),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.ForeignKeyConstraint(['api_key_id'], ['api_keys.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index('idx_query_logs_sources', 'query_logs', ['sources'], unique=False, postgresql_using='gin')
    op.create_table('extracted_records',
    sa.Column('id', sa.UUID(), nullable=False),
    sa.Column('document_id', sa.UUID(), nullable=False),
    sa.Column('sheet_name', sa.String(length=255), nullable=False),
    sa.Column('table_title', sa.String(length=255), nullable=True),
    sa.Column('row_number', sa.Integer(), nullable=False),
    sa.Column('col_range', sa.String(length=50), nullable=True),
    sa.Column('content', postgresql.JSONB(astext_type=sa.Text()), nullable=False),
    sa.Column('resolved_content', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
    sa.Column('headers', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.ForeignKeyConstraint(['document_id'], ['documents.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index('idx_extracted_records_content', 'extracted_records', ['content'], unique=False, postgresql_using='gin')
    op.create_index('idx_extracted_records_document_id', 'extracted_records', ['document_id'], unique=False)
    op.create_index('idx_extracted_records_resolved_content', 'extracted_records', ['resolved_content'], unique=False, postgresql_using='gin')
    op.create_index('idx_extracted_records_sheet_name', 'extracted_records', ['sheet_name'], unique=False)
    op.create_table('symbol_dictionaries',
    sa.Column('id', sa.UUID(), nullable=False),
    sa.Column('document_id', sa.UUID(), nullable=False),
    sa.Column('sheet_name', sa.String(length=255), nullable=True),
    sa.Column('symbol', sa.String(length=50), nullable=False),
    sa.Column('meaning', sa.Text(), nullable=False),
    sa.Column('context', sa.String(length=255), nullable=True),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.ForeignKeyConstraint(['document_id'], ['documents.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index('idx_symbol_dictionaries_document_id', 'symbol_dictionaries', ['document_id'], unique=False)
    op.create_index('idx_symbol_dictionaries_symbol', 'symbol_dictionaries', ['symbol'], unique=False)
    op.create_table('cross_references',
    sa.Column('id', sa.UUID(), nullable=False),
    sa.Column('source_record_id', sa.UUID(), nullable=False),
    sa.Column('target_record_id', sa.UUID(), nullable=True),
    sa.Column('reference_text', sa.Text(), nullable=False),
    sa.Column('reference_type', sa.String(length=50), nullable=True),
    sa.Column('resolved', sa.Boolean(), nullable=False),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.ForeignKeyConstraint(['source_record_id'], ['extracted_records.id'], ondelete='CASCADE'),
    sa.ForeignKeyConstraint(['target_record_id'], ['extracted_records.id'], ondelete='SET NULL'),
    sa.PrimaryKeyConstraint('id')
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('cross_references')
    op.drop_index('idx_symbol_dictionaries_symbol', table_name='symbol_dictionaries')
    op.drop_index('idx_symbol_dictionaries_document_id', table_name='symbol_dictionaries')
    op.drop_table('symbol_dictionaries')
    op.drop_index('idx_extracted_records_sheet_name', table_name='extracted_records')
    op.drop_index('idx_extracted_records_resolved_content', table_name='extracted_records', postgresql_using='gin')
    op.drop_index('idx_extracted_records_document_id', table_name='extracted_records')
    op.drop_index('idx_extracted_records_content', table_name='extracted_records', postgresql_using='gin')
    op.drop_table('extracted_records')
    op.drop_index('idx_query_logs_sources', table_name='query_logs', postgresql_using='gin')
    op.drop_table('query_logs')
    op.drop_index('idx_documents_status', table_name='documents')
    op.drop_index('idx_documents_api_key_id', table_name='documents')
    op.drop_table('documents')
    op.drop_table('api_keys')
    # ### end Alembic commands ###
