
-- Default hazard types
INSERT INTO ohs_hazard_types (name, org_id, branch_id)
SELECT * FROM (VALUES
    ('Physical', NULL, NULL),
    ('Chemical', NULL, NULL),
    ('Biological', NULL, NULL),
    ('Psychosocial', NULL, NULL),
    ('Ergonomic', NULL, NULL)
) AS v(name, org_id, branch_id)
WHERE NOT EXISTS (SELECT 1 FROM ohs_hazard_types WHERE org_id IS NULL AND branch_id IS NULL);

-- Default incident types
INSERT INTO ohs_incident_types (name, form_id, org_id, branch_id)
SELECT * FROM (VALUES
    ('Injury', NULL, NULL, NULL),
    ('Property Damage', NULL, NULL, NULL),
    ('Environmental', NULL, NULL, NULL),
    ('Near Miss', NULL, NULL, NULL),
    ('First Aid', NULL, NULL, NULL),
    ('Medical Treatment', NULL, NULL, NULL),
    ('LTI', NULL, NULL, NULL),
    ('Fatality', NULL, NULL, NULL),
    ('Field Incident', NULL, NULL, NULL)
) AS v(name, form_id, org_id, branch_id)
WHERE NOT EXISTS (SELECT 1 FROM ohs_incident_types WHERE org_id IS NULL AND branch_id IS NULL);

-- Default safety incident types
INSERT INTO safety_incident_types (name, form_id, org_id, branch_id)
SELECT * FROM (VALUES
    ('Minor Injury', 20, NULL, NULL),
    ('Near Miss', 15, NULL, NULL),
    ('Property Damage', 13, NULL, NULL),
    ('Environmental', 14, NULL, NULL),
    ('Other', 97, NULL, NULL),
    ('Field Incident', 108, NULL, NULL)
) AS v(name, form_id, org_id, branch_id)
WHERE NOT EXISTS (SELECT 1 FROM safety_incident_types WHERE org_id IS NULL AND branch_id IS NULL);

-- Ensure the sequence for safety_incident_types continues from the inserted values
SELECT setval('safety_incident_types_id_seq', (SELECT MAX(id) FROM safety_incident_types));

-- Default incident statuses
INSERT INTO ohs_incident_statuses (name, purpose, org_id, branch_id)
SELECT * FROM (VALUES
    ('Reported / Logged', 'Capture the initial incident details', NULL, NULL),
    ('Under Review / Triage', 'Assess seriousness and need for investigation', NULL, NULL),
    ('Investigation in Progress', 'Conduct full investigation and identify root causes', NULL, NULL),
    ('Action Required', 'Implement corrective and preventive measures', NULL, NULL),
    ('Awaiting Review', 'Final review before closure', NULL, NULL),
    ('Closed / Resolved', 'All actions complete, incident formally closed', NULL, NULL),
    ('Reopened', 'Reevaluate due to new info or recurrence', NULL, NULL),
    ('Deferred / On Hold', 'Temporarily paused due to dependencies', NULL, NULL)
) AS v(name, purpose, org_id, branch_id)
WHERE NOT EXISTS (SELECT 1 FROM ohs_incident_statuses WHERE org_id IS NULL AND branch_id IS NULL);

-- Default severity levels
INSERT INTO ohs_severities (name, org_id, branch_id)
SELECT * FROM (VALUES
    ('Minor', NULL, NULL),
    ('Moderate', NULL, NULL),
    ('Major', NULL, NULL),
    ('Severe', NULL, NULL),
    ('Critical', NULL, NULL)
) AS v(name, org_id, branch_id)
WHERE NOT EXISTS (SELECT 1 FROM ohs_severities WHERE org_id IS NULL AND branch_id IS NULL);


-- Default inspection types
INSERT INTO ohs_inspection_types (name, form_id, org_id, branch_id)
SELECT * FROM (VALUES
    ('General Site', NULL, NULL, NULL),
    ('Plant & Equipment', NULL, NULL, NULL),
    ('Confined Space Entry', NULL, NULL, NULL),
    ('Field Work', NULL, NULL, NULL),
    ('Vehicle', NULL, NULL, NULL)
) AS v(name, form_id, org_id, branch_id)
WHERE NOT EXISTS (SELECT 1 FROM ohs_inspection_types WHERE org_id IS NULL AND branch_id IS NULL);

DELETE FROM modules WHERE name = 'Drill Down Dashboard';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES
  ('Drill Down Dashboard','{"title":"Drill Down Dashboard","body":[{"type":"table","options":{"tableName":"ohs_incidents"}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',NULL, NULL,'fa-filter','Drill Down Dashboard','Drill Down Dashboard');

