-- Add OHS Actions, Regulations and Compliance Tasks modals

INSERT INTO ui_modals (name, config, tenant_id, branch_id, icon, aria_label, description)
SELECT 'OHS Actions',
       '{"title":"OHS Actions","header":[{"type":"button","label":"Add Action","formName":"OHS Action"}],"body":[{"type":"table","options":{"tableName":"ohs_actions","showGlobalSearch":false,"rowClick":"selectRecord"}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',
       NULL, NULL, 'fa-list-check', 'OHS Actions', 'OHS Actions'
WHERE NOT EXISTS (SELECT 1 FROM ui_modals WHERE name = 'OHS Actions');

INSERT INTO ui_modals (name, config, tenant_id, branch_id, icon, aria_label, description)
SELECT 'OHS Regulations',
       '{"title":"OHS Regulations","header":[{"type":"button","label":"Add Regulation","formName":"OHS Regulation"}],"body":[{"type":"table","options":{"tableName":"ohs_regulations","showGlobalSearch":false,"rowClick":"selectRecord"}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',
       NULL, NULL, 'fa-book', 'OHS Regulations', 'OHS Regulations'
WHERE NOT EXISTS (SELECT 1 FROM ui_modals WHERE name = 'OHS Regulations');

INSERT INTO ui_modals (name, config, tenant_id, branch_id, icon, aria_label, description)
SELECT 'OHS Compliance Tasks',
       '{"title":"OHS Compliance Tasks","header":[{"type":"button","label":"Add Task","formName":"OHS Compliance Task"}],"body":[{"type":"table","options":{"tableName":"ohs_compliance_tasks","showGlobalSearch":false,"rowClick":"selectRecord"}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',
       NULL, NULL, 'fa-list-check', 'OHS Compliance Tasks', 'OHS Compliance Tasks'
WHERE NOT EXISTS (SELECT 1 FROM ui_modals WHERE name = 'OHS Compliance Tasks');

-- Grant OHS permissions to existing roles
UPDATE roles_permissions
SET permissions = JSON_SET(permissions,
  '$.ohs_actions', JSON_ARRAY('manage'),
  '$.ohs_inspections', JSON_ARRAY('view'),
  '$.ohs_regulations', JSON_ARRAY('manage'),
  '$.documents', JSON_ARRAY('*'),
  '$.ohs_compliance_tasks', JSON_ARRAY('manage'),
  '$.ohs_hazard_types', JSON_ARRAY('manage'))
WHERE role = 'HR Administrator';

UPDATE roles_permissions
SET permissions = JSON_SET(permissions,
  '$.ohs_inspections', JSON_ARRAY('view'))
WHERE role = 'Location Officer';
