-- Dashboard widget visibility + KPI selection
ALTER TABLE user_ui_preferences ADD COLUMN IF NOT EXISTS dashboard_widgets JSONB DEFAULT '{}'::jsonb;
ALTER TABLE user_ui_preferences ADD COLUMN IF NOT EXISTS dashboard_kpis JSONB DEFAULT '[]'::jsonb;

-- Ensure RSS widget in default layout for existing users
UPDATE user_ui_preferences
SET dashboard_layout = (
    SELECT jsonb_agg(elem)
    FROM (
        SELECT DISTINCT elem
        FROM jsonb_array_elements_text(
            COALESCE(dashboard_layout, '[]'::jsonb) || '["rss"]'::jsonb
        ) AS elem
    ) s
)
WHERE dashboard_layout IS NOT NULL
  AND NOT dashboard_layout @> '["rss"]'::jsonb;
