-- 待补偿扣减记录表
-- 当租户或超管扣减因异常失败时，写入此表，后台任务定期重试
CREATE TABLE IF NOT EXISTS domain_monitor.pending_deductions (
    id SERIAL PRIMARY KEY,
    target_type VARCHAR(20) NOT NULL,          -- 'tenant' / 'sa'
    target_id INTEGER NOT NULL,                -- tenant_id 或 panel_sa_id
    amount NUMERIC(20, 4) NOT NULL,
    biz_order_no VARCHAR(100) NOT NULL,
    retry_count INTEGER DEFAULT 0,
    last_error TEXT,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_pending_deductions_created
    ON domain_monitor.pending_deductions(created_at);
