"""finalize search_queries schema with filter and NOT NULL constraints

Revision ID: d1e2f3g4h5i6
Revises: d4e5f6g7h8i9
Create Date: 2025-10-07 11:07:00.000000

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'd1e2f3g4h5i6'
down_revision = 'd4e5f6g7h8i9'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###

    # Add the filter column (new, wasn't in d4e5f6g7h8i9)
    op.add_column('search_queries', sa.Column('filter', sa.JSON(), nullable=True))

    # Set defaults for existing NULL values from d4e5f6g7h8i9
    op.execute("""
        UPDATE search_queries
        SET
            retrieval_strategy = COALESCE(retrieval_strategy, 'hybrid'),
            temporal_relevance = COALESCE(temporal_relevance, 0.3),
            expand_query = COALESCE(expand_query, true),
            interpret_filters = COALESCE(interpret_filters, false),
            rerank = COALESCE(rerank, true),
            generate_answer = COALESCE(generate_answer, false),
            "limit" = COALESCE("limit", 1000),
            "offset" = COALESCE("offset", 0)
    """)

    # Make columns NOT NULL (except filter which is truly optional)
    op.alter_column('search_queries', 'retrieval_strategy',
                    existing_type=sa.String(length=20),
                    nullable=False)
    op.alter_column('search_queries', 'temporal_relevance',
                    existing_type=sa.Float(),
                    nullable=False)
    op.alter_column('search_queries', 'expand_query',
                    existing_type=sa.Boolean(),
                    nullable=False)
    op.alter_column('search_queries', 'interpret_filters',
                    existing_type=sa.Boolean(),
                    nullable=False)
    op.alter_column('search_queries', 'rerank',
                    existing_type=sa.Boolean(),
                    nullable=False)
    op.alter_column('search_queries', 'generate_answer',
                    existing_type=sa.Boolean(),
                    nullable=False)

    # Make limit and offset NOT NULL
    op.alter_column('search_queries', 'limit',
                    existing_type=sa.Integer(),
                    nullable=False)
    op.alter_column('search_queries', 'offset',
                    existing_type=sa.Integer(),
                    nullable=False)
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###

    # Make columns nullable before dropping
    op.alter_column('search_queries', 'offset',
                    existing_type=sa.Integer(),
                    nullable=True)
    op.alter_column('search_queries', 'limit',
                    existing_type=sa.Integer(),
                    nullable=True)
    op.alter_column('search_queries', 'generate_answer',
                    existing_type=sa.Boolean(),
                    nullable=True)
    op.alter_column('search_queries', 'rerank',
                    existing_type=sa.Boolean(),
                    nullable=True)
    op.alter_column('search_queries', 'interpret_filters',
                    existing_type=sa.Boolean(),
                    nullable=True)
    op.alter_column('search_queries', 'expand_query',
                    existing_type=sa.Boolean(),
                    nullable=True)
    op.alter_column('search_queries', 'temporal_relevance',
                    existing_type=sa.Float(),
                    nullable=True)
    op.alter_column('search_queries', 'retrieval_strategy',
                    existing_type=sa.String(length=20),
                    nullable=True)

    # Drop the filter column
    op.drop_column('search_queries', 'filter')
    # ### end Alembic commands ###
