BEGIN;

-- 停止入账生产者后在同一事务锁定资金流水与 Outbox，避免检查和删索引之间提交新事实。
LOCK TABLE tb_agent_wallet_transaction, tb_agent_recharge_record, tb_outbox_event IN ACCESS EXCLUSIVE MODE;

DO $$
BEGIN
    IF EXISTS (
        SELECT 1
          FROM tb_outbox_event
         WHERE event_type = 'wallet.agent_main.credited'
    ) THEN
        RAISE EXCEPTION '已存在代理主钱包统一入账事实，禁止移除入账防重约束';
    END IF;
END
$$;

DROP INDEX IF EXISTS uq_agent_recharge_provider_transaction;
DROP INDEX IF EXISTS uq_agent_wallet_credit_posting_reference;

COMMENT ON COLUMN tb_agent_wallet_transaction.transaction_type IS '交易类型：recharge-充值 deduct-扣款 refund-退款 commission-分佣 withdrawal-提现';
COMMENT ON COLUMN tb_agent_wallet_transaction.reference_type IS '关联业务类型：order commission withdrawal topup refund exchange';

COMMIT;
