"""Create PaymentMethod

Revision ID: 197161f42f14
Revises: d21ac0812338
Create Date: 2025-06-20 09:52:27.598192

"""

import concurrent.futures
import random
import time
from datetime import UTC, datetime
from typing import TypedDict

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

# Polar Custom Imports
from polar.config import settings
from polar.integrations.stripe.utils import get_expandable_id

# revision identifiers, used by Alembic.
revision = "197161f42f14"
down_revision = "d21ac0812338"
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(
        "payment_methods",
        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("processor", sa.String(), nullable=False),
        sa.Column("processor_id", sa.String(), nullable=False),
        sa.Column("type", sa.String(), nullable=False),
        sa.Column(
            "method_metadata", postgresql.JSONB(astext_type=sa.Text()), nullable=False
        ),
        sa.Column("customer_id", sa.Uuid(), nullable=False),
        sa.ForeignKeyConstraint(
            ["customer_id"],
            ["customers.id"],
            name=op.f("payment_methods_customer_id_fkey"),
            ondelete="cascade",
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("payment_methods_pkey")),
        sa.UniqueConstraint(
            "processor",
            "processor_id",
            "customer_id",
            name=op.f("payment_methods_processor_processor_id_customer_id_key"),
        ),
    )
    op.create_index(
        op.f("ix_payment_methods_created_at"),
        "payment_methods",
        ["created_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_payment_methods_customer_id"),
        "payment_methods",
        ["customer_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_payment_methods_deleted_at"),
        "payment_methods",
        ["deleted_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_payment_methods_type"), "payment_methods", ["type"], unique=False
    )
    op.create_index(
        op.f("ix_payment_methods_modified_at"),
        "payment_methods",
        ["modified_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_payment_methods_processor"),
        "payment_methods",
        ["processor"],
        unique=False,
    )
    op.create_index(
        op.f("ix_payment_methods_processor_id"),
        "payment_methods",
        ["processor_id"],
        unique=False,
    )
    op.add_column(
        "subscriptions", sa.Column("payment_method_id", sa.Uuid(), nullable=True)
    )
    op.create_foreign_key(
        op.f("subscriptions_payment_method_id_fkey"),
        "subscriptions",
        "payment_methods",
        ["payment_method_id"],
        ["id"],
        ondelete="set null",
    )
    op.add_column(
        "customers", sa.Column("default_payment_method_id", sa.Uuid(), nullable=True)
    )
    op.create_foreign_key(
        op.f("customers_default_payment_method_id_fkey"),
        "customers",
        "payment_methods",
        ["default_payment_method_id"],
        ["id"],
        ondelete="set null",
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(
        op.f("customers_default_payment_method_id_fkey"),
        "customers",
        type_="foreignkey",
    )
    op.drop_column("customers", "default_payment_method_id")
    op.drop_constraint(
        op.f("subscriptions_payment_method_id_fkey"),
        "subscriptions",
        type_="foreignkey",
    )
    op.drop_column("subscriptions", "payment_method_id")
    op.drop_index(op.f("ix_payment_methods_processor_id"), table_name="payment_methods")
    op.drop_index(op.f("ix_payment_methods_processor"), table_name="payment_methods")
    op.drop_index(op.f("ix_payment_methods_modified_at"), table_name="payment_methods")
    op.drop_index(op.f("ix_payment_methods_type"), table_name="payment_methods")
    op.drop_index(op.f("ix_payment_methods_deleted_at"), table_name="payment_methods")
    op.drop_index(op.f("ix_payment_methods_customer_id"), table_name="payment_methods")
    op.drop_index(op.f("ix_payment_methods_created_at"), table_name="payment_methods")
    op.drop_table("payment_methods")
    # ### end Alembic commands ###
