"""Migrate Notifications

Revision ID: e3554bca67c8
Revises: 59a5d45ae3fd
Create Date: 2025-10-20 16:55:56.483407

"""

import sqlalchemy as sa
from alembic import op

# Polar Custom Imports

# revision identifiers, used by Alembic.
revision = "e3554bca67c8"
down_revision = "59a5d45ae3fd"
branch_labels: tuple[str] | None = None
depends_on: tuple[str] | None = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.alter_column("notifications", "user_id", existing_type=sa.UUID(), nullable=False)
    op.drop_index(op.f("idx_notifications_user_id"), table_name="notifications")
    op.drop_index(op.f("ix_notifications_organization_id"), table_name="notifications")
    op.drop_constraint(
        op.f("notifications_pledge_id_fkey"), "notifications", type_="foreignkey"
    )
    op.create_foreign_key(
        op.f("notifications_user_id_fkey"),
        "notifications",
        "users",
        ["user_id"],
        ["id"],
        ondelete="cascade",
    )
    op.drop_column("notifications", "pledge_id")
    op.drop_column("notifications", "organization_id")
    op.drop_column("notifications", "email_addr")
    with op.get_context().autocommit_block():
        op.create_index(
            op.f("ix_notifications_user_id"),
            "notifications",
            ["user_id"],
            unique=False,
            postgresql_concurrently=True,
        )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column(
        "notifications",
        sa.Column("email_addr", sa.VARCHAR(), autoincrement=False, nullable=True),
    )
    op.add_column(
        "notifications",
        sa.Column("organization_id", sa.UUID(), autoincrement=False, nullable=True),
    )
    op.add_column(
        "notifications",
        sa.Column("pledge_id", sa.UUID(), autoincrement=False, nullable=True),
    )
    op.drop_constraint(
        op.f("notifications_user_id_fkey"), "notifications", type_="foreignkey"
    )
    op.create_foreign_key(
        op.f("notifications_pledge_id_fkey"),
        "notifications",
        "pledges",
        ["pledge_id"],
        ["id"],
    )
    op.drop_index(op.f("ix_notifications_user_id"), table_name="notifications")
    op.create_index(
        op.f("ix_notifications_organization_id"),
        "notifications",
        ["organization_id"],
        unique=False,
    )
    op.create_index(
        op.f("idx_notifications_user_id"), "notifications", ["user_id"], unique=False
    )
    op.alter_column("notifications", "user_id", existing_type=sa.UUID(), nullable=True)
    # ### end Alembic commands ###
