"""Add Discount models

Revision ID: 6fee3df6376c
Revises: 2996c0ed1512
Create Date: 2024-11-18 16:02:59.335411

"""

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql

# Polar Custom Imports

# revision identifiers, used by Alembic.
revision = "6fee3df6376c"
down_revision = "2996c0ed1512"
branch_labels: tuple[str] | None = None
depends_on: tuple[str] | None = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "discounts",
        sa.Column("name", postgresql.CITEXT(), nullable=False),
        sa.Column("type", sa.String(), nullable=False),
        sa.Column("code", sa.String(), nullable=True),
        sa.Column("starts_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("ends_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("max_redemptions", sa.Integer(), nullable=True),
        sa.Column("duration", sa.String(), nullable=False),
        sa.Column("duration_in_months", sa.Integer(), nullable=True),
        sa.Column("stripe_coupon_id", sa.String(), nullable=False),
        sa.Column("organization_id", sa.Uuid(), nullable=False),
        sa.Column(
            "user_metadata", postgresql.JSONB(astext_type=sa.Text()), nullable=False
        ),
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("created_at", sa.TIMESTAMP(timezone=True), nullable=False),
        sa.Column("modified_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("deleted_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("amount", sa.Integer(), nullable=True),
        sa.Column("currency", sa.String(length=3), nullable=True),
        sa.Column("basis_points", sa.Integer(), nullable=True),
        sa.ForeignKeyConstraint(
            ["organization_id"],
            ["organizations.id"],
            name=op.f("discounts_organization_id_fkey"),
            ondelete="cascade",
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("discounts_pkey")),
        sa.UniqueConstraint(
            "organization_id", "code", name=op.f("discounts_organization_id_code_key")
        ),
    )
    op.create_index(op.f("ix_discounts_code"), "discounts", ["code"], unique=False)
    op.create_index(
        op.f("ix_discounts_created_at"), "discounts", ["created_at"], unique=False
    )
    op.create_index(
        op.f("ix_discounts_deleted_at"), "discounts", ["deleted_at"], unique=False
    )
    op.create_index(
        op.f("ix_discounts_modified_at"), "discounts", ["modified_at"], unique=False
    )
    op.create_index(
        op.f("ix_discounts_stripe_coupon_id"),
        "discounts",
        ["stripe_coupon_id"],
        unique=True,
    )
    op.create_table(
        "discount_products",
        sa.Column("discount_id", sa.Uuid(), nullable=False),
        sa.Column("product_id", sa.Uuid(), nullable=False),
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("created_at", sa.TIMESTAMP(timezone=True), nullable=False),
        sa.Column("modified_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("deleted_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.ForeignKeyConstraint(
            ["discount_id"],
            ["discounts.id"],
            name=op.f("discount_products_discount_id_fkey"),
            ondelete="cascade",
        ),
        sa.ForeignKeyConstraint(
            ["product_id"],
            ["products.id"],
            name=op.f("discount_products_product_id_fkey"),
            ondelete="cascade",
        ),
        sa.PrimaryKeyConstraint(
            "discount_id", "product_id", "id", name=op.f("discount_products_pkey")
        ),
    )
    op.create_index(
        op.f("ix_discount_products_created_at"),
        "discount_products",
        ["created_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_discount_products_deleted_at"),
        "discount_products",
        ["deleted_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_discount_products_modified_at"),
        "discount_products",
        ["modified_at"],
        unique=False,
    )
    op.create_table(
        "discount_redemptions",
        sa.Column("discount_id", sa.Uuid(), nullable=False),
        sa.Column("checkout_id", sa.Uuid(), nullable=False),
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("created_at", sa.TIMESTAMP(timezone=True), nullable=False),
        sa.Column("modified_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("deleted_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.ForeignKeyConstraint(
            ["checkout_id"],
            ["checkouts.id"],
            name=op.f("discount_redemptions_checkout_id_fkey"),
            ondelete="cascade",
        ),
        sa.ForeignKeyConstraint(
            ["discount_id"],
            ["discounts.id"],
            name=op.f("discount_redemptions_discount_id_fkey"),
            ondelete="cascade",
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("discount_redemptions_pkey")),
    )
    op.create_index(
        op.f("ix_discount_redemptions_created_at"),
        "discount_redemptions",
        ["created_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_discount_redemptions_deleted_at"),
        "discount_redemptions",
        ["deleted_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_discount_redemptions_modified_at"),
        "discount_redemptions",
        ["modified_at"],
        unique=False,
    )

    op.add_column(
        "checkouts", sa.Column("allow_discount_codes", sa.Boolean(), nullable=True)
    )
    op.execute(
        "UPDATE checkouts SET allow_discount_codes = TRUE WHERE allow_discount_codes IS NULL"
    )
    op.alter_column("checkouts", "allow_discount_codes", nullable=False)

    op.add_column("checkouts", sa.Column("discount_id", sa.Uuid(), nullable=True))
    op.create_foreign_key(
        op.f("checkouts_discount_id_fkey"),
        "checkouts",
        "discounts",
        ["discount_id"],
        ["id"],
        ondelete="set null",
    )
    op.add_column("orders", sa.Column("discount_id", sa.Uuid(), nullable=True))
    op.create_foreign_key(
        op.f("orders_discount_id_fkey"),
        "orders",
        "discounts",
        ["discount_id"],
        ["id"],
        ondelete="set null",
    )
    op.add_column("subscriptions", sa.Column("discount_id", sa.Uuid(), nullable=True))
    op.create_foreign_key(
        op.f("subscriptions_discount_id_fkey"),
        "subscriptions",
        "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("subscriptions_discount_id_fkey"), "subscriptions", type_="foreignkey"
    )
    op.drop_column("subscriptions", "discount_id")
    op.drop_constraint(op.f("orders_discount_id_fkey"), "orders", type_="foreignkey")
    op.drop_column("orders", "discount_id")
    op.drop_constraint(
        op.f("checkouts_discount_id_fkey"), "checkouts", type_="foreignkey"
    )
    op.drop_column("checkouts", "discount_id")
    op.drop_column("checkouts", "allow_discount_codes")
    op.drop_index(
        op.f("ix_discount_redemptions_modified_at"), table_name="discount_redemptions"
    )
    op.drop_index(
        op.f("ix_discount_redemptions_deleted_at"), table_name="discount_redemptions"
    )
    op.drop_index(
        op.f("ix_discount_redemptions_created_at"), table_name="discount_redemptions"
    )
    op.drop_table("discount_redemptions")
    op.drop_index(
        op.f("ix_discount_products_modified_at"), table_name="discount_products"
    )
    op.drop_index(
        op.f("ix_discount_products_deleted_at"), table_name="discount_products"
    )
    op.drop_index(
        op.f("ix_discount_products_created_at"), table_name="discount_products"
    )
    op.drop_table("discount_products")
    op.drop_index(op.f("ix_discounts_stripe_coupon_id"), table_name="discounts")
    op.drop_index(op.f("ix_discounts_modified_at"), table_name="discounts")
    op.drop_index(op.f("ix_discounts_deleted_at"), table_name="discounts")
    op.drop_index(op.f("ix_discounts_created_at"), table_name="discounts")
    op.drop_index(op.f("ix_discounts_code"), table_name="discounts")
    op.drop_table("discounts")
    # ### end Alembic commands ###
