-- Demo OHS Incident form with multi-step fields
-- Clears existing elements then inserts new definitions

DELETE FROM form_elements WHERE form_id = 4;

-- Base form record (if not already present)
INSERT INTO forms (id, name, description, mapped_table, tenant_id, branch_id)
VALUES (4, 'OHS Incident', 'Default incident report form', 'ohs_incidents', NULL, NULL)
ON CONFLICT (id) DO NOTHING;

-- Form fields
INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index) VALUES
    (4, 'injury_date', 'injury_date', 'injury_date', 3, 1),
    (4, 'injury_time', 'injury_time', 'injury_time', 1, 2),
    (4, 'classification', 'classification', 'classification', 4, 3),
    (4, 'location', 'location', 'location', 1, 4),
    (4, 'nature', 'nature', 'nature', 1, 5),
    (4, 'workplace_part', 'workplace_part', 'workplace_part', 1, 6),
    (4, 'activity', 'activity', 'activity', 1, 7),
    (4, 'unexpected', 'unexpected', 'unexpected', 1, 8),
    (4, 'how_sustained', 'how_sustained', 'how_sustained', 1, 9),
    (4, 'completed_by', 'completed_by', 'completed_by', 1, 10),
    (4, 'position', 'position', 'position', 1, 11),
    (4, 'outcome', 'outcome', 'outcome', 1, 12),
    (4, 'preventative_action', 'preventative_action', 'preventative_action', 1, 13),
    (4, 'investigator_id', 'investigator_id', 'investigator_id', 1, 14),
    (4, 'status', 'status', 'status', 4, 15);

-- Tenant scoped fields
INSERT INTO form_elements
    (form_id, tenant_id, branch_id, label, field_name, data_column, type_id, order_index)
VALUES
    (4, 1, NULL, 'Days Lost', 'days_lost', 'days_lost', 2, 16),
    (4, 1, NULL, 'Return-to-Work Date', 'rtw_date', 'rtw_date', 3, 17),
    (4, 1, NULL, 'Restricted Duty Notes', 'restricted_duty_notes', 'restricted_duty_notes', 1, 18);

-- Classification options
INSERT INTO element_options (element_id, display_text, value) VALUES
    ((SELECT id FROM form_elements WHERE form_id = 4 AND field_name = 'classification'), 'Injury', 'injury'),
    ((SELECT id FROM form_elements WHERE form_id = 4 AND field_name = 'classification'), 'Property Damage', 'property_damage'),
    ((SELECT id FROM form_elements WHERE form_id = 4 AND field_name = 'classification'), 'Environmental', 'environmental'),
    ((SELECT id FROM form_elements WHERE form_id = 4 AND field_name = 'classification'), 'Near Miss', 'near_miss'),
    ((SELECT id FROM form_elements WHERE form_id = 4 AND field_name = 'classification'), 'First Aid', 'first_aid'),
    ((SELECT id FROM form_elements WHERE form_id = 4 AND field_name = 'classification'), 'Medical Treatment', 'medical_treatment'),
    ((SELECT id FROM form_elements WHERE form_id = 4 AND field_name = 'classification'), 'LTI', 'lti'),
    ((SELECT id FROM form_elements WHERE form_id = 4 AND field_name = 'classification'), 'Fatality', 'fatality');

-- Status options
INSERT INTO element_options (element_id, display_text, value) VALUES
    ((SELECT id FROM form_elements WHERE form_id = 4 AND field_name = 'status'), 'Reported', 'reported'),
    ((SELECT id FROM form_elements WHERE form_id = 4 AND field_name = 'status'), 'Investigating', 'investigating'),
    ((SELECT id FROM form_elements WHERE form_id = 4 AND field_name = 'status'), 'Closed', 'closed');

-- Tenant table view including custom fields
INSERT INTO table_views (form_id, tenant_id, branch_id, name, columns, sort_by, sort_dir)
VALUES (4, 1, NULL, 'Default', '["status","injury_date","classification","location","days_lost","rtw_date","restricted_duty_notes"]', 'injury_date', 'DESC');

-- ============================================================================
-- 1) PROPERTY DAMAGE (form_id = 13)
-- ============================================================================
DELETE FROM form_elements WHERE form_id = 13;