DELETE FROM modules WHERE name = 'Emergency Preparedness';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES
  ('Emergency Preparedness','{"title":"Emergency Preparedness","body":[{"type":"table","options":{"tableName":"ohs_emergency_drills"}},{"type":"table","options":{"tableName":"ohs_emergency_contacts"}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',NULL, NULL,'fa-phone','Emergency Preparedness','Emergency Preparedness');

DELETE FROM modules WHERE name = 'Hazard Management';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES
  ('Hazard Management','{"title":"Hazard Management","header":[{"type":"button","label":"Add Hazard","formName":"OHS Hazard"}],"body":[{"type":"table","options":{"tableName":"ohs_hazards","showGlobalSearch":false,"addButton":false,"editFormId":7}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',NULL, NULL,'fa-shield-halved','Hazard Management','Hazard Management');

DELETE FROM modules WHERE name = 'Hazard Types';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES
  ('Hazard Types','{"title":"Hazard Types","header":[{"type":"button","label":"Add Hazard Type","formName":"Hazard Type"}],"body":[{"type":"table","options":{"tableName":"ohs_hazard_types","showGlobalSearch":false,"addButton":false}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',NULL, NULL,'fa-layer-group','Hazard Types','Hazard Types');

DELETE FROM modules WHERE name = 'Incident Heatmap';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES
  ('Incident Heatmap','{"title":"Incident Heatmap","body":[{"type":"graph-map","options":{"endpoint":"../api/ohs_dashboard/heatmap","dataPath":"rows"}},{"type":"graph-chart","options":{"endpoint":"../api/ohs_dashboard/heatmap","dataPath":"chart","type":"bar"}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',NULL, NULL,'fa-map','Incident Heatmap','Incident Heatmap');

DELETE FROM modules WHERE name = 'Incident Summary Dashboard';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES
  ('Incident Summary Dashboard','{"title":"Incident Summary","body":[{"type":"graph-chart","options":{"endpoint":"../api/ohs_dashboard/summary","type":"pie"}},{"type":"graph-chart","options":{"endpoint":"../api/ohs_dashboard/monthly_trend","type":"line"}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',NULL, NULL,'fa-chart-pie','Incident Summary Dashboard','Incident Summary Dashboard');

DELETE FROM modules WHERE name = 'Inspection Audit';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES
  ('Inspection Audit','{"title":"Inspection Audit","body":[{"type":"table","options":{"tableName":"ohs_inspection_audits"}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',NULL, NULL,'fa-list-check','Inspection Audit','Inspection Audit');

DELETE FROM modules WHERE name = 'JSA Training Status';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES
  ('JSA Training Status','{"title":"JSA Training Status","body":[{"type":"table","options":{"tableName":"ohs_jsas"}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',NULL, NULL,'fa-user-graduate','JSA Training Status','JSA Training Status');

DELETE FROM modules WHERE name = 'Near Miss Dashboard';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES
  ('Near Miss Dashboard','{"title":"Near Miss Dashboard","body":[{"type":"html","id":"nearMissDash","html":"<canvas id=\"nearMissTrend\"></canvas><table class=\"near-miss-table\"></table>"}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',NULL, NULL,'fa-chart-line','Near Miss Dashboard','Near Miss Dashboard');

DELETE FROM modules WHERE name = 'OHS Assignment Rules';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES
  ('OHS Assignment Rules','{"title":"OHS Assignment Rules","body":[{"type":"table","options":{"tableName":"ohs_assignment_rules"}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',NULL, NULL,'fa-route','OHS Assignment Rules','OHS Assignment Rules');

DELETE FROM modules WHERE name = 'WorkCOM Assignment Rules';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES
  ('Inspection Assignment Rules','{"title":"Inspection Assignment Rules","body":[{"type":"table","options":{"tableName":"inspection_assignment_rules"}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',NULL, NULL,'fa-route','Inspection Assignment Rules','Inspection Assignment Rules');

DELETE FROM modules WHERE name = 'WorkCOM Inspection Assignment Rules';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES
  ('WorkCOM Inspection Assignment Rules','{"title":"WorkCOM Inspection Assignment Rules","body":[{"type":"table","options":{"tableName":"inspection_assignment_rules"}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',NULL, NULL,'fa-route','WorkCOM Inspection Assignment Rules','WorkCOM Inspection Assignment Rules');

