"""add system_events table for operational warnings/errors

Revision ID: 021
Revises: 020
Create Date: 2026-07-14 00:00:00.000000

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


revision: str = "021"
down_revision: Union[str, None] = "020"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.create_table(
        "system_events",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("severity", sa.String(length=16), nullable=False),
        sa.Column("source", sa.String(length=64), nullable=False),
        sa.Column("code", sa.String(length=64), nullable=False),
        sa.Column("message", sa.Text(), nullable=False),
        sa.Column("symbol", sa.String(length=20), nullable=True),
        sa.Column("dedup_key", sa.String(length=200), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("acknowledged_at", sa.DateTime(timezone=True), nullable=True),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        "ix_system_events_created_at",
        "system_events",
        ["created_at"],
    )
    op.create_index(
        "ix_system_events_ack_created",
        "system_events",
        ["acknowledged_at", "created_at"],
    )
    op.create_index(
        "ix_system_events_dedup_created",
        "system_events",
        ["dedup_key", "created_at"],
    )


def downgrade() -> None:
    op.drop_index("ix_system_events_dedup_created", table_name="system_events")
    op.drop_index("ix_system_events_ack_created", table_name="system_events")
    op.drop_index("ix_system_events_created_at", table_name="system_events")
    op.drop_table("system_events")
