"""Add PWYW support to ProductPrice model

Revision ID: be588d8f19ab
Revises: 0e09189cb135
Create Date: 2024-09-16 10:52:52.351200

"""

import sqlalchemy as sa
from alembic import op

# Polar Custom Imports

# revision identifiers, used by Alembic.
revision = "be588d8f19ab"
down_revision = "0e09189cb135"
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("amount_type", sa.String(), nullable=True)
    )
    op.execute(
        """
        UPDATE product_prices
        SET amount_type = 'fixed'
        """
    )
    op.alter_column(
        "product_prices", "amount_type", existing_type=sa.String(), nullable=False
    )
    op.create_index(
        op.f("ix_product_prices_amount_type"),
        "product_prices",
        ["amount_type"],
        unique=False,
    )

    op.add_column(
        "product_prices", sa.Column("minimum_amount", sa.Integer(), nullable=True)
    )
    op.add_column(
        "product_prices", sa.Column("maximum_amount", sa.Integer(), nullable=True)
    )
    op.add_column(
        "product_prices", sa.Column("preset_amount", sa.Integer(), nullable=True)
    )
    op.alter_column(
        "product_prices", "price_amount", existing_type=sa.INTEGER(), nullable=True
    )

    op.add_column("subscriptions", sa.Column("amount", sa.Integer(), nullable=True))
    op.add_column(
        "subscriptions", sa.Column("currency", sa.String(length=3), nullable=True)
    )
    op.add_column(
        "subscriptions", sa.Column("recurring_interval", sa.String(), nullable=True)
    )

    op.execute(
        """
        UPDATE subscriptions
        SET amount = product_prices.price_amount,
            currency = product_prices.price_currency,
            recurring_interval = product_prices.recurring_interval
        FROM product_prices
        WHERE subscriptions.price_id = product_prices.id
        """
    )
    op.execute(
        """
        UPDATE subscriptions
        SET recurring_interval = 'month'
        WHERE recurring_interval IS NULL
        """
    )
    op.alter_column(
        "subscriptions", "recurring_interval", existing_type=sa.String(), nullable=False
    )
    op.create_index(
        op.f("ix_subscriptions_recurring_interval"),
        "subscriptions",
        ["recurring_interval"],
        unique=False,
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(
        op.f("ix_subscriptions_recurring_interval"), table_name="subscriptions"
    )
    op.drop_column("subscriptions", "recurring_interval")
    op.drop_column("subscriptions", "currency")
    op.drop_column("subscriptions", "amount")

    op.execute(
        """
        DELETE FROM orders
        WHERE orders.product_price_id IN (
            SELECT id
            FROM product_prices
            WHERE price_amount IS NULL
        )
        """
    )
    op.execute(
        """
        DELETE FROM product_prices
        WHERE price_amount IS NULL
        """
    )
    op.alter_column(
        "product_prices", "price_amount", existing_type=sa.INTEGER(), nullable=False
    )

    op.drop_index(op.f("ix_product_prices_amount_type"), table_name="product_prices")

    op.drop_column("product_prices", "preset_amount")
    op.drop_column("product_prices", "maximum_amount")
    op.drop_column("product_prices", "minimum_amount")
    op.drop_column("product_prices", "amount_type")
    # ### end Alembic commands ###
