CREATE TABLE wallet_balance_adjustments (
    id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
    admin_user_id uuid NOT NULL REFERENCES users(id) ON DELETE RESTRICT,
    target_user_id uuid NOT NULL REFERENCES users(id) ON DELETE RESTRICT,
    wallet_id uuid NOT NULL REFERENCES wallets(id) ON DELETE RESTRICT,
    wallet_balance_id uuid NOT NULL REFERENCES wallet_balances(id) ON DELETE RESTRICT,
    account_id uuid NOT NULL REFERENCES accounts(id) ON DELETE RESTRICT,
    direction text NOT NULL CHECK (direction IN ('add', 'subtract')),
    amount_cents bigint NOT NULL CHECK (amount_cents > 0),
    currency char(3) NOT NULL REFERENCES currencies(code) ON DELETE RESTRICT,
    reason text NOT NULL,
    idempotency_key text NOT NULL,
    wallet_balance_before_cents bigint NOT NULL CHECK (wallet_balance_before_cents >= 0),
    wallet_balance_after_cents bigint NOT NULL CHECK (wallet_balance_after_cents >= 0),
    account_balance_before_cents bigint NOT NULL CHECK (account_balance_before_cents >= 0),
    account_balance_after_cents bigint NOT NULL CHECK (account_balance_after_cents >= 0),
    created_at timestamptz NOT NULL DEFAULT now(),
    UNIQUE (admin_user_id, idempotency_key)
);

CREATE INDEX wallet_balance_adjustments_wallet_created_at_idx
    ON wallet_balance_adjustments (wallet_id, created_at DESC);

CREATE INDEX wallet_balance_adjustments_target_user_created_at_idx
    ON wallet_balance_adjustments (target_user_id, created_at DESC);

CREATE INDEX wallet_balance_adjustments_admin_created_at_idx
    ON wallet_balance_adjustments (admin_user_id, created_at DESC);
