-- Add system_info.view permission for existing installs.
-- Idempotent: ON DUPLICATE KEY prevents re-insert.

INSERT INTO `permissions` (`key`, `description`, `active`, `is_system`)
VALUES ('system_info.view', 'Can view system info and health status', 1, 1)
ON DUPLICATE KEY UPDATE `description` = VALUES(`description`);

-- Assign to Admin role(s) for existing installs.
INSERT INTO `role_permissions` (`role_id`, `permission_id`, `created`)
SELECT r.id, p.id, NOW()
FROM roles r
JOIN permissions p ON p.`key` = 'system_info.view'
WHERE r.description IN ('Admin', 'Administrator') OR r.id = 1
ON DUPLICATE KEY UPDATE role_id = role_id;
