"""Add Product.source and ProductPrice.checkout_product_id

Revision ID: 6f4085630fe9
Revises: 16322c272e89
Create Date: 2025-11-17 20:42:24.555836

"""

import sqlalchemy as sa
from alembic import op

# Polar Custom Imports

# revision identifiers, used by Alembic.
revision = "6f4085630fe9"
down_revision = "16322c272e89"
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(
        "product_prices",
        sa.Column("source", sa.String(), nullable=True),
    )
    op.execute("UPDATE product_prices SET source = 'catalog' WHERE source IS NULL")
    op.alter_column(
        "product_prices",
        "source",
        existing_type=sa.String(),
        nullable=False,
    )

    op.add_column(
        "product_prices", sa.Column("checkout_product_id", sa.Uuid(), nullable=True)
    )
    op.create_index(
        op.f("ix_product_prices_checkout_product_id"),
        "product_prices",
        ["checkout_product_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_product_prices_source"), "product_prices", ["source"], unique=False
    )
    op.create_foreign_key(
        op.f("product_prices_checkout_product_id_fkey"),
        "product_prices",
        "checkout_products",
        ["checkout_product_id"],
        ["id"],
        ondelete="set null",
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(
        op.f("product_prices_checkout_product_id_fkey"),
        "product_prices",
        type_="foreignkey",
    )
    op.drop_index(op.f("ix_product_prices_source"), table_name="product_prices")
    op.drop_index(
        op.f("ix_product_prices_checkout_product_id"), table_name="product_prices"
    )
    op.drop_column("product_prices", "checkout_product_id")
    op.drop_column("product_prices", "source")
    # ### end Alembic commands ###