DELETE FROM modules WHERE name = 'OHS Incident Types';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES
  ('OHS Incident Types','{"title":"OHS Incident Types","header":[{"type":"button","label":"Add Incident Type","formName":"Incident Type"}],"body":[{"type":"table","options":{"tableName":"ohs_incident_types","showGlobalSearch":false,"addButton":false,"editFormId":57}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',NULL, NULL,'fa-clipboard-list','OHS Incident Types','OHS Incident Types');

DELETE FROM modules WHERE name = 'OHS Inspection Types';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES
  ('OHS Inspection Types','{"title":"OHS Inspection Types","header":[{"type":"button","label":"Add Inspection Type","formName":"Inspection Type"}],"body":[{"type":"table","options":{"tableName":"ohs_inspection_types","showGlobalSearch":false,"addButton":false,"editFormId":68}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',NULL, NULL,'fa-clipboard-list','OHS Inspection Types','OHS Inspection Types');

DELETE FROM modules WHERE name = 'OHS Inspection Templates';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES
  ('OHS Inspection Templates','{"title":"OHS Inspection Templates","header":[{"type":"button","label":"Add Template","formName":"Inspection Template"}],"body":[{"type":"table","options":{"tableName":"ohs_inspection_templates","showGlobalSearch":false,"addButton":false,"editFormId":74}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',NULL, NULL,'fa-clipboard-list','OHS Inspection Templates','OHS Inspection Templates');

DELETE FROM modules WHERE name = 'WorkCOM Inspection Types';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES
  ('WorkCOM Inspection Types','{"title":"WorkCOM Inspection Types","header":[{"type":"button","label":"Add Inspection Type","formName":"WorkCOM Inspection Type"}],"body":[{"type":"table","options":{"tableName":"workcom_inspection_types","showGlobalSearch":false,"addButton":false,"editFormId":104}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',NULL, NULL,'fa-clipboard-list','WorkCOM Inspection Types','WorkCOM Inspection Types');

DELETE FROM modules WHERE name = 'OHS Incident Wizard';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES
  ('OHS Incident Wizard','{"title":"OHS Incident Wizard","body":[{"type":"html","id":"incidentWizard","html":"<div id=\"incidentWizard\"></div>"}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',NULL, NULL,'fa-wand-magic-sparkles','OHS Incident Wizard','OHS Incident Wizard');

DELETE FROM modules WHERE name = 'OHS Incidents';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES
  ('OHS Incidents','{"title":"OHS Incidents","header":[{"type":"button","label":"Add Incident","wizardId":1}],"body":[{"type":"table","options":{"tableName":"ohs_incidents","showGlobalSearch":false,"addButton":false,"editFormLookup":{"table":"ohs_incident_types","field":"classification","valueColumn":"name","formColumn":"form_id"}}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',NULL, NULL,'fa-exclamation-triangle','OHS Incidents','OHS Incidents');

DELETE FROM modules WHERE name = 'OHS Field Incident';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES
  ('OHS Field Incident','{"title":"OHS Field Incident","header":[{"type":"button","label":"Add Field Incident","wizardId":1}],"body":[{"type":"table","options":{"tableName":"ohs_incidents","showGlobalSearch":false,"addButton":false,"filterField":"classification","filterValue":"Field Incident","editFormLookup":{"table":"ohs_incident_types","field":"classification","valueColumn":"name","formColumn":"form_id"}}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',NULL, NULL,'fa-exclamation-triangle','OHS Field Incident','OHS Field Incident');

DELETE FROM modules WHERE name = 'OHS Inspections';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES
  ('OHS Inspections','{"title":"OHS Inspections","header":[{"type":"button","label":"Add Inspection","wizardId":3}],"body":[{"type":"table","options":{"tableName":"ohs_inspections","showGlobalSearch":false,"addButton":false,"editFormLookup":{"table":"ohs_inspection_types","field":"inspection_type_id","valueColumn":"id","formColumn":"form_id"}}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',NULL, NULL,'fa-clipboard-check','OHS Inspections','OHS Inspections');

DELETE FROM modules WHERE name = 'My Inspections';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES
  ('My Inspections','{"title":"My Inspections","header":[{"type":"button","label":"Add Inspection","wizardId":3}],"body":[{"type":"table","options":{"tableName":"ohs_inspections","scope":"user","userField":"inspector_id","editFormLookup":{"table":"ohs_inspection_types","field":"inspection_type_id","valueColumn":"id","formColumn":"form_id"}}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',NULL, NULL,'fa-clipboard-check','My Inspections','My Inspections');

