"""Add discount to CheckoutLink

Revision ID: bed939e666c6
Revises: 6fee3df6376c
Create Date: 2024-11-20 13:59:40.666946

"""

import sqlalchemy as sa
from alembic import op

# Polar Custom Imports

# revision identifiers, used by Alembic.
revision = "bed939e666c6"
down_revision = "6fee3df6376c"
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(
        "checkout_links",
        sa.Column("allow_discount_codes", sa.Boolean(), nullable=True),
    )
    op.execute(
        "UPDATE checkout_links SET allow_discount_codes = TRUE WHERE allow_discount_codes IS NULL"
    )
    op.alter_column("checkout_links", "allow_discount_codes", nullable=False)

    op.add_column("checkout_links", sa.Column("discount_id", sa.Uuid(), nullable=True))
    op.drop_constraint(
        "checkout_links_product_price_id_fkey", "checkout_links", type_="foreignkey"
    )
    op.create_foreign_key(
        op.f("checkout_links_product_price_id_fkey"),
        "checkout_links",
        "product_prices",
        ["product_price_id"],
        ["id"],
        ondelete="set null",
    )
    op.create_foreign_key(
        op.f("checkout_links_discount_id_fkey"),
        "checkout_links",
        "discounts",
        ["discount_id"],
        ["id"],
        ondelete="set null",
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(
        op.f("checkout_links_discount_id_fkey"), "checkout_links", type_="foreignkey"
    )
    op.drop_constraint(
        op.f("checkout_links_product_price_id_fkey"),
        "checkout_links",
        type_="foreignkey",
    )
    op.create_foreign_key(
        "checkout_links_product_price_id_fkey",
        "checkout_links",
        "product_prices",
        ["product_price_id"],
        ["id"],
        ondelete="CASCADE",
    )
    op.drop_column("checkout_links", "discount_id")
    op.drop_column("checkout_links", "allow_discount_codes")
    # ### end Alembic commands ###
