"""Add search_log table for query analytics and popular searches.

Revision ID: 013_add_search_log
Revises: 012_multi_source_fmt
"""
from alembic import op
import sqlalchemy as sa

revision = "013_add_search_log"
down_revision = "012_multi_source_fmt"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "search_log",
        sa.Column("id", sa.Integer, primary_key=True, autoincrement=True),
        sa.Column("query", sa.String(500), nullable=False),
        sa.Column("scope", sa.String(50), nullable=False),
        sa.Column("result_count", sa.Integer, nullable=False, server_default="0"),
        sa.Column("created_at", sa.TIMESTAMP(), server_default=sa.func.now(), nullable=False),
    )
    op.create_index("ix_search_log_query", "search_log", ["query"])
    op.create_index("ix_search_log_created_at", "search_log", ["created_at"])


def downgrade() -> None:
    op.drop_index("ix_search_log_created_at", table_name="search_log")
    op.drop_index("ix_search_log_query", table_name="search_log")
    op.drop_table("search_log")