DELETE FROM modules WHERE name = 'WorkCOM Inspections';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES
  ('WorkCOM Inspections','{"title":"WorkCOM Inspections","header":[{"type":"button","label":"Add Inspection","wizardId":5}],"body":[{"type":"table","options":{"tableName":"workcom_inspections","showGlobalSearch":false,"addButton":false,"editFormLookup":{"table":"workcom_inspection_types","field":"inspection_type_id","valueColumn":"id","formColumn":"form_id"}}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',NULL, NULL,'fa-clipboard-check','WorkCOM Inspections','WorkCOM Inspections');

DELETE FROM modules WHERE name = 'OHS Investigation Dashboard';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES
  ('OHS Investigation Dashboard','{"title":"Investigation Dashboard","header":[{"type":"button","label":"Add Incident","wizardId":1}],"body":[{"type":"table","options":{"tableName":"ohs_incidents","editFormId":4,"rowClick":"selectRecord"}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',NULL, NULL,'fa-clipboard-question','OHS Investigation Dashboard','OHS Investigation Dashboard');

DELETE FROM modules WHERE name = 'WorkCOM Inspection Dashboard';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES (
  'WorkCOM Inspection Dashboard',
  '{"title":"WorkCOM Inspection Dashboard","body":[{"type":"table","options":{"tableName":"workcom_inspections","calculatedColumns":{"status":"inspector_id ? ''Complete'' : ''Pending''"},"searchInputTypes":{"status":{"type":"select","options":["Pending","Complete"]}}}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',
  NULL, NULL, 'fa-chart-bar', 'WorkCOM Inspection Dashboard', 'WorkCOM Inspection Dashboard'
);

DELETE FROM modules WHERE name = 'Inspector Workload';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES (
  'Inspector Workload',
  '{"title":"Inspector Workload","body":[{"type":"table","options":{"tableName":"workcom_inspections","scope":"user","userField":"inspector_id","calculatedColumns":{"pending_count":"inspector_id ? 0 : 1","completed_count":"inspector_id ? 1 : 0"}}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',
  NULL, NULL, 'fa-tasks', 'Inspector Workload', 'Inspector Workload'
);

DELETE FROM modules WHERE name = 'WorkCOM Inspection Kanban';
INSERT INTO modules (
  name, config, org_id, branch_id, icon, aria_label, description
) VALUES (
  'WorkCOM Inspection Kanban',
  $json$
  {
    "title": "WorkCOM Inspection Kanban",
    "body": [
      {
        "type": "html",
        "html": "<div id=\"workcomKanban\"></div>"
      }
    ],
    "footer": [
      {
        "type": "button",
        "label": "Close",
        "onClick": "closeModal"
      }
    ]
  }
  $json$::jsonb,
  NULL, NULL, 'fa-table-columns', 'WorkCOM Inspection Kanban', 'WorkCOM Inspection Kanban'
);

DELETE FROM modules WHERE name = 'Investigation Form';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES
  ('Investigation Form','{"title":"Investigation Form","body":[{"type":"html","html":"<div id=\"whyList\"></div><button id=\"addWhyBtn\" type=\"button\">Add Why</button><button id=\"saveInvBtn\" type=\"button\">Save</button>"}],"footer":[]}',NULL,NULL,'fa-question','Investigation Form','Investigation Form');

DELETE FROM modules WHERE name = 'OHS JSA';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES
  ('OHS JSA','{"title":"OHS JSA","header":[{"type":"button","label":"Create JSA","formName":"Job Safety Analysis"}],"body":[{"type":"table","options":{"tableName":"ohs_jsas","showGlobalSearch":false,"addButton":false,"editFormId":6}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',NULL, NULL,'fa-helmet-safety','OHS JSA','OHS JSA');

DELETE FROM modules WHERE name = 'Safety Documents';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES
  ('Safety Documents','{"title":"Safety Documents","body":[{"type":"table","options":{"tableName":"safety_documents"}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',NULL, NULL,'fa-file-lines','Safety Documents','Safety Documents');

DELETE FROM modules WHERE name = 'Safety Incidents';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES (
  'Safety Incidents',
  '{"title":"Safety Incidents","header":[{"type":"button","label":"Add Incident","wizardId":6}],"body":[{"type":"table","options":{"tableName":"safety_incidents","showGlobalSearch":false,"addButton":false,"editFormLookup":{"table":"safety_incident_types","field":"incident_type","valueColumn":"id","formColumn":"form_id"},"columnLabels":{"document_ids":"Documents"}}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',
  NULL,
  NULL,
  'fa-helmet-safety',
  'Safety Incidents',
  'Safety Incidents'
);

