"""add delayed notification feedback and address-scoped suppressions

Revision ID: c8d4f0a91b62
Revises: a23c8e4d91f0
Create Date: 2026-08-16 16:30:00
"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


revision: str = "c8d4f0a91b62"
down_revision: Union[str, Sequence[str], None] = "a23c8e4d91f0"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    with op.batch_alter_table("notification_deliveries") as batch_op:
        batch_op.add_column(
            sa.Column("recipient_address_digest", sa.LargeBinary(length=32), nullable=True)
        )
        batch_op.add_column(
            sa.Column("feedback_status", sa.String(length=32), nullable=True)
        )
        batch_op.add_column(
            sa.Column("feedback_at", sa.DateTime(timezone=True), nullable=True)
        )
        batch_op.create_index(
            "ix_notification_deliveries_provider_message",
            ["provider_message_id"],
            unique=False,
        )
    op.create_table(
        "notification_feedback_events",
        sa.Column("id", sa.String(length=128), nullable=False),
        sa.Column("provider", sa.String(length=64), nullable=False),
        sa.Column("provider_event_id", sa.String(length=256), nullable=False),
        sa.Column("delivery_id", sa.String(length=128), nullable=True),
        sa.Column("delivery_reference_id", sa.String(length=128), nullable=False),
        sa.Column("provider_message_id", sa.String(length=256), nullable=True),
        sa.Column("feedback_type", sa.String(length=32), nullable=False),
        sa.Column("occurred_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("payload_digest", sa.LargeBinary(length=32), nullable=False),
        sa.Column("processed_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column(
            "created_at",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.CheckConstraint(
            "feedback_type IN ('soft_bounce', 'hard_bounce', 'complaint')",
            name="ck_notification_feedback_type",
        ),
        sa.ForeignKeyConstraint(
            ["delivery_id"],
            ["notification_deliveries.id"],
            ondelete="SET NULL",
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "provider",
            "provider_event_id",
            name="uq_notification_feedback_provider_event",
        ),
    )
    op.create_index(
        "ix_notification_feedback_delivery",
        "notification_feedback_events",
        ["delivery_id", "occurred_at"],
        unique=False,
    )
    op.create_table(
        "notification_suppressions",
        sa.Column("id", sa.String(length=128), nullable=False),
        sa.Column("user_id", sa.String(length=128), nullable=False),
        sa.Column("recipient_address_digest", sa.LargeBinary(length=32), nullable=False),
        sa.Column("reason", sa.String(length=32), nullable=False),
        sa.Column("source_feedback_id", sa.String(length=128), nullable=True),
        sa.Column("active", sa.Boolean(), nullable=False),
        sa.Column("suppressed_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("cleared_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column(
            "created_at",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.CheckConstraint(
            "reason IN ('hard_bounce', 'complaint')",
            name="ck_notification_suppression_reason",
        ),
        sa.CheckConstraint(
            "(active = true AND cleared_at IS NULL) OR "
            "(active = false AND cleared_at IS NOT NULL)",
            name="ck_notification_suppression_lifecycle",
        ),
        sa.ForeignKeyConstraint(
            ["source_feedback_id"],
            ["notification_feedback_events.id"],
            ondelete="SET NULL",
        ),
        sa.ForeignKeyConstraint(
            ["user_id"],
            ["users.id"],
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "user_id",
            "recipient_address_digest",
            name="uq_notification_suppression_user_address",
        ),
    )
    op.create_index(
        "ix_notification_suppressions_active",
        "notification_suppressions",
        ["user_id", "active"],
        unique=False,
    )


def downgrade() -> None:
    op.drop_index(
        "ix_notification_suppressions_active",
        table_name="notification_suppressions",
    )
    op.drop_table("notification_suppressions")
    op.drop_index(
        "ix_notification_feedback_delivery",
        table_name="notification_feedback_events",
    )
    op.drop_table("notification_feedback_events")
    with op.batch_alter_table("notification_deliveries") as batch_op:
        batch_op.drop_index("ix_notification_deliveries_provider_message")
        batch_op.drop_column("feedback_at")
        batch_op.drop_column("feedback_status")
        batch_op.drop_column("recipient_address_digest")
