-- Add missing fields to datto_rmm_alerts for webhook ingestion
-- Matches Datto RMM webhook payload field names exactly

-- Alert metadata
ALTER TABLE datto_rmm_alerts ADD COLUMN IF NOT EXISTS alert_category TEXT;
ALTER TABLE datto_rmm_alerts ADD COLUMN IF NOT EXISTS alert_type TEXT;
ALTER TABLE datto_rmm_alerts ADD COLUMN IF NOT EXISTS alert_message_en TEXT;

-- Device info
ALTER TABLE datto_rmm_alerts ADD COLUMN IF NOT EXISTS device_id TEXT;
ALTER TABLE datto_rmm_alerts ADD COLUMN IF NOT EXISTS device_hostname TEXT;
ALTER TABLE datto_rmm_alerts ADD COLUMN IF NOT EXISTS device_ip TEXT;
ALTER TABLE datto_rmm_alerts ADD COLUMN IF NOT EXISTS device_os TEXT;
ALTER TABLE datto_rmm_alerts ADD COLUMN IF NOT EXISTS device_description TEXT;
ALTER TABLE datto_rmm_alerts ADD COLUMN IF NOT EXISTS last_user TEXT;

-- Site info
ALTER TABLE datto_rmm_alerts ADD COLUMN IF NOT EXISTS site_id TEXT;

-- Platform
ALTER TABLE datto_rmm_alerts ADD COLUMN IF NOT EXISTS platform TEXT;

-- Triggered flag from webhook (True = alert fired, False = resolved)
ALTER TABLE datto_rmm_alerts ADD COLUMN IF NOT EXISTS triggered TEXT;

-- Device UDFs 1–29 (individual columns matching webhook field names)
ALTER TABLE datto_rmm_alerts ADD COLUMN IF NOT EXISTS device_udf1 TEXT;
ALTER TABLE datto_rmm_alerts ADD COLUMN IF NOT EXISTS device_udf2 TEXT;
ALTER TABLE datto_rmm_alerts ADD COLUMN IF NOT EXISTS device_udf3 TEXT;
ALTER TABLE datto_rmm_alerts ADD COLUMN IF NOT EXISTS device_udf4 TEXT;
ALTER TABLE datto_rmm_alerts ADD COLUMN IF NOT EXISTS device_udf5 TEXT;
ALTER TABLE datto_rmm_alerts ADD COLUMN IF NOT EXISTS device_udf6 TEXT;
ALTER TABLE datto_rmm_alerts ADD COLUMN IF NOT EXISTS device_udf7 TEXT;
ALTER TABLE datto_rmm_alerts ADD COLUMN IF NOT EXISTS device_udf8 TEXT;
ALTER TABLE datto_rmm_alerts ADD COLUMN IF NOT EXISTS device_udf9 TEXT;
ALTER TABLE datto_rmm_alerts ADD COLUMN IF NOT EXISTS device_udf10 TEXT;
ALTER TABLE datto_rmm_alerts ADD COLUMN IF NOT EXISTS device_udf11 TEXT;
ALTER TABLE datto_rmm_alerts ADD COLUMN IF NOT EXISTS device_udf12 TEXT;
ALTER TABLE datto_rmm_alerts ADD COLUMN IF NOT EXISTS device_udf13 TEXT;
ALTER TABLE datto_rmm_alerts ADD COLUMN IF NOT EXISTS device_udf14 TEXT;
ALTER TABLE datto_rmm_alerts ADD COLUMN IF NOT EXISTS device_udf15 TEXT;
ALTER TABLE datto_rmm_alerts ADD COLUMN IF NOT EXISTS device_udf16 TEXT;
ALTER TABLE datto_rmm_alerts ADD COLUMN IF NOT EXISTS device_udf17 TEXT;
ALTER TABLE datto_rmm_alerts ADD COLUMN IF NOT EXISTS device_udf18 TEXT;
ALTER TABLE datto_rmm_alerts ADD COLUMN IF NOT EXISTS device_udf19 TEXT;
ALTER TABLE datto_rmm_alerts ADD COLUMN IF NOT EXISTS device_udf20 TEXT;
ALTER TABLE datto_rmm_alerts ADD COLUMN IF NOT EXISTS device_udf21 TEXT;
ALTER TABLE datto_rmm_alerts ADD COLUMN IF NOT EXISTS device_udf22 TEXT;
ALTER TABLE datto_rmm_alerts ADD COLUMN IF NOT EXISTS device_udf23 TEXT;
ALTER TABLE datto_rmm_alerts ADD COLUMN IF NOT EXISTS device_udf24 TEXT;
ALTER TABLE datto_rmm_alerts ADD COLUMN IF NOT EXISTS device_udf25 TEXT;
ALTER TABLE datto_rmm_alerts ADD COLUMN IF NOT EXISTS device_udf26 TEXT;
ALTER TABLE datto_rmm_alerts ADD COLUMN IF NOT EXISTS device_udf27 TEXT;
ALTER TABLE datto_rmm_alerts ADD COLUMN IF NOT EXISTS device_udf28 TEXT;
ALTER TABLE datto_rmm_alerts ADD COLUMN IF NOT EXISTS device_udf29 TEXT;

-- Indexes for new fields
CREATE INDEX IF NOT EXISTS idx_datto_rmm_alerts_alert_type ON datto_rmm_alerts(alert_type);
CREATE INDEX IF NOT EXISTS idx_datto_rmm_alerts_alert_category ON datto_rmm_alerts(alert_category);
CREATE INDEX IF NOT EXISTS idx_datto_rmm_alerts_device_hostname ON datto_rmm_alerts(device_hostname);
