"""Add Payment model

Revision ID: 173ca3810487
Revises: bbe98883e4bd
Create Date: 2025-05-13 09:15:08.984031

"""

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

from polar.kit.extensions.sqlalchemy.types import StrEnumType

# Polar Custom Imports

# revision identifiers, used by Alembic.
revision = "173ca3810487"
down_revision = "bbe98883e4bd"
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(
        "payments",
        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("status", sa.String(), nullable=False),
        sa.Column("amount", sa.Integer(), nullable=False),
        sa.Column("currency", sa.String(length=3), nullable=False),
        sa.Column("method", sa.String(), nullable=False),
        sa.Column(
            "method_metadata", postgresql.JSONB(astext_type=sa.Text()), nullable=False
        ),
        sa.Column("customer_email", sa.String(), nullable=True),
        sa.Column("processor_id", sa.String(), nullable=False),
        sa.Column("decline_reason", sa.String(), nullable=True),
        sa.Column("decline_message", sa.String(), nullable=True),
        sa.Column("risk_level", sa.String(), nullable=True),
        sa.Column("risk_score", sa.SmallInteger(), nullable=True),
        sa.Column("organization_id", sa.Uuid(), nullable=False),
        sa.Column("checkout_id", sa.Uuid(), nullable=True),
        sa.Column("order_id", sa.Uuid(), nullable=True),
        sa.ForeignKeyConstraint(
            ["checkout_id"],
            ["checkouts.id"],
            name=op.f("payments_checkout_id_fkey"),
            ondelete="set null",
        ),
        sa.ForeignKeyConstraint(
            ["order_id"],
            ["orders.id"],
            name=op.f("payments_order_id_fkey"),
            ondelete="set null",
        ),
        sa.ForeignKeyConstraint(
            ["organization_id"],
            ["organizations.id"],
            name=op.f("payments_organization_id_fkey"),
            ondelete="cascade",
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("payments_pkey")),
    )
    op.create_index(
        op.f("ix_payments_checkout_id"), "payments", ["checkout_id"], unique=False
    )
    op.create_index(
        op.f("ix_payments_created_at"), "payments", ["created_at"], unique=False
    )
    op.create_index(
        op.f("ix_payments_deleted_at"), "payments", ["deleted_at"], unique=False
    )
    op.create_index(op.f("ix_payments_method"), "payments", ["method"], unique=False)
    op.create_index(
        op.f("ix_payments_customer_email"), "payments", ["customer_email"], unique=False
    )
    op.create_index(
        op.f("ix_payments_modified_at"), "payments", ["modified_at"], unique=False
    )
    op.create_index(
        op.f("ix_payments_order_id"), "payments", ["order_id"], unique=False
    )
    op.create_index(
        op.f("ix_payments_organization_id"),
        "payments",
        ["organization_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_payments_processor"), "payments", ["processor"], unique=False
    )
    op.create_index(
        op.f("ix_payments_processor_id"), "payments", ["processor_id"], unique=True
    )
    op.create_index(op.f("ix_payments_status"), "payments", ["status"], unique=False)
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f("ix_payments_status"), table_name="payments")
    op.drop_index(op.f("ix_payments_processor_id"), table_name="payments")
    op.drop_index(op.f("ix_payments_processor"), table_name="payments")
    op.drop_index(op.f("ix_payments_organization_id"), table_name="payments")
    op.drop_index(op.f("ix_payments_order_id"), table_name="payments")
    op.drop_index(op.f("ix_payments_modified_at"), table_name="payments")
    op.drop_index(op.f("ix_payments_method"), table_name="payments")
    op.drop_index(op.f("ix_payments_deleted_at"), table_name="payments")
    op.drop_index(op.f("ix_payments_created_at"), table_name="payments")
    op.drop_index(op.f("ix_payments_checkout_id"), table_name="payments")
    op.drop_table("payments")
    # ### end Alembic commands ###
