"""Drop Transaction.refund_id and rename Transaction.polar_refund_id

Revision ID: 9cc24f553f30
Revises: f5223877acbb
Create Date: 2025-12-02 11:40:19.904667

"""

import sqlalchemy as sa
from alembic import op

# Polar Custom Imports

# revision identifiers, used by Alembic.
revision = "9cc24f553f30"
down_revision = "f5223877acbb"
branch_labels: tuple[str] | None = None
depends_on: tuple[str] | None = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column("transactions", "refund_id")

    op.alter_column(
        "transactions",
        "polar_refund_id",
        new_column_name="refund_id",
    )
    op.execute(
        "ALTER INDEX ix_transactions_polar_refund_id RENAME TO ix_transactions_refund_id"
    )
    op.execute(
        "ALTER TABLE transactions RENAME CONSTRAINT transactions_polar_refund_id_fkey TO transactions_refund_id_fkey"
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.alter_column(
        "transactions",
        "refund_id",
        new_column_name="polar_refund_id",
    )
    op.execute(
        "ALTER INDEX ix_transactions_refund_id RENAME TO ix_transactions_polar_refund_id"
    )
    op.execute(
        "ALTER TABLE transactions RENAME CONSTRAINT transactions_refund_id_fkey TO transactions_polar_refund_id_fkey"
    )

    op.add_column("transactions", sa.Column("refund_id", sa.String(), nullable=True))
    op.create_index(
        op.f("ix_transactions_refund_id"), "transactions", ["refund_id"], unique=False
    )
    op.execute(
        """
        UPDATE transactions
        SET refund_id = refunds.processor_id
        FROM refunds
        WHERE transactions.polar_refund_id = refunds.id
        """
    )

    # ### end Alembic commands ###