-- (Re-declare the form itself in case it’s missing)
INSERT INTO forms (id, name, description, mapped_table, tenant_id, branch_id)
VALUES
  (13, 'OHS Incident – Property Damage', 'Report property damage incidents', 'ohs_incidents', NULL, NULL)
ON CONFLICT (id) DO NOTHING;

-- Fields
INSERT INTO form_elements (form_id, label,           field_name,           data_column,            type_id, order_index) VALUES
  (13, 'Date of Damage',   'incident_date',       'incident_date',        3,       1),
  (13, 'Time of Damage',   'incident_time',       'incident_time',        1,       2),
  (13, 'Location',         'location',            'location',             1,       3),
  (13, 'Asset Affected',   'asset_affected',      'asset_affected',       1,       4),
  (13, 'Damage Description','damage_description', 'damage_description',   1,       5),
  (13, 'Estimated Cost',   'estimated_cost',      'estimated_cost',       2,       6),
  (13, 'Immediate Action', 'immediate_action',    'immediate_action',     1,       7),
  (13, 'Responsible Person','responsible_person','responsible_person',    1,       8),
  (13, 'Status',           'status',              'status',               4,       9),
  (13, 'Investigator',     'investigator_id',     'investigator_id',      1,       10);

-- Status dropdown
INSERT INTO element_options (element_id, display_text, value)
VALUES
  ((SELECT id FROM form_elements WHERE form_id=13 AND field_name='status'), 'Reported',      'reported'),
  ((SELECT id FROM form_elements WHERE form_id=13 AND field_name='status'), 'Investigating','investigating'),
  ((SELECT id FROM form_elements WHERE form_id=13 AND field_name='status'), 'Closed',        'closed');



-- ============================================================================
-- 2) ENVIRONMENTAL (form_id = 14)
-- ============================================================================
DELETE FROM form_elements WHERE form_id = 14;

INSERT INTO forms (id, name, description, mapped_table, tenant_id, branch_id)
VALUES
  (14, 'OHS Incident – Environmental', 'Report environmental incidents (spills, emissions, etc.)', 'ohs_incidents', NULL, NULL)
ON CONFLICT (id) DO NOTHING;

INSERT INTO form_elements (form_id, label,               field_name,              data_column,         type_id, order_index) VALUES
  (14, 'Date',                 'incident_date',         'incident_date',         3,       1),
  (14, 'Time',                 'incident_time',         'incident_time',         1,       2),
  (14, 'Location',             'location',              'location',              1,       3),
  (14, 'Type of Incident',     'env_incident_type',     'env_incident_type',     1,       4),
  (14, 'Impact Description',   'impact_description',    'impact_description',    1,       5),
  (14, 'Area Affected',        'area_affected',         'area_affected',         1,       6),
  (14, 'Volume/Quantity',      'volume',                'volume',                2,       7),
  (14, 'Remediation Action',   'remediation_action',    'remediation_action',    1,       8),
  (14, 'Status',               'status',                'status',                4,       9),
  (14, 'Investigator',         'investigator_id',       'investigator_id',       1,       10);

INSERT INTO element_options (element_id, display_text, value)
VALUES
  ((SELECT id FROM form_elements WHERE form_id=14 AND field_name='status'), 'Reported',      'reported'),
  ((SELECT id FROM form_elements WHERE form_id=14 AND field_name='status'), 'Investigating','investigating'),
  ((SELECT id FROM form_elements WHERE form_id=14 AND field_name='status'), 'Closed',        'closed');



-- ============================================================================
-- 3) NEAR MISS (form_id = 15)
-- ============================================================================
DELETE FROM form_elements WHERE form_id = 15;

INSERT INTO forms (id, name, description, mapped_table, tenant_id, branch_id)
VALUES
  (15, 'OHS Incident – Near Miss', 'Report near-miss incidents', 'ohs_incidents', NULL, NULL)
ON CONFLICT (id) DO NOTHING;

