"""quota_events: per-spend audit log for per-user quota attribution

Revision ID: 0009_quota_events
Revises: 0008_invites
Create Date: 2026-06-12
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa

revision: str = "0009_quota_events"
down_revision: Union[str, None] = "0008_invites"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.create_table(
        "quota_events",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column(
            "user_id",
            sa.Integer(),
            sa.ForeignKey("users.id", ondelete="SET NULL"),
            nullable=True,
        ),
        sa.Column("action", sa.String(length=40), nullable=False),
        sa.Column("units", sa.Integer(), nullable=False),
        sa.Column(
            "created_at",
            sa.DateTime(timezone=True),
            server_default=sa.func.now(),
            nullable=False,
        ),
    )
    op.create_index("ix_quota_events_user_id", "quota_events", ["user_id"])
    op.create_index("ix_quota_events_action", "quota_events", ["action"])
    op.create_index("ix_quota_events_created_at", "quota_events", ["created_at"])


def downgrade() -> None:
    op.drop_index("ix_quota_events_created_at", table_name="quota_events")
    op.drop_index("ix_quota_events_action", table_name="quota_events")
    op.drop_index("ix_quota_events_user_id", table_name="quota_events")
    op.drop_table("quota_events")
