"""Reverse Refund <> Dispute relationship

Revision ID: c4b8b4d63805
Revises: 8818b5816a78
Create Date: 2025-12-05 09:10:05.781638

"""

import sqlalchemy as sa
from alembic import op

# Polar Custom Imports

# revision identifiers, used by Alembic.
revision = "c4b8b4d63805"
down_revision = "8818b5816a78"
branch_labels: tuple[str] | None = None
depends_on: tuple[str] | None = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column("refunds", sa.Column("dispute_id", sa.Uuid(), nullable=True))
    op.create_foreign_key(
        op.f("refunds_dispute_id_fkey"), "refunds", "disputes", ["dispute_id"], ["id"]
    )

    op.execute(
        """
        UPDATE refunds
        SET dispute_id = disputes.id
        FROM disputes
        WHERE refunds.id = disputes.refund_id
        """
    )

    op.drop_constraint(op.f("disputes_refund_id_fkey"), "disputes", type_="foreignkey")
    op.drop_column("disputes", "refund_id")
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column(
        "disputes",
        sa.Column("refund_id", sa.UUID(), autoincrement=False, nullable=True),
    )
    op.create_foreign_key(
        op.f("disputes_refund_id_fkey"), "disputes", "refunds", ["refund_id"], ["id"]
    )

    op.execute(
        """
        UPDATE disputes
        SET refund_id = refunds.id
        FROM refunds
        WHERE disputes.id = refunds.dispute_id
        """
    )

    op.drop_constraint(op.f("refunds_dispute_id_fkey"), "refunds", type_="foreignkey")
    op.drop_column("refunds", "dispute_id")
    # ### end Alembic commands ###
