-- 增加企业微信审批详情当前快照，供回调同步和后续读取投影复用。
ALTER TABLE tb_wecom_approval_context
    ADD COLUMN IF NOT EXISTS latest_sp_status integer NOT NULL DEFAULT 0,
    ADD COLUMN IF NOT EXISTS latest_detail_snapshot jsonb,
    ADD COLUMN IF NOT EXISTS last_synced_at timestamptz;

DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM pg_constraint WHERE conname = 'chk_wecom_approval_context_detail_snapshot'
    ) THEN
        ALTER TABLE tb_wecom_approval_context
            ADD CONSTRAINT chk_wecom_approval_context_detail_snapshot
            CHECK (latest_detail_snapshot IS NULL OR jsonb_typeof(latest_detail_snapshot) = 'object');
    END IF;
END $$;

COMMENT ON COLUMN tb_wecom_approval_context.latest_sp_status IS '最近一次企微审批详情状态码';
COMMENT ON COLUMN tb_wecom_approval_context.latest_detail_snapshot IS '最近一次权威企微审批详情快照，不含 access_token';
COMMENT ON COLUMN tb_wecom_approval_context.last_synced_at IS '最近一次成功获取企微审批详情的时间';
