-- User-based forms for personnel

-- Ensure Lawyer workflow exists before referencing it
INSERT INTO workflows (name, trigger_event, definition, is_active)
VALUES ('Lawyer Created', 'form.37.updated', '{"conditions":[]}', TRUE)
ON CONFLICT (name) DO NOTHING;

-- Driver form mapping to users
INSERT INTO forms (id, name, description, mapped_table, on_submit_workflow_id)
VALUES (45, 'Driver', 'Driver user', 'users', (SELECT id FROM workflows WHERE trigger_event='form.45.updated' LIMIT 1));
INSERT INTO form_elements (form_id,label,field_name,data_column,type_id,order_index) VALUES
  (45,'full_name','full_name','full_name',1,1),
  (45,'email','email',NULL,(SELECT id FROM element_types WHERE name='email' LIMIT 1),2),
  (45,'phone','phone',NULL,1,3),
  (45,'password','password',NULL,(SELECT id FROM element_types WHERE name='text' LIMIT 1),4);
INSERT INTO table_views (form_id,name,columns,sort_by,sort_dir) VALUES
  (45,'Default','["full_name","email","phone"]','full_name','ASC');
INSERT INTO ui_modals (name,config,tenant_id,branch_id,icon,aria_label,description) VALUES
  ('Drivers','{"title":"Drivers","header":[{"type":"button","label":"Add Driver","formName":"Driver"}],"body":[{"type":"table","options":{"tableName":"users","showGlobalSearch":false,"filterField":"groups.name","filterValue":"Drivers"}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',NULL,NULL,'fa-id-card','Drivers','Drivers');

-- Lawyer form
INSERT INTO forms (id, name, description, mapped_table, on_submit_workflow_id)
VALUES (37, 'Lawyer', 'Lawyer user', 'users', (SELECT id FROM workflows WHERE trigger_event='form.37.updated' LIMIT 1));
INSERT INTO form_elements (form_id,label,field_name,data_column,type_id,order_index) VALUES
  (37,'full_name','full_name','full_name',1,1),
  (37,'emails','emails','emails',(SELECT id FROM element_types WHERE name='text-multiple' LIMIT 1),2),
  (37,'phones','phones','phones',(SELECT id FROM element_types WHERE name='text-multiple' LIMIT 1),3),
  (37,'password','password',NULL,(SELECT id FROM element_types WHERE name='text' LIMIT 1),4);
INSERT INTO table_views (form_id,name,columns,sort_by,sort_dir) VALUES
  (37,'Default','["full_name","emails","phones"]','full_name','ASC');
INSERT INTO ui_modals (name,config,tenant_id,branch_id,icon,aria_label,description) VALUES
  ('Lawyers','{"title":"Lawyers","header":[{"type":"button","label":"Add Lawyer","formName":"Lawyer"}],"body":[{"type":"table","options":{"tableName":"users","showGlobalSearch":false,"filterField":"groups.name","filterValue":"Lawyers"}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',NULL,NULL,'fa-gavel','Lawyers','Lawyers');

-- Fleet Engineer form
INSERT INTO forms (id, name, description, mapped_table, on_submit_workflow_id)
VALUES (46, 'Fleet Engineer', 'Fleet engineer user', 'users', (SELECT id FROM workflows WHERE trigger_event='form.46.updated' LIMIT 1));
INSERT INTO form_elements (form_id,label,field_name,data_column,type_id,order_index) VALUES
  (46,'full_name','full_name','full_name',1,1),
  (46,'email','email',NULL,(SELECT id FROM element_types WHERE name='email' LIMIT 1),2),
  (46,'phone','phone',NULL,1,3),
  (46,'password','password',NULL,(SELECT id FROM element_types WHERE name='text' LIMIT 1),4);
INSERT INTO table_views (form_id,name,columns,sort_by,sort_dir) VALUES
  (46,'Default','["full_name","email","phone"]','full_name','ASC');
INSERT INTO ui_modals (name,config,tenant_id,branch_id,icon,aria_label,description) VALUES
  ('Fleet Engineers','{"title":"Fleet Engineers","header":[{"type":"button","label":"Add Fleet Engineer","formName":"Fleet Engineer"}],"body":[{"type":"table","options":{"tableName":"users","showGlobalSearch":false,"filterField":"groups.name","filterValue":"Fleet Engineers"}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',NULL,NULL,'fa-screwdriver-wrench','Fleet Engineers','Fleet Engineers');
