"""create recharge_orders and balance_logs tables

Revision ID: 016_recharge_and_balance
Revises: 015_cart_items
Create Date: 2026-05-17
"""

from alembic import op
import sqlalchemy as sa

revision = "016_recharge_and_balance"
down_revision = "015_cart_items"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "recharge_orders",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
        sa.Column("user_id", sa.BigInteger(), sa.ForeignKey("users.id", ondelete="CASCADE"), nullable=False),
        sa.Column("out_trade_no", sa.String(64), nullable=False),
        sa.Column("trade_no", sa.String(64), nullable=True),
        sa.Column(
            "channel",
            sa.Enum("alipay", "wxpay", "admin", name="rechargechannel"),
            nullable=False,
        ),
        sa.Column("amount", sa.Numeric(10, 2), nullable=False),
        sa.Column("amount_cents", sa.Integer(), nullable=False),
        sa.Column(
            "status",
            sa.Enum("pending", "paid", "expired", "closed", "refunded", name="rechargestatus"),
            nullable=False,
            server_default="pending",
        ),
        sa.Column("callback_raw", sa.String(2048), nullable=True),
        sa.Column("client_ip", sa.String(64), nullable=True),
        sa.Column("remark", sa.String(255), nullable=True),
    )
    op.create_index("ix_recharge_orders_user_id", "recharge_orders", ["user_id"])
    op.create_index("ix_recharge_orders_out_trade_no", "recharge_orders", ["out_trade_no"], unique=True)
    op.create_index("ix_recharge_orders_trade_no", "recharge_orders", ["trade_no"])
    op.create_index("ix_recharge_orders_status", "recharge_orders", ["status"])

    op.create_table(
        "balance_logs",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
        sa.Column("user_id", sa.BigInteger(), sa.ForeignKey("users.id", ondelete="CASCADE"), nullable=False),
        sa.Column(
            "change_type",
            sa.Enum(
                "recharge", "admin_adjust", "order_pay", "order_refund", "recharge_refund",
                name="balancechangetype",
            ),
            nullable=False,
        ),
        sa.Column("delta", sa.Integer(), nullable=False),
        sa.Column("balance_after", sa.Integer(), nullable=False),
        sa.Column("ref_no", sa.String(64), nullable=True),
        sa.Column("operator_id", sa.BigInteger(), nullable=True),
        sa.Column("remark", sa.String(255), nullable=True),
    )
    op.create_index("ix_balance_logs_user_id", "balance_logs", ["user_id"])
    op.create_index("ix_balance_logs_change_type", "balance_logs", ["change_type"])
    op.create_index("ix_balance_logs_ref_no", "balance_logs", ["ref_no"])


def downgrade() -> None:
    op.drop_index("ix_balance_logs_ref_no", table_name="balance_logs")
    op.drop_index("ix_balance_logs_change_type", table_name="balance_logs")
    op.drop_index("ix_balance_logs_user_id", table_name="balance_logs")
    op.drop_table("balance_logs")
    op.drop_index("ix_recharge_orders_status", table_name="recharge_orders")
    op.drop_index("ix_recharge_orders_trade_no", table_name="recharge_orders")
    op.drop_index("ix_recharge_orders_out_trade_no", table_name="recharge_orders")
    op.drop_index("ix_recharge_orders_user_id", table_name="recharge_orders")
    op.drop_table("recharge_orders")
    op.drop_enum("rechargestatus")
    op.drop_enum("rechargechannel")
    op.drop_enum("balancechangetype")
