-- Settings Tabs Permissions Upgrade
-- Run this after the previous migration if you already installed permissions.
-- It adds no new table; it only documents the new action keys used in user_permissions.
-- New settings action_key values:
-- tab_account, tab_users, tab_lost, tab_stages, tab_whatsapp, tab_email, tab_countries, tab_permissions, tab_password, tab_config

-- Optional: give all currently permitted settings users access to all settings tabs.
-- Remove this INSERT if you prefer to assign tabs manually from Settings > Permissions.
INSERT IGNORE INTO user_permissions (account_id, user_id, page_key, action_key, allowed)
SELECT DISTINCT account_id, user_id, 'settings', x.action_key, 1
FROM user_permissions
JOIN (
  SELECT 'tab_account' action_key UNION ALL
  SELECT 'tab_users' UNION ALL
  SELECT 'tab_lost' UNION ALL
  SELECT 'tab_stages' UNION ALL
  SELECT 'tab_whatsapp' UNION ALL
  SELECT 'tab_email' UNION ALL
  SELECT 'tab_countries' UNION ALL
  SELECT 'tab_permissions' UNION ALL
  SELECT 'tab_password' UNION ALL
  SELECT 'tab_config'
) x
WHERE page_key='settings' AND action_key='view' AND allowed=1;
