-- Datto RMM Webhook Logs
-- Generic capture table for incoming Datto RMM webhook payloads
-- No processing logic yet — store everything raw for inspection

CREATE TABLE IF NOT EXISTS datto_rmm_webhook_logs (
    id SERIAL PRIMARY KEY,
    received_at TIMESTAMP NOT NULL DEFAULT NOW(),
    source_ip VARCHAR(45),
    user_agent TEXT,
    headers JSONB,
    payload JSONB,
    raw_body TEXT,
    status VARCHAR(20) NOT NULL DEFAULT 'received',  -- received, processed, failed
    notes TEXT,
    created_at TIMESTAMP NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_datto_rmm_webhook_logs_received ON datto_rmm_webhook_logs(received_at DESC);
CREATE INDEX IF NOT EXISTS idx_datto_rmm_webhook_logs_status ON datto_rmm_webhook_logs(status);

COMMENT ON TABLE datto_rmm_webhook_logs IS 'Raw capture of all incoming Datto RMM webhook payloads for inspection';
