-- Workflows for personnel user creation

INSERT INTO workflows (name, trigger_event, definition, is_active)
VALUES
  ('Driver Created', 'form.45.updated', '{"conditions":[]}', TRUE),
  ('Lawyer Created', 'form.37.updated', '{"conditions":[]}', TRUE),
  ('Fleet Engineer Created', 'form.46.updated', '{"conditions":[]}', TRUE)
ON CONFLICT (name) DO NOTHING;

-- Driver workflow steps
INSERT INTO workflow_steps (workflow_id, step_order, step_type, config) VALUES
  ((SELECT id FROM workflows WHERE name='Driver Created'),1,'map_fields','{"map":{"user_id":"id"}}'),
  ((SELECT id FROM workflows WHERE name='Driver Created'),2,'save_record','{"table":"users","data":{"full_name":"{full_name}","password":"{password}"},"context_key":"user_id"}'),
  ((SELECT id FROM workflows WHERE name='Driver Created'),3,'ensure_group','{"name":"Drivers"}'),
  ((SELECT id FROM workflows WHERE name='Driver Created'),4,'save_record','{"table":"user_group_memberships","data":{"user_id":"{user_id}","group_id":"{group_id}","role":"Driver"}}'),
  ((SELECT id FROM workflows WHERE name='Driver Created'),5,'load_record','{"table":"users","id_key":"user_id","context_key":"user"}'),
  ((SELECT id FROM workflows WHERE name='Driver Created'),6,'queue_notification','{"entity":"users","record_id":"{user_id}","event_type":"insert","payload":"{user}"}');

-- Lawyer workflow steps
INSERT INTO workflow_steps (workflow_id, step_order, step_type, config) VALUES
  ((SELECT id FROM workflows WHERE name='Lawyer Created'),1,'map_fields','{"map":{"user_id":"id"}}'),
  ((SELECT id FROM workflows WHERE name='Lawyer Created'),2,'save_record','{"table":"users","data":{"full_name":"{full_name}","password":"{password}"},"context_key":"user_id"}'),
  ((SELECT id FROM workflows WHERE name='Lawyer Created'),3,'ensure_group','{"name":"Lawyers"}'),
  ((SELECT id FROM workflows WHERE name='Lawyer Created'),4,'save_record','{"table":"user_group_memberships","data":{"user_id":"{user_id}","group_id":"{group_id}","role":"Lawyer"}}'),
  ((SELECT id FROM workflows WHERE name='Lawyer Created'),5,'load_record','{"table":"users","id_key":"user_id","context_key":"user"}'),
  ((SELECT id FROM workflows WHERE name='Lawyer Created'),6,'queue_notification','{"entity":"users","record_id":"{user_id}","event_type":"insert","payload":"{user}"}');

-- Fleet Engineer workflow steps
INSERT INTO workflow_steps (workflow_id, step_order, step_type, config) VALUES
  ((SELECT id FROM workflows WHERE name='Fleet Engineer Created'),1,'map_fields','{"map":{"user_id":"id"}}'),
  ((SELECT id FROM workflows WHERE name='Fleet Engineer Created'),2,'save_record','{"table":"users","data":{"full_name":"{full_name}","password":"{password}"},"context_key":"user_id"}'),
  ((SELECT id FROM workflows WHERE name='Fleet Engineer Created'),3,'ensure_group','{"name":"Fleet Engineers"}'),
  ((SELECT id FROM workflows WHERE name='Fleet Engineer Created'),4,'save_record','{"table":"user_group_memberships","data":{"user_id":"{user_id}","group_id":"{group_id}","role":"Fleet Engineer"}}'),
  ((SELECT id FROM workflows WHERE name='Fleet Engineer Created'),5,'load_record','{"table":"users","id_key":"user_id","context_key":"user"}'),
  ((SELECT id FROM workflows WHERE name='Fleet Engineer Created'),6,'queue_notification','{"entity":"users","record_id":"{user_id}","event_type":"insert","payload":"{user}"}');

-- Ensure Lawyer form references the newly inserted workflow
UPDATE forms
  SET on_submit_workflow_id = (
    SELECT id FROM workflows WHERE name='Lawyer Created'
  )
WHERE name='Lawyer';