DELETE FROM modules WHERE name = 'Safety Field Incident';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description)
SELECT 'Safety Field Incident', '{"title":"Safety Field Incident","header":[{"type":"button","label":"Add Field Incident","wizardId":6}],"body":[{"type":"table","options":{"tableName":"safety_incidents","showGlobalSearch":false,"addButton":false,"filterField":"incident_type","filterValue":6,"editFormLookup":{"table":"safety_incident_types","field":"incident_type","valueColumn":"id","formColumn":"form_id"}}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}' , NULL, NULL, 'fa-helmet-safety', 'Safety Field Incident', 'Safety Field Incident'
WHERE NOT EXISTS (SELECT 1 FROM modules WHERE name='Safety Field Incident');

DELETE FROM modules WHERE name = 'OHS Reports';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES
('Ohs Reports','{"title": "OHS Reports", "header": [{"type": "html", "html": "<button class=\"btn btn-sm\" onclick=\"location.href=''../api/ohs_reports/export/excel''\">Export Excel</button><button class=\"btn btn-sm ml-2\" onclick=\"location.href=''../api/ohs_reports/export/pdf''\">Export PDF</button>"}], "body": [{"type": "graph-chart", "options": {"endpoint": "../api/ohs_reports/trendsByDepartment", "type": "line"}}, {"type": "graph-chart", "options": {"endpoint": "../api/ohs_reports/locationBenchmark", "type": "bar"}}], "footer": [{"type": "button", "label": "Close", "onClick": "closeModal"}]}',NULL,NULL,'fa-chart-bar','OHS Reports','OHS Reports');

DELETE FROM modules WHERE name = 'Risk Matrix Dashboard';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES
  ('Risk Matrix Dashboard','{"title":"Risk Matrix Dashboard","body":[{"type":"heatmap","id":"riskMatrixHeatmap","options":{"endpoint":"../api/ohs_risk_matrices"}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',NULL, NULL,'fa-table-cells','Risk Matrix Dashboard','Risk Matrix Dashboard');

DELETE FROM modules WHERE name = 'Root Cause Dashboard';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES
  ('Root Cause Dashboard','{"title":"Root Cause Dashboard","body":[{"type":"html","html":"<div id=\"rootCauseDashboard\"></div>"}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',NULL, NULL,'fa-magnifying-glass-chart','Root Cause Dashboard','Root Cause Dashboard');


DELETE FROM modules WHERE name = 'Fishbone Example';
INSERT INTO modules (name, config, org_id, branch_id) VALUES ('Fishbone Example','{"title":"Fishbone Example","body":[{"type":"fishbone-diagram","options":{"diagramId":1}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',NULL, NULL);

DELETE FROM modules WHERE name = 'Graph Demo';
INSERT INTO modules (name, config, org_id, branch_id) VALUES ('Graph Demo','{"title":"Graph Demo","body":[{"type":"graph-d3","graphId":1}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',NULL, NULL);

DELETE FROM modules WHERE name = 'Fishbone Diagram Manager';
INSERT INTO modules (name, config, org_id, branch_id) VALUES ('Fishbone Diagram Manager','{"title":"Fishbone Diagram Manager","header":[{"type":"button","label":"Add Diagram","onClick":"addFishboneDiagram"}],"body":[{"type":"table","id":"fishboneList","options":{"tableName":"fishbone_diagrams","showGlobalSearch":false,"addButton":false,"rowActions":[{"label":"Edit","onClick":"editFishboneDiagram"}]}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',NULL, NULL);

DELETE FROM modules WHERE name = 'Fault Tree Manager';
INSERT INTO modules (name, config, org_id, branch_id) VALUES ('Fault Tree Manager','{"title":"Fault Tree Manager","header":[{"type":"button","label":"Add Diagram","onClick":"addFaultTreeDiagram"}],"body":[{"type":"table","id":"ftList","options":{"tableName":"ohs_fault_trees","showGlobalSearch":false,"addButton":false,"rowActions":[{"label":"Edit","onClick":"editFaultTreeDiagram"}]}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',NULL, NULL);

DELETE FROM modules WHERE name = 'Five Whys Manager';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description)
VALUES (
  'Five Whys Manager',
  '{"title":"Five Whys Manager","header":[{"type":"button","label":"Add Whys","onClick":"addFiveWhys"}],"body":[{"type":"table","options":{"tableName":"ohs_five_whys","showGlobalSearch":false,"rowActions":[{"label":"Edit","onClick":"editFiveWhys"}],"editFormId":83}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',
  NULL, NULL, 'fa-question', 'Five Whys Manager','Five Whys Manager'
);

