"""Add Wallet

Revision ID: edf952c2129a
Revises: e87a34881c93
Create Date: 2025-10-28 10:41:59.296057

"""

import sqlalchemy as sa
from alembic import op

# Polar Custom Imports

# revision identifiers, used by Alembic.
revision = "edf952c2129a"
down_revision = "e87a34881c93"
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(
        "wallets",
        sa.Column("currency", sa.String(length=3), nullable=False),
        sa.Column("customer_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(
            ["customer_id"],
            ["customers.id"],
            name=op.f("wallets_customer_id_fkey"),
            ondelete="cascade",
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("wallets_pkey")),
        sa.UniqueConstraint("customer_id", name=op.f("wallets_customer_id_key")),
    )
    op.create_index(
        op.f("ix_wallets_created_at"), "wallets", ["created_at"], unique=False
    )
    op.create_index(
        op.f("ix_wallets_deleted_at"), "wallets", ["deleted_at"], unique=False
    )
    op.create_table(
        "wallet_transactions",
        sa.Column("type", sa.String(), nullable=False),
        sa.Column("timestamp", sa.TIMESTAMP(timezone=True), nullable=False),
        sa.Column("currency", sa.String(length=3), nullable=False),
        sa.Column("amount", sa.BigInteger(), nullable=False),
        sa.Column("wallet_id", sa.Uuid(), nullable=False),
        sa.Column("tax_amount", sa.BigInteger(), nullable=True),
        sa.Column("tax_calculation_processor_id", sa.String(), nullable=True),
        sa.Column("order_id", sa.Uuid(), nullable=True),
        sa.Column("refund_id", sa.Uuid(), nullable=True),
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.ForeignKeyConstraint(
            ["order_id"],
            ["orders.id"],
            name=op.f("wallet_transactions_order_id_fkey"),
            ondelete="restrict",
        ),
        sa.ForeignKeyConstraint(
            ["refund_id"],
            ["refunds.id"],
            name=op.f("wallet_transactions_refund_id_fkey"),
            ondelete="restrict",
        ),
        sa.ForeignKeyConstraint(
            ["wallet_id"],
            ["wallets.id"],
            name=op.f("wallet_transactions_wallet_id_fkey"),
            ondelete="restrict",
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("wallet_transactions_pkey")),
    )
    op.create_index(
        op.f("ix_wallet_transactions_order_id"),
        "wallet_transactions",
        ["order_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_wallet_transactions_refund_id"),
        "wallet_transactions",
        ["refund_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_wallet_transactions_timestamp"),
        "wallet_transactions",
        ["timestamp"],
        unique=False,
    )
    op.create_index(
        op.f("ix_wallet_transactions_wallet_id"),
        "wallet_transactions",
        ["wallet_id"],
        unique=False,
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(
        op.f("ix_wallet_transactions_wallet_id"), table_name="wallet_transactions"
    )
    op.drop_index(
        op.f("ix_wallet_transactions_timestamp"), table_name="wallet_transactions"
    )
    op.drop_index(
        op.f("ix_wallet_transactions_refund_id"), table_name="wallet_transactions"
    )
    op.drop_index(
        op.f("ix_wallet_transactions_order_id"), table_name="wallet_transactions"
    )
    op.drop_table("wallet_transactions")
    op.drop_index(op.f("ix_wallets_deleted_at"), table_name="wallets")
    op.drop_index(op.f("ix_wallets_created_at"), table_name="wallets")
    op.drop_table("wallets")
    # ### end Alembic commands ###