INSERT INTO form_elements (form_id, label,               field_name,              data_column,         type_id, order_index) VALUES
  (15, 'Date',                 'incident_date',         'incident_date',         3,       1),
  (15, 'Time',                 'incident_time',         'incident_time',         1,       2),
  (15, 'Location',             'location',              'location',              1,       3),
  (15, 'Description of Near Miss','description',        'description',           1,       4),
  (15, 'Hazard Identified',    'hazard_identified',     'hazard_identified',     1,       5),
  (15, 'Potential Consequence','potential_consequence', 'potential_consequence', 1,       6),
  (15, 'Immediate Action',     'immediate_action',      'immediate_action',      1,       7),
  (15, 'Status',               'status',                'status',                4,       8),
  (15, 'Investigator',         'investigator_id',       'investigator_id',       1,       9);

INSERT INTO element_options (element_id, display_text, value)
VALUES
  ((SELECT id FROM form_elements WHERE form_id=15 AND field_name='status'), 'Reported',      'reported'),
  ((SELECT id FROM form_elements WHERE form_id=15 AND field_name='status'), 'Investigating','investigating'),
  ((SELECT id FROM form_elements WHERE form_id=15 AND field_name='status'), 'Closed',        'closed');



-- ============================================================================
-- 4) FIRST AID (form_id = 16)
-- ============================================================================
DELETE FROM form_elements WHERE form_id = 16;

INSERT INTO forms (id, name, description, mapped_table, tenant_id, branch_id)
VALUES
  (16, 'OHS Incident – First Aid', 'Report incidents requiring first aid', 'ohs_incidents', NULL, NULL)
ON CONFLICT (id) DO NOTHING;

INSERT INTO form_elements (form_id, label,               field_name,              data_column,         type_id, order_index) VALUES
  (16, 'Date',                 'incident_date',         'incident_date',         3,       1),
  (16, 'Time',                 'incident_time',         'incident_time',         1,       2),
  (16, 'Location',             'location',              'location',              1,       3),
  (16, 'Person Treated',       'person_treated',        'person_treated',        1,       4),
  (16, 'Injury Description',   'injury_description',    'injury_description',    1,       5),
  (16, 'First Aid Given',      'first_aid_given',       'first_aid_given',       1,       6),
  (16, 'Treated By',           'treated_by',            'treated_by',            1,       7),
  (16, 'Follow-Up Required?',  'follow_up_required',    'follow_up_required',    4,       8),
  (16, 'Status',               'status',                'status',                4,       9),
  (16, 'Investigator',         'investigator_id',       'investigator_id',       1,       10);

-- Yes/No for follow-up
INSERT INTO element_options (element_id, display_text, value)
VALUES
  ((SELECT id FROM form_elements WHERE form_id=16 AND field_name='follow_up_required'), 'Yes', 'yes'),
  ((SELECT id FROM form_elements WHERE form_id=16 AND field_name='follow_up_required'), 'No',  'no');

-- Status
INSERT INTO element_options (element_id, display_text, value)
VALUES
  ((SELECT id FROM form_elements WHERE form_id=16 AND field_name='status'), 'Reported',      'reported'),
  ((SELECT id FROM form_elements WHERE form_id=16 AND field_name='status'), 'Investigating','investigating'),
  ((SELECT id FROM form_elements WHERE form_id=16 AND field_name='status'), 'Closed',        'closed');



-- ============================================================================
-- 5) MEDICAL TREATMENT (form_id = 17)
-- ============================================================================
DELETE FROM form_elements WHERE form_id = 17;

INSERT INTO forms (id, name, description, mapped_table, tenant_id, branch_id)
VALUES
  (17, 'OHS Incident – Medical Treatment', 'Report incidents requiring medical treatment', 'ohs_incidents', NULL, NULL)
ON CONFLICT (id) DO NOTHING;

INSERT INTO form_elements (form_id, label,               field_name,              data_column,         type_id, order_index) VALUES
  (17, 'Date',                 'incident_date',         'incident_date',         3,       1),
  (17, 'Time',                 'incident_time',         'incident_time',         1,       2),
  (17, 'Location',             'location',              'location',              1,       3),
  (17, 'Person Treated',       'person_treated',        'person_treated',        1,       4),
  (17, 'Injury Description',   'injury_description',    'injury_description',    1,       5),
  (17, 'Medical Details',      'medical_details',       'medical_details',       1,       6),
  (17, 'Provider',             'medical_provider',      'medical_provider',      1,       7),
  (17, 'Follow-Up Required?',  'follow_up_required',    'follow_up_required',    4,       8),
  (17, 'Status',               'status',                'status',                4,       9),
  (17, 'Investigator',         'investigator_id',       'investigator_id',       1,       10);