DELETE FROM modules WHERE name = 'Severity Dashboard';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES
  ('Severity Dashboard','{"title":"Severity Dashboard","body":[{"type":"html","id":"severityDash","html":"<canvas id=\"sevChart\"></canvas><table class=\"treat-outcomes\"></table>"}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',NULL, NULL,'fa-notes-medical','Severity Dashboard','Severity Dashboard');

DELETE FROM modules WHERE name = 'OHS Actions';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES
  ('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');

DELETE FROM modules WHERE name = 'OHS Regulations';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES
  ('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');

DELETE FROM modules WHERE name = 'OHS Compliance Tasks';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES
  ('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');

DELETE FROM modules WHERE name = 'Safety Inductions';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES
  ('Safety Inductions','{"title":"Safety Inductions","header":[{"type":"button","label":"Add Induction","formName":"Safety Induction"}],"body":[{"type":"table","options":{"tableName":"safety_inductions","showGlobalSearch":false,"rowClick":"selectRecord","editFormId":10}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',NULL, NULL,'fa-helmet-safety','Safety Inductions','Safety Inductions');

DELETE FROM modules WHERE name = 'Issues Log';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES
  ('Issues Log','{"title":"Issues Log","header":[{"type":"button","label":"Add Issue","formName":"Issues Log"}],"body":[{"type":"table","options":{"tableName":"ohs_issues","showGlobalSearch":false,"addButton":false,"editFormId":66}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',NULL, NULL,'fa-bug','Issues Log','Issues Log');

DELETE FROM modules WHERE name = 'Employee Licenses';
-- Employee Licenses modal
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES
  ('Employee Licenses','{"title":"Employee Licenses","header":[{"type":"button","label":"Add License","formId":11}],"body":[{"type":"table","options":{"tableName":"employee_licenses","showGlobalSearch":false,"editFormId":11}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',NULL, NULL,'fa-id-card','Employee Licenses','Employee Licenses');

DELETE FROM modules WHERE name = 'Permits';
-- Permits modal
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES
  ('Permits','{"title":"Permits","header":[{"type":"button","label":"Add Application","formId":15}],"body":[{"type":"table","options":{"tableName":"permit_applications","showGlobalSearch":false,"addButton":false,"editFormId":15,"nestedTableConfig":{"childTableName":"permit_inspections","linkField":"permit_application_id","columnsToShow":["inspector_id","scheduled_for","result","notes","hours_logged"],"editFormId":55}}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',NULL, NULL,'fa-passport','Permits','Permits');
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES ('Certification Tracking','{"title":"Certification Tracking","header":[{"type":"button","label":"Add New","formName":"Training Certificate"}],"body":[{"type":"table","options":{"tableName":"training_certificates","showGlobalSearch":false,"addButton":false}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',NULL, NULL,'fa-certificate','Certification Tracking','Certification Tracking');

DELETE FROM modules WHERE name = 'Needs & Expectations';
-- Needs & Expectations modal
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description)
VALUES ('Needs & Expectations','{"title":"Needs & Expectations","header":[{"type":"button","label":"Add Assessment","formName":"Needs & Expectations Assessment"}],"body":[{"type":"table","options":{"tableName":"ohs_needs_expectations","showGlobalSearch":false,"rowClick":"selectRecord"}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',NULL, NULL,'fa-hands-helping','Needs & Expectations','Needs & Expectations');

DELETE FROM modules WHERE name = 'Incident Alert Rules';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES (
  'Incident Alert Rules',
  '{"title":"Incident Alert Rules","header":[{"type":"button","label":"Add Rule","formName":"Incident Alert Rule"}],"body":[{"type":"table","options":{"tableName":"incident_alert_rules","showGlobalSearch":false,"addButton":false,"editFormId":98}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',
  NULL,
  NULL,
  'fa-bell',
  'Incident Alert Rules',
  'Incident Alert Rules'
);

DELETE FROM modules WHERE name = 'Risk Assessment Types';
-- Risk Assessment Types modal
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES (
  'Risk Assessment Types',
  '{"title":"Risk Assessment Types","header":[{"type":"button","label":"Add Type","formName":"Risk Assessment Type"}],"body":[{"type":"table","options":{"tableName":"risk_assessment_types","showGlobalSearch":false,"addButton":false,"editFormId":74}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',
  NULL,
  NULL,
  'fa-layer-group',
  'Risk Assessment Types',
  'Risk Assessment Types'
);

