"""add token_events table

Revision ID: o2j3k4l5m6n7
Revises: n1i2j3k4l5m6
Create Date: 2026-03-29

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects.postgresql import UUID

revision = "o2j3k4l5m6n7"
down_revision = "n1i2j3k4l5m6"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "token_events",
        sa.Column("id", UUID(as_uuid=True), primary_key=True, server_default=sa.text("gen_random_uuid()")),
        sa.Column("task_id", UUID(as_uuid=True), sa.ForeignKey("tasks.id", ondelete="SET NULL"), nullable=True),
        sa.Column("workstream_id", UUID(as_uuid=True), sa.ForeignKey("workstreams.id", ondelete="SET NULL"), nullable=True),
        sa.Column("repo_id", UUID(as_uuid=True), sa.ForeignKey("managed_repos.id", ondelete="SET NULL"), nullable=True),
        sa.Column("session_id", sa.Text(), nullable=True),
        sa.Column("model", sa.Text(), nullable=True),
        sa.Column("tokens_in", sa.Integer(), nullable=False),
        sa.Column("tokens_out", sa.Integer(), nullable=False),
        sa.Column("agent", sa.Text(), nullable=True),
        sa.Column("ref_type", sa.Text(), nullable=True),
        sa.Column("ref_id", sa.Text(), nullable=True),
        sa.Column("note", sa.Text(), nullable=True),
        sa.Column("created_at", sa.TIMESTAMP(timezone=True), server_default=sa.text("now()"), nullable=False),
    )
    op.create_index("ix_token_events_task_id", "token_events", ["task_id"])
    op.create_index("ix_token_events_workstream_id", "token_events", ["workstream_id"])
    op.create_index("ix_token_events_repo_id", "token_events", ["repo_id"])
    op.create_index("ix_token_events_created_at", "token_events", ["created_at"])


def downgrade() -> None:
    op.drop_index("ix_token_events_created_at", table_name="token_events")
    op.drop_index("ix_token_events_repo_id", table_name="token_events")
    op.drop_index("ix_token_events_workstream_id", table_name="token_events")
    op.drop_index("ix_token_events_task_id", table_name="token_events")
    op.drop_table("token_events")
