"""Phase 7.6 — Wallet Engine (accounts + immutable ledger)."""
from alembic import op
import sqlalchemy as sa

revision = "0007_phase_76_wallet"
down_revision = "0006_phase_75_referral"
branch_labels = None
depends_on = None


def upgrade():
    op.create_table(
        "wallet_accounts",
        sa.Column("id", sa.CHAR(length=36), primary_key=True),
        sa.Column("tenant_id", sa.CHAR(length=36), nullable=False),
        sa.Column("program_id", sa.CHAR(length=36), nullable=False),
        sa.Column("member_id", sa.CHAR(length=36), nullable=False),
        sa.Column("account_number", sa.String(length=50), nullable=False),
        sa.Column(
            "currency_code",
            sa.String(length=3),
            nullable=False,
            server_default="IRR",
        ),
        sa.Column("status", sa.String(length=20), nullable=False),
        sa.Column(
            "is_deleted", sa.Boolean(), nullable=False, server_default=sa.false()
        ),
        sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("deleted_by", sa.String(length=100), nullable=True),
        sa.Column("created_by", sa.String(length=100), nullable=True),
        sa.Column("updated_by", sa.String(length=100), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
        sa.UniqueConstraint(
            "tenant_id",
            "account_number",
            name="uq_wallet_accounts_tenant_account_number",
        ),
        sa.UniqueConstraint(
            "tenant_id",
            "program_id",
            "member_id",
            name="uq_wallet_accounts_tenant_program_member",
        ),
    )
    op.create_index(
        "ix_wallet_accounts_program", "wallet_accounts", ["tenant_id", "program_id"]
    )
    op.create_index(
        "ix_wallet_accounts_member", "wallet_accounts", ["tenant_id", "member_id"]
    )
    op.create_index(
        "ix_wallet_accounts_tenant_deleted",
        "wallet_accounts",
        ["tenant_id", "is_deleted"],
    )

    op.create_table(
        "wallet_ledger_entries",
        sa.Column("id", sa.CHAR(length=36), primary_key=True),
        sa.Column("tenant_id", sa.CHAR(length=36), nullable=False),
        sa.Column("wallet_account_id", sa.CHAR(length=36), nullable=False),
        sa.Column("program_id", sa.CHAR(length=36), nullable=False),
        sa.Column("member_id", sa.CHAR(length=36), nullable=False),
        sa.Column("entry_type", sa.String(length=30), nullable=False),
        sa.Column("amount", sa.Integer(), nullable=False),
        sa.Column("balance_after", sa.Integer(), nullable=False),
        sa.Column("idempotency_key", sa.String(length=100), nullable=True),
        sa.Column("reference_type", sa.String(length=50), nullable=True),
        sa.Column("reference_id", sa.String(length=100), nullable=True),
        sa.Column("reason", sa.String(length=500), nullable=True),
        sa.Column("metadata_json", sa.JSON(), nullable=True),
        sa.Column("actor_user_id", sa.String(length=100), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
        sa.UniqueConstraint(
            "tenant_id",
            "idempotency_key",
            name="uq_wallet_ledger_tenant_idempotency",
        ),
    )
    op.create_index(
        "ix_wallet_ledger_account",
        "wallet_ledger_entries",
        ["tenant_id", "wallet_account_id"],
    )
    op.create_index(
        "ix_wallet_ledger_member", "wallet_ledger_entries", ["tenant_id", "member_id"]
    )
    op.create_index(
        "ix_wallet_ledger_type", "wallet_ledger_entries", ["tenant_id", "entry_type"]
    )
    op.create_index(
        "ix_wallet_ledger_created",
        "wallet_ledger_entries",
        ["tenant_id", "created_at"],
    )


def downgrade():
    op.drop_index("ix_wallet_ledger_created", table_name="wallet_ledger_entries")
    op.drop_index("ix_wallet_ledger_type", table_name="wallet_ledger_entries")
    op.drop_index("ix_wallet_ledger_member", table_name="wallet_ledger_entries")
    op.drop_index("ix_wallet_ledger_account", table_name="wallet_ledger_entries")
    op.drop_table("wallet_ledger_entries")

    op.drop_index("ix_wallet_accounts_tenant_deleted", table_name="wallet_accounts")
    op.drop_index("ix_wallet_accounts_member", table_name="wallet_accounts")
    op.drop_index("ix_wallet_accounts_program", table_name="wallet_accounts")
    op.drop_table("wallet_accounts")
