-- Migration script to update "Submit Payment" labels to "Submit" in workflow_stage action_buttons
-- Update "Submit Payment" to "Submit" in all workflow stages
UPDATE workflow_stage
SET action_buttons = (
  SELECT jsonb_agg(
    CASE 
      WHEN elem->>'label' = 'Submit Payment' THEN
        jsonb_set(elem, '{label}', '"submit"'::jsonb)
      ELSE
        elem
    END
  )
  FROM jsonb_array_elements(action_buttons) AS elem
)
WHERE action_buttons::text LIKE '%Submit Payment%'
AND deleted_at IS NULL;

-- Update "Submit Travel Details" to "Submit" in all workflow stages
UPDATE workflow_stage
SET action_buttons = (
  SELECT jsonb_agg(
    CASE 
      WHEN elem->>'label' = 'Submit Travel Details' THEN
        jsonb_set(elem, '{label}', '"submit"'::jsonb)
      ELSE
        elem
    END
  )
  FROM jsonb_array_elements(action_buttons) AS elem
)
WHERE action_buttons::text LIKE '%Submit Travel Details%'
AND deleted_at IS NULL;

-- Update "Save" to "Submit" in goodies-related workflow stages
UPDATE workflow_stage
SET action_buttons = (
  SELECT jsonb_agg(
    CASE 
      WHEN elem->>'label' = 'Save' AND elem->>'actionKey' = 'SAVE_GOODIES' THEN
        jsonb_set(elem, '{label}', '"submit"'::jsonb)
      ELSE
        elem
    END
  )
  FROM jsonb_array_elements(action_buttons) AS elem
)
WHERE action_buttons::text LIKE '%SAVE_GOODIES%'
AND action_buttons::text LIKE '%"Save"%'
AND deleted_at IS NULL;

-- Update "Save Goodies" to "submit" in all workflow stages
UPDATE workflow_stage
SET action_buttons = (
  SELECT jsonb_agg(
    CASE 
      WHEN elem->>'label' = 'Save Goodies' THEN
        jsonb_set(elem, '{label}', '"submit"'::jsonb)
      ELSE
        elem
    END
  )
  FROM jsonb_array_elements(action_buttons) AS elem
)
WHERE action_buttons::text LIKE '%Save Goodies%'
AND deleted_at IS NULL;

-- Update "submit goodies selection" to "submit" for SAVE_GOODIES action
UPDATE workflow_stage
SET action_buttons = (
  SELECT jsonb_agg(
    CASE 
      WHEN elem->>'label' = 'Submit Goodies Selection' AND elem->>'actionKey' = 'SAVE_GOODIES' THEN
        jsonb_set(elem, '{label}', '"submit"'::jsonb)
      ELSE
        elem
    END
  )
  FROM jsonb_array_elements(action_buttons) AS elem
)
WHERE action_buttons::text LIKE '%SAVE_GOODIES%'
AND action_buttons::text LIKE '%Submit Goodies Selection%'
AND deleted_at IS NULL;



