"""Add TrialRedemption

Revision ID: 8511b8b03a3b
Revises: 01ba80f80708
Create Date: 2025-11-10 21:17:48.022173

"""

import sqlalchemy as sa
from alembic import op

# Polar Custom Imports

# revision identifiers, used by Alembic.
revision = "8511b8b03a3b"
down_revision = "01ba80f80708"
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(
        "trial_redemptions",
        sa.Column("customer_email", sa.String(), nullable=False),
        sa.Column("payment_method_fingerprint", sa.String(), nullable=True),
        sa.Column("customer_id", sa.Uuid(), nullable=False),
        sa.Column("product_id", sa.Uuid(), nullable=True),
        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(
            ["customer_id"],
            ["customers.id"],
            name=op.f("trial_redemptions_customer_id_fkey"),
            ondelete="cascade",
        ),
        sa.ForeignKeyConstraint(
            ["product_id"],
            ["products.id"],
            name=op.f("trial_redemptions_product_id_fkey"),
            ondelete="cascade",
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("trial_redemptions_pkey")),
    )
    op.create_index(
        op.f("ix_trial_redemptions_created_at"),
        "trial_redemptions",
        ["created_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_trial_redemptions_customer_email"),
        "trial_redemptions",
        ["customer_email"],
        unique=False,
    )
    op.create_index(
        op.f("ix_trial_redemptions_customer_id"),
        "trial_redemptions",
        ["customer_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_trial_redemptions_deleted_at"),
        "trial_redemptions",
        ["deleted_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_trial_redemptions_payment_method_fingerprint"),
        "trial_redemptions",
        ["payment_method_fingerprint"],
        unique=False,
    )
    op.create_index(
        op.f("ix_trial_redemptions_product_id"),
        "trial_redemptions",
        ["product_id"],
        unique=False,
    )

    op.execute("""
        UPDATE organizations
        SET subscription_settings = subscription_settings || '{"prevent_trial_abuse": false}'::jsonb
        WHERE subscription_settings IS NOT NULL
          AND NOT subscription_settings ? 'prevent_trial_abuse'
    """)
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(
        op.f("ix_trial_redemptions_product_id"), table_name="trial_redemptions"
    )
    op.drop_index(
        op.f("ix_trial_redemptions_payment_method_fingerprint"),
        table_name="trial_redemptions",
    )
    op.drop_index(
        op.f("ix_trial_redemptions_deleted_at"), table_name="trial_redemptions"
    )
    op.drop_index(
        op.f("ix_trial_redemptions_customer_id"), table_name="trial_redemptions"
    )
    op.drop_index(
        op.f("ix_trial_redemptions_customer_email"), table_name="trial_redemptions"
    )
    op.drop_index(
        op.f("ix_trial_redemptions_created_at"), table_name="trial_redemptions"
    )
    op.drop_table("trial_redemptions")
    # ### end Alembic commands ###