DELETE FROM modules WHERE name = 'Risk Assessments';
-- Risk Assessments modal
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES (
  'Risk Assessments',
  '{"title":"Risk Assessments","header":[{"type":"button","label":"Add Assessment","wizardId":4}],"body":[{"type":"table","options":{"tableName":"risk_assessments","showGlobalSearch":false,"addButton":false,"editFormLookup":{"table":"risk_assessment_types","field":"risk_assessment_type_id","valueColumn":"id","formColumn":"form_id"}}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',
  NULL,
  NULL,
  'fa-clipboard-list',
  'Risk Assessments',
  'Risk Assessments'
);

DELETE FROM modules WHERE name = 'Risk Categories Manager';
-- Risk Categories Manager modal
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description)
SELECT 'Risk Categories Manager',
       '{"title":"Risk Categories Manager","header":[{"type":"button","label":"Add Category","formName":"Risk Category"}],"body":[{"type":"table","options":{"tableName":"risk_categories","showGlobalSearch":false,"addButton":false,"rowClick":"selectRecord","editFormId":74}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',
       NULL, NULL, 'fa-tags', 'Risk Categories Manager', 'Risk Categories Manager'
WHERE NOT EXISTS (SELECT 1 FROM modules WHERE name = 'Risk Categories Manager');


DELETE FROM modules WHERE name = 'Risk Identification Manager';
-- Risk Identification Manager modal
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description)
SELECT 'Risk Identification Manager',
       '{"title":"Risk Identification Manager","header":[{"type":"button","label":"Add Risk","formName":"Risk Identification"}],"body":[{"type":"table","options":{"tableName":"risk_identifications","showGlobalSearch":false,"addButton":false,"rowClick":"selectRecord","editFormId":75}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',
       NULL, NULL, 'fa-exclamation-triangle', 'Risk Identification Manager', 'Risk Identification Manager'
WHERE NOT EXISTS (SELECT 1 FROM modules WHERE name = 'Risk Identification Manager');

DELETE FROM modules WHERE name = 'Calendar Categories';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES (
  'Calendar Categories',
  '{"title":"Calendar Categories","header":[{"type":"button","label":"Add Category","formName":"Calendar Category"}],"body":[{"type":"table","options":{"tableName":"calendar_categories","showGlobalSearch":false,"addButton":false,"editFormId":74}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',
  NULL,
  NULL,
  'fa-tags',
  'Calendar Categories',
  'Calendar Categories'
);



DELETE FROM modules WHERE name = 'Inspection Templates';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES (
  'Inspection Templates',
  '{"title":"Inspection Templates","header":[{"type":"button","label":"Add Template","formName":"Inspection Template"}],"body":[{"type":"table","options":{"tableName":"inspection_templates","showGlobalSearch":false,"addButton":false,"editFormId":70}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',
  NULL,
  NULL,
  'fa-clipboard-list',
  'Inspection Templates',
  'Inspection Templates'
);


DELETE FROM modules WHERE name = 'OHS Incident Statuses';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description)
VALUES (
  'OHS Incident Statuses',
  '{"title":"OHS Incident Statuses","header":[{"type":"button","label":"Add Incident Status","formName":"Incident Status"}],"body":[{"type":"table","options":{"tableName":"ohs_incident_statuses","showGlobalSearch":false,"addButton":false,"editFormId":72}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',
  NULL,
  NULL,
  'fa-clipboard-check',
  'OHS Incident Statuses',
  'OHS Incident Statuses'
);

DELETE FROM modules WHERE name = 'Corrective Action Tracking';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description)
VALUES (
  'Corrective Action Tracking',
  '{"title":"Corrective Action Tracking","header":[{"type":"button","label":"Add Corrective Action","formName":"Corrective Action"}],"body":[{"type":"table","options":{"tableName":"corrective_actions","columnsToShow":["incident_id","name","description","steps","responsible_user_id","status"],"showGlobalSearch":false}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',
  NULL,
  NULL,
  'fa-list-check',
  'Corrective Action Tracking',
  'Corrective Action Tracking'
);


DELETE FROM modules WHERE name = 'Incidents by classification';
-- Insert Incidents by classification modal referencing saved graph
INSERT INTO modules
  (name, config, org_id, branch_id, icon, aria_label, description)
VALUES (
  'Incidents by classification',
  '{"title":"Incidents by classification","body":[{"type":"graph-d3","graphId":1}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',
  NULL, NULL, 'fa-chart-line', 'Incidents by classification', 'Incidents by classification');

DELETE FROM modules WHERE name = 'Incident Trends';
-- Insert Incident Trends modal referencing second graph
INSERT INTO modules
  (name, config, org_id, branch_id, icon, aria_label, description)
