"""Add email_notifications_enabled toggle to uptime_client_subscriptions.

Revision ID: a7b2e9f4c3d8
Revises: f4b0a6f99965
Create Date: 2026-05-03
"""
from __future__ import annotations

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "a7b2e9f4c3d8"
down_revision = "f4b0a6f99965"
branch_labels = None
depends_on = None


def _has_column(bind, table: str, column: str) -> bool:
    inspector = sa.inspect(bind)
    try:
        cols = [c["name"] for c in inspector.get_columns(table)]
    except Exception:
        return False
    return column in cols


def upgrade() -> None:
    bind = op.get_bind()
    if not _has_column(bind, "uptime_client_subscriptions", "email_notifications_enabled"):
        op.add_column(
            "uptime_client_subscriptions",
            sa.Column(
                "email_notifications_enabled",
                sa.Boolean(),
                nullable=False,
                server_default=sa.text("false"),
            ),
        )


def downgrade() -> None:
    bind = op.get_bind()
    if _has_column(bind, "uptime_client_subscriptions", "email_notifications_enabled"):
        op.drop_column("uptime_client_subscriptions", "email_notifications_enabled")