"""Remove WebhookNotification

Revision ID: 806a7aac6884
Revises: 3981cdd5db56
Create Date: 2024-08-14 10:12:08.345782

"""

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql

# Polar Custom Imports

# revision identifiers, used by Alembic.
revision = "806a7aac6884"
down_revision = "3981cdd5db56"
branch_labels: tuple[str] | None = None
depends_on: tuple[str] | None = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.execute(
        """
        INSERT INTO webhook_endpoints (id, created_at, modified_at, url, format, secret, organization_id, user_id, events)
        SELECT
            uuid_generate_v4(),
            created_at,
            NOW(),
            url,
            integration,
            replace(uuid_generate_v4()::text, '-', ''),
            organization_id,
            NULL,
            to_jsonb(ARRAY['pledge.created', 'subscription.created', 'donation.created'])
        FROM webhook_notifications
        WHERE deleted_at IS NULL
        """
    )

    op.drop_index(
        "ix_webhook_notifications_created_at", table_name="webhook_notifications"
    )
    op.drop_index(
        "ix_webhook_notifications_deleted_at", table_name="webhook_notifications"
    )
    op.drop_index(
        "ix_webhook_notifications_modified_at", table_name="webhook_notifications"
    )
    op.drop_index(
        "ix_webhook_notifications_organization_id", table_name="webhook_notifications"
    )
    op.drop_table("webhook_notifications")
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "webhook_notifications",
        sa.Column("integration", sa.VARCHAR(), autoincrement=False, nullable=False),
        sa.Column("organization_id", sa.UUID(), autoincrement=False, nullable=False),
        sa.Column("url", sa.VARCHAR(), autoincrement=False, nullable=False),
        sa.Column("id", sa.UUID(), autoincrement=False, nullable=False),
        sa.Column(
            "created_at",
            postgresql.TIMESTAMP(timezone=True),
            autoincrement=False,
            nullable=False,
        ),
        sa.Column(
            "modified_at",
            postgresql.TIMESTAMP(timezone=True),
            autoincrement=False,
            nullable=True,
        ),
        sa.Column(
            "deleted_at",
            postgresql.TIMESTAMP(timezone=True),
            autoincrement=False,
            nullable=True,
        ),
        sa.ForeignKeyConstraint(
            ["organization_id"],
            ["organizations.id"],
            name="webhook_notifications_organization_id_fkey",
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint("id", name="webhook_notifications_pkey"),
    )
    op.create_index(
        "ix_webhook_notifications_organization_id",
        "webhook_notifications",
        ["organization_id"],
        unique=False,
    )
    op.create_index(
        "ix_webhook_notifications_modified_at",
        "webhook_notifications",
        ["modified_at"],
        unique=False,
    )
    op.create_index(
        "ix_webhook_notifications_deleted_at",
        "webhook_notifications",
        ["deleted_at"],
        unique=False,
    )
    op.create_index(
        "ix_webhook_notifications_created_at",
        "webhook_notifications",
        ["created_at"],
        unique=False,
    )

    # ### end Alembic commands ###
