"""Add token-usage, model, and cost columns to user_activity.

Adds four nullable columns so the admin Activity view can show the LLM
model used for each request, the prompt/completion token counts, and the
computed per-request cost in USD:

- ``llm_model``          : short model identifier from supported_llms
  (e.g. ``gpt-4.1-mini``).
- ``prompt_tokens``      : input tokens consumed by the LLM call.
- ``completion_tokens``  : output tokens generated by the LLM call.
- ``cost_usd``           : computed cost in USD with 6 decimals so
  sub-cent values stay visible.

All columns are nullable with no defaults — historical rows render as
"—" in the UI and no backfill is needed.

Revision ID: 0026_add_token_usage
Revises: 0025_merge_0024_heads
Create Date: 2026-05-28

Note: the revision ID is intentionally kept under 32 characters to fit
the stock ``alembic_version.version_num`` column type (``varchar(32)``).
"""

import sqlalchemy as sa

from alembic import op  # type: ignore[attr-defined]

revision = "0026_add_token_usage"
down_revision = "0025_merge_0024_heads"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.add_column(
        "user_activity",
        sa.Column("llm_model", sa.String(length=128), nullable=True),
    )
    op.add_column(
        "user_activity",
        sa.Column("prompt_tokens", sa.Integer(), nullable=True),
    )
    op.add_column(
        "user_activity",
        sa.Column("completion_tokens", sa.Integer(), nullable=True),
    )
    op.add_column(
        "user_activity",
        sa.Column("cost_usd", sa.Numeric(precision=12, scale=6), nullable=True),
    )


def downgrade() -> None:
    op.drop_column("user_activity", "cost_usd")
    op.drop_column("user_activity", "completion_tokens")
    op.drop_column("user_activity", "prompt_tokens")
    op.drop_column("user_activity", "llm_model")
