"""add analytics columns to hr_query_logs

Revision ID: a7b8c9d0e1f2
Revises: f1a2b3c4d5e6
Create Date: 2026-10-06

FR-09 asks for general-vs-HR volume, usage by tenant, conversation counts and
model/service errors. hr_query_logs already carries the per-turn grain those
need, so the columns go here rather than into a second table — a separate
table would mean two copies of every prompt and a dual-write that can
diverge. The reporting view excludes query/response instead.

All columns are nullable with no backfill: rows written before this migration
have no category and should not be guessed at. Reports either filter on
`category IS NOT NULL` or treat the gap as "before analytics".

Additive only. Downgrade drops the six columns and leaves the table as it was.
"""
from alembic import op
import sqlalchemy as sa


revision = "a7b8c9d0e1f2"
down_revision = "f1a2b3c4d5e6"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.add_column("hr_query_logs", sa.Column("category", sa.String(), nullable=True))
    op.add_column("hr_query_logs", sa.Column("true_company_id", sa.Integer(), nullable=True))
    op.add_column("hr_query_logs", sa.Column("conversation_id", sa.String(), nullable=True))
    op.add_column("hr_query_logs", sa.Column("failure_stage", sa.String(), nullable=True))
    op.add_column("hr_query_logs", sa.Column("provider", sa.String(), nullable=True))
    op.add_column("hr_query_logs", sa.Column("model", sa.String(), nullable=True))

    # The three columns every FR-09 report groups or filters by.
    op.create_index(op.f("ix_hr_query_logs_category"), "hr_query_logs", ["category"])
    op.create_index(op.f("ix_hr_query_logs_true_company_id"), "hr_query_logs", ["true_company_id"])
    op.create_index(op.f("ix_hr_query_logs_conversation_id"), "hr_query_logs", ["conversation_id"])


def downgrade() -> None:
    op.drop_index(op.f("ix_hr_query_logs_conversation_id"), table_name="hr_query_logs")
    op.drop_index(op.f("ix_hr_query_logs_true_company_id"), table_name="hr_query_logs")
    op.drop_index(op.f("ix_hr_query_logs_category"), table_name="hr_query_logs")

    op.drop_column("hr_query_logs", "model")
    op.drop_column("hr_query_logs", "provider")
    op.drop_column("hr_query_logs", "failure_stage")
    op.drop_column("hr_query_logs", "conversation_id")
    op.drop_column("hr_query_logs", "true_company_id")
    op.drop_column("hr_query_logs", "category")