-- follow-up yes/no, plus status as above
INSERT INTO element_options (element_id, display_text, value)
VALUES
  ((SELECT id FROM form_elements WHERE form_id=17 AND field_name='follow_up_required'), 'Yes','yes'),
  ((SELECT id FROM form_elements WHERE form_id=17 AND field_name='follow_up_required'), 'No', 'no'),
  ((SELECT id FROM form_elements WHERE form_id=17 AND field_name='status'),            'Reported','reported'),
  ((SELECT id FROM form_elements WHERE form_id=17 AND field_name='status'),            'Investigating','investigating'),
  ((SELECT id FROM form_elements WHERE form_id=17 AND field_name='status'),            'Closed','closed');



-- ============================================================================
-- 6) LTI (Lost Time Injury) (form_id = 18)
-- ============================================================================
DELETE FROM form_elements WHERE form_id = 18;

INSERT INTO forms (id, name, description, mapped_table, tenant_id, branch_id)
VALUES
  (18, 'OHS Incident – LTI', 'Report lost-time injuries', 'ohs_incidents', NULL, NULL)
ON CONFLICT (id) DO NOTHING;

INSERT INTO form_elements (form_id, label,               field_name,              data_column,         type_id, order_index) VALUES
  (18, 'Date',                 'incident_date',         'incident_date',         3,       1),
  (18, 'Time',                 'incident_time',         'incident_time',         1,       2),
  (18, 'Location',             'location',              'location',              1,       3),
  (18, 'Person Injured',       'person_injured',        'person_injured',        1,       4),
  (18, 'Injury Description',   'injury_description',    'injury_description',    1,       5),
  (18, 'Days Lost',            'days_lost',             'days_lost',             2,       6),
  (18, 'Return-to-Work Date',  'rtw_date',              'rtw_date',              3,       7),
  (18, 'Corrective Action',    'corrective_action',     'corrective_action',     1,       8),
  (18, 'Status',               'status',                'status',                4,       9),
  (18, 'Investigator',         'investigator_id',       'investigator_id',       1,       10);

INSERT INTO element_options (element_id, display_text, value)
VALUES
  ((SELECT id FROM form_elements WHERE form_id=18 AND field_name='status'), 'Reported','reported'),
  ((SELECT id FROM form_elements WHERE form_id=18 AND field_name='status'), 'Investigating','investigating'),
  ((SELECT id FROM form_elements WHERE form_id=18 AND field_name='status'), 'Closed','closed');



-- ============================================================================
-- 7) FATALITY (form_id = 19)
-- ============================================================================
DELETE FROM form_elements WHERE form_id = 19;

INSERT INTO forms (id, name, description, mapped_table, tenant_id, branch_id)
VALUES
  (19, 'OHS Incident – Fatality', 'Report fatalities', 'ohs_incidents', NULL, NULL)
ON CONFLICT (id) DO NOTHING;

INSERT INTO form_elements (form_id, label,               field_name,              data_column,         type_id, order_index) VALUES
  (19, 'Date',                 'incident_date',         'incident_date',         3,       1),
  (19, 'Time',                 'incident_time',         'incident_time',         1,       2),
  (19, 'Location',             'location',              'location',              1,       3),
  (19, 'Deceased Name',        'deceased_name',         'deceased_name',         1,       4),
  (19, 'Cause of Death',       'cause_of_death',        'cause_of_death',        1,       5),
  (19, 'Reported to Authorities','reported_authorities','reported_authorities', 3,       6),
  (19, 'Coroner Details',      'coroner_details',       'coroner_details',       1,       7),
  (19, 'Corrective Action',    'corrective_action',     'corrective_action',     1,       8),
  (19, 'Status',               'status',                'status',                4,       9),
  (19, 'Investigator',         'investigator_id',       'investigator_id',       1,       10);

INSERT INTO element_options (element_id, display_text, value)
VALUES
  ((SELECT id FROM form_elements WHERE form_id=19 AND field_name='status'), 'Reported','reported'),
  ((SELECT id FROM form_elements WHERE form_id=19 AND field_name='status'), 'Investigating','investigating'),
  ((SELECT id FROM form_elements WHERE form_id=19 AND field_name='status'), 'Closed','closed');

