-- Risk Category and Risk Identification forms with modals

-- Risk Category form
INSERT INTO forms (id, name, description, mapped_table, tenant_id, branch_id)
SELECT 74, 'Risk Category', 'Risk classification', 'risk_categories', NULL, NULL
WHERE NOT EXISTS (SELECT 1 FROM forms WHERE id = 74);

INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
SELECT 74, 'name', 'name', 'name', (SELECT id FROM element_types WHERE name='text' LIMIT 1), 1
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 74 AND field_name = 'name');

-- Default table view for Risk Category
INSERT INTO table_views (form_id, name, columns, sort_by, sort_dir)
SELECT 74, 'Default', '["name"]', 'name', 'ASC'
WHERE NOT EXISTS (SELECT 1 FROM table_views WHERE form_id = 74 AND name = 'Default');

-- Risk Categories Manager modal
INSERT INTO ui_modals (name, config, tenant_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,"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 ui_modals WHERE name = 'Risk Categories Manager');

-- Risk Identification form
INSERT INTO forms (id, name, description, mapped_table, tenant_id, branch_id)
SELECT 75, 'Risk Identification', 'Identify risks', 'risk_identifications', NULL, NULL
WHERE NOT EXISTS (SELECT 1 FROM forms WHERE id = 75);

INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
SELECT 75, 'risk_description', 'risk_description', 'risk_description', (SELECT id FROM element_types WHERE name='textarea' LIMIT 1), 1
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 75 AND field_name = 'risk_description');
INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, options_table, options_label_column, options_value_column, order_index)
SELECT 75, 'Risk category', 'risk_category_id', 'risk_category_id', (SELECT id FROM element_types WHERE name='dropdown' LIMIT 1), 'risk_categories', 'name', 'id', 2
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 75 AND field_name = 'risk_category_id');
INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
SELECT 75, 'root_cause', 'root_cause', 'root_cause', (SELECT id FROM element_types WHERE name='text' LIMIT 1), 3
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 75 AND field_name = 'root_cause');
INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
SELECT 75, 'likelihood', 'likelihood', 'likelihood', (SELECT id FROM element_types WHERE name='dropdown' LIMIT 1), 4
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 75 AND field_name = 'likelihood');
INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
SELECT 75, 'impact', 'impact', 'impact', (SELECT id FROM element_types WHERE name='dropdown' LIMIT 1), 5
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 75 AND field_name = 'impact');

-- Dropdown options 1-5 for likelihood
INSERT INTO element_options (element_id, display_text, value)
SELECT fe.id, v.opt, v.opt
FROM (VALUES ('1'),('2'),('3'),('4'),('5')) AS v(opt)
CROSS JOIN LATERAL (
    SELECT id FROM form_elements WHERE form_id = 75 AND field_name = 'likelihood'
) fe
WHERE NOT EXISTS (
    SELECT 1 FROM element_options eo
    WHERE eo.element_id = fe.id AND eo.value = v.opt
);

-- Dropdown options 1-5 for impact
INSERT INTO element_options (element_id, display_text, value)
SELECT fe.id, v.opt, v.opt
FROM (VALUES ('1'),('2'),('3'),('4'),('5')) AS v(opt)
CROSS JOIN LATERAL (
    SELECT id FROM form_elements WHERE form_id = 75 AND field_name = 'impact'
) fe
WHERE NOT EXISTS (
    SELECT 1 FROM element_options eo
    WHERE eo.element_id = fe.id AND eo.value = v.opt
);
INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, validation, order_index)
SELECT 75, 'risk_rating', 'risk_rating', 'risk_rating', (SELECT id FROM element_types WHERE name='number' LIMIT 1), '{"readOnly":true}', 6
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 75 AND field_name = 'risk_rating');
INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
SELECT 75, 'current_controls', 'current_controls', 'current_controls', (SELECT id FROM element_types WHERE name='textarea' LIMIT 1), 7
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 75 AND field_name = 'current_controls');

-- Default table view for Risk Identification
INSERT INTO table_views (form_id, name, columns, sort_by, sort_dir)
SELECT 75, 'Default', '["risk_description","risk_category_id","likelihood","impact","risk_rating"]', 'risk_description', 'ASC'
WHERE NOT EXISTS (SELECT 1 FROM table_views WHERE form_id = 75 AND name = 'Default');

-- Risk Identification Manager modal
INSERT INTO ui_modals (name, config, tenant_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,"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 ui_modals WHERE name = 'Risk Identification Manager');