VALUES (
  'Incident Trends',
  '{"title":"Incident Trends","body":[{"type":"graph-d3","graphId":2,"options":{"timeUnits":["day","week","month","year"]}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',
  NULL, NULL, 'fa-chart-bar', 'Incident Trends', 'Incident Trends');

DELETE FROM modules WHERE name = 'Workplace Registration';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES ('Workplace Registration','{"title":"Workplace Registration","header":[{"type":"button","label":"New Registration","formName":"Workplace Registration"}],"body":[{"type":"table","options":{"tableName":"workplace_registrations","rowClick":"selectRecord","rowActions":[{"label":"Detail","onClick":"openSelectedDetail"},{"label":"Invoice","onClick":"openWorkplaceInvoiceForm"},{"label":"Review","onClick":"openWorkplaceReview"}],"inlineEdit":true,"editFormId":98}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',NULL,NULL,'fa-building','Workplace Registration','Workplace Registration');

DELETE FROM modules WHERE name = 'Inspection Request';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES (
  'Inspection Request',
  '{"title":"Inspection Request","header":[{"type":"button","label":"Add Request","formName":"Inspection Request"}],"body":[{"type":"table","options":{"tableName":"inspection_requests","rowClick":"selectRecord","rowActions":[{"label":"Detail","onClick":"openSelectedDetail"}],"inlineEdit":true,"editFormId":99}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',
  NULL, NULL, 'fa-clipboard-list', 'Inspection Request', 'Inspection Request');

DELETE FROM modules WHERE name = 'WorkCOM Inspection Assignment';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES (
  'WorkCOM Inspection Assignment',
  '{"title":"Assign Inspector","header":[{"type":"button","label":"Add Assignment","formName":"WorkCOM Inspection Assignment"}],"body":[{"type":"table","options":{"tableName":"workplace_inspections","showGlobalSearch":false,"addButton":false,"editFormId":100}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',
  NULL, NULL, 'fa-user-check', 'WorkCOM Inspection Assignment', 'WorkCOM Inspection Assignment');

DELETE FROM modules WHERE name = 'WorkCOM OHS Inspections';
-- WorkCOM OHS Inspections modal with Assign Inspector action
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES (
  'WorkCOM OHS Inspections',
  '{"title":"WorkCOM OHS Inspections","header":[{"type":"button","label":"New Inspection","wizardId":5}],"body":[{"type":"table","options":{"tableName":"workplace_inspections","showGlobalSearch":false,"addButton":false,"rowActions":[{"label":"Assign Inspector","formName":"WorkCOM Inspection Assignment"}]}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',
  NULL, NULL, 'fa-clipboard-check', 'WorkCOM OHS Inspections', 'WorkCOM OHS Inspections');

DELETE FROM modules WHERE name = 'Workplace Notice';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES (
  'Workplace Notice',
  '{"title":"Workplace Notice","body":[{"type":"form","formName":"Workplace Notice"}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',
  NULL, NULL, 'fa-file-invoice', 'Workplace Notice', 'Workplace Notice');

DELETE FROM modules WHERE name = 'Safety Incident Types';
INSERT INTO modules (name, config, org_id, branch_id, icon, aria_label, description) VALUES (
  'Safety Incident Types',
  '{"title":"Safety Incident Types","header":[{"type":"button","label":"Add Type","formName":"Safety Incident Type"}],"body":[{"type":"table","options":{"tableName":"safety_incident_types","showGlobalSearch":false,"addButton":false,"editFormId":105}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',
  NULL, NULL, 'fa-list', 'Safety Incident Types', 'Safety Incident Types'
);


INSERT INTO group_module_permissions (group_id, module_id, action)
SELECT 
    '0194a8ba-0000-7123-8123-123456789ad1'::uuid AS group_id,
    m.module_id,
    'allow' AS action
FROM modules m
WHERE m.name IN (
    -- Incidents (5 modules)
    'OHS Incidents',
    'Safety Incidents',
    'OHS Incident Types',
    'Safety Incident Types',
    'Incident Alert Rules',
    -- RCA Tools (3 modules)
    'Five Whys Manager',
    'Fault Tree Manager',
    'Fishbone Diagram Manager',
    -- Training (3 modules)
    'Safety Inductions',
    'JSA Training Status',
    'OHS JSA',
    -- Certification (3 modules)
    'Certification Tracking',
    'Employee Licenses',
    'Certificates',
    -- Inspections (6 modules)
    'OHS Inspections',
    'My Inspections',
    'WorkCOM Inspections',
    'Inspection Templates',
    'OHS Inspection Templates',
    'OHS Inspection Types',
    -- Other (1 module)
    'Issues Log'
)
ON CONFLICT DO NOTHING;
