-- Invoice Configuration modal and workflow
INSERT INTO ui_modals (name, config, tenant_id, branch_id)
SELECT 'Invoice Configuration', '{"title":"Invoice Configuration","body":[{"type":"form","formId":44}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}' , NULL, NULL
WHERE NOT EXISTS (SELECT 1 FROM ui_modals WHERE name = 'Invoice Configuration');

INSERT INTO workflows (name, trigger_event, definition, is_active)
SELECT 'Invoice Configuration', 'invoice.configure', '{"conditions":[]}', TRUE
WHERE NOT EXISTS (SELECT 1 FROM workflows WHERE name = 'Invoice Configuration');

INSERT INTO workflow_steps (workflow_id, step_order, step_type, config)
SELECT (SELECT id FROM workflows WHERE name='Invoice Configuration'), 1, 'open_modal', '{"modal":"Invoice Configuration"}'
WHERE NOT EXISTS (
  SELECT 1 FROM workflow_steps ws
  JOIN workflows w ON ws.workflow_id = w.id
  WHERE w.name='Invoice Configuration' AND ws.step_type='open_modal'
);
