"""Add notification tables

Revision ID: ffcf0ed13a25
Revises: 96e9056b8e72
Create Date: 2025-11-06 02:15:23.499766

"""

import sqlalchemy as sa
from alembic import op

from ai.backend.common.data.notification import NotificationChannelType
from ai.backend.manager.models.base import GUID, EnumType

# revision identifiers, used by Alembic.
revision = "ffcf0ed13a25"
down_revision = "96e9056b8e72"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "notification_channels",
        sa.Column(
            "id",
            GUID(),
            server_default=sa.text("uuid_generate_v4()"),
            nullable=False,
        ),
        sa.Column("name", sa.String(length=256), nullable=False),
        sa.Column("description", sa.Text(), nullable=True),
        sa.Column(
            "channel_type",
            EnumType(NotificationChannelType),
            nullable=False,
        ),
        sa.Column("config", sa.JSON(none_as_null=True), nullable=False),
        sa.Column("enabled", sa.Boolean(), nullable=False),
        sa.Column("created_by", GUID(), nullable=False),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_notification_channels")),
    )
    op.create_index(
        op.f("ix_notification_channels_enabled"), "notification_channels", ["enabled"], unique=False
    )
    op.create_table(
        "notification_rules",
        sa.Column(
            "id",
            GUID(),
            server_default=sa.text("uuid_generate_v4()"),
            nullable=False,
        ),
        sa.Column("name", sa.String(length=256), nullable=False),
        sa.Column("description", sa.Text(), nullable=True),
        sa.Column("rule_type", sa.String(length=256), nullable=False),
        sa.Column("channel_id", GUID(), nullable=False),
        sa.Column("message_template", sa.Text(), nullable=False),
        sa.Column("enabled", sa.Boolean(), nullable=False),
        sa.Column("created_by", GUID(), nullable=False),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_notification_rules")),
    )
    op.create_index(
        op.f("ix_notification_rules_enabled"), "notification_rules", ["enabled"], unique=False
    )
    op.create_index(
        op.f("ix_notification_rules_rule_type"), "notification_rules", ["rule_type"], unique=False
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f("ix_notification_rules_rule_type"), table_name="notification_rules")
    op.drop_index(op.f("ix_notification_rules_enabled"), table_name="notification_rules")
    op.drop_table("notification_rules")
    op.drop_index(op.f("ix_notification_channels_enabled"), table_name="notification_channels")
    op.drop_table("notification_channels")
    op.execute("DROP TYPE IF EXISTS notificationchanneltype CASCADE")
    # ### end Alembic commands ###
