"""Add alert model

Revision ID: 1646629c1c90
Revises: 314a5d69646a
Create Date: 2025-08-13 21:08:28.462217

"""

from typing import Sequence, Union

import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "1646629c1c90"
down_revision: Union[str, None] = "314a5d69646a"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "alert",
        sa.Column("detected_ts", sa.DateTime(), nullable=False),
        sa.Column("active_marker", sa.Boolean(), nullable=True),
        sa.Column("resolved_ts", sa.DateTime(), nullable=True),
        sa.Column("details", postgresql.JSON(astext_type=sa.Text()), nullable=True),
        sa.Column("alert_definition_id", sa.Integer(), nullable=True),
        sa.Column("type", sa.SmallInteger(), nullable=True),
        sa.Column("level", sa.SmallInteger(), nullable=True),
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("tenant", sa.UUID(), nullable=True),
        sa.Column("external_id", sa.Text(), nullable=True),
        sa.Column("meter_id", sa.Text(), nullable=True),
        sa.Column("created_ts", sa.DateTime(), nullable=True),
        sa.Column("updated_ts", sa.DateTime(), nullable=True),
        sa.ForeignKeyConstraint(
            ["alert_definition_id"],
            ["alert_definition.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "tenant",
            "meter_id",
            "type",
            "level",
            "active_marker",
            name="unique_active_alert_per_device",
        ),
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table("alert")
    # ### end Alembic commands ###
