WITH viji AS (
  SELECT id
  FROM users
  WHERE phone_number = '8939910100'
  ORDER BY id
  LIMIT 1
)
UPDATE template_question tq
SET config = jsonb_set(
      tq.config,
      '{dependsOn}',
      (
        SELECT jsonb_agg(
                 CASE
                   WHEN jsonb_typeof(elem->'value') = 'array'
                     THEN jsonb_set(elem, '{value}', to_jsonb(ARRAY[(SELECT id FROM viji)]))
                   ELSE jsonb_set(elem, '{value}', to_jsonb((SELECT id FROM viji)))
                 END
               )
        FROM jsonb_array_elements(tq.config->'dependsOn') AS elem
      )
    )
FROM template_form_section tfs
JOIN program_template pt ON pt.id = tfs.program_template_id
JOIN program_type_v1 ptype ON ptype.id = pt.program_type_id
WHERE tq.template_form_section_id = tfs.id
  AND ptype.key = 'PST_TAT'
  AND tq.binding_key IN ('otherRM', 'otherInfinitheismContact')
  AND tq.config ? 'dependsOn'
  AND EXISTS (SELECT 1 FROM viji);

-- Verify the repointed questions (value should now be vijjisubbu's id)
-- Scoped to the TAT template only.
SELECT tq.id, tq.binding_key, tq.config->'dependsOn' AS depends_on,
       pt.id AS program_template_id, pt.name AS template_name
FROM template_question tq
JOIN template_form_section tfs ON tfs.id = tq.template_form_section_id
JOIN program_template pt ON pt.id = tfs.program_template_id
JOIN program_type_v1 ptype ON ptype.id = pt.program_type_id
WHERE ptype.key = 'PST_TAT'
  AND tq.binding_key IN ('otherRM', 'otherInfinitheismContact')
ORDER BY tq.binding_key, tq.id;

-- ----------------------------------------------------------------------------
-- Review the verification output above.
--   - roles            = relational_manager (RM) + shoba (+ viewer)
--   - depends_on value = vijjisubbu's id  (for otherRM / otherInfinitheismContact)
-- Then COMMIT to keep changes, or ROLLBACK to discard.
-- ----------------------------------------------------------------------------
COMMIT;
-- ROLLBACK;
