BEGIN;

-- 将实名逆转确认状态与卡事实放入同一 PostgreSQL 事务，避免 Redis 计数与状态回滚不一致。
ALTER TABLE tb_iot_card
    ADD COLUMN realname_reversal_count INTEGER NOT NULL DEFAULT 0,
    ADD COLUMN realname_reversal_started_at TIMESTAMPTZ;

COMMENT ON COLUMN tb_iot_card.realname_reversal_count IS '实名逆转连续观测次数';
COMMENT ON COLUMN tb_iot_card.realname_reversal_started_at IS '实名逆转当前确认窗口开始时间';

ALTER TABLE tb_iot_card
    ADD CONSTRAINT chk_iot_card_realname_reversal_count
        CHECK (realname_reversal_count >= 0 AND realname_reversal_count < 3);

COMMIT;
