"""add alert definitions

Revision ID: 0002_alert_definitions
Revises: 0001_init
Create Date: 2026-02-12
"""

from alembic import op
import sqlalchemy as sa


revision = "0002_alert_definitions"
down_revision = "0001_init"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "alert_definitions",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("name", sa.String(length=160), nullable=False),
        sa.Column("description", sa.Text(), nullable=True),
        sa.Column("target_id", sa.Integer(), sa.ForeignKey("targets.id", ondelete="CASCADE"), nullable=True),
        sa.Column("sql_text", sa.Text(), nullable=False),
        sa.Column("comparison", sa.String(length=10), nullable=False, server_default="gte"),
        sa.Column("warning_threshold", sa.Float(), nullable=True),
        sa.Column("alert_threshold", sa.Float(), nullable=False),
        sa.Column("enabled", sa.Boolean(), nullable=False, server_default=sa.text("true")),
        sa.Column("created_by_user_id", sa.Integer(), sa.ForeignKey("users.id"), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
    )
    op.create_index("ix_alert_definitions_target_id", "alert_definitions", ["target_id"])
    op.create_index("ix_alert_definitions_created_by_user_id", "alert_definitions", ["created_by_user_id"])
    op.create_index("ix_alert_definitions_created_at", "alert_definitions", ["created_at"])


def downgrade() -> None:
    op.drop_index("ix_alert_definitions_created_at", table_name="alert_definitions")
    op.drop_index("ix_alert_definitions_created_by_user_id", table_name="alert_definitions")
    op.drop_index("ix_alert_definitions_target_id", table_name="alert_definitions")
    op.drop_table("alert_definitions")
