-- Migration: T-shirt size "size chart" info dialog (Entrainment + HDB/MSD)
-- Description: Merges an infoDialog block into the goodiesTshirtSize question's config,
--              scoped to Entrainment ("PT_ENTRAINMENT") and HDB/MSD ("PT_HDBMSD") only —
--              no other program type's T-shirt size question is touched. Uses jsonb `||`
--              merge so any existing config keys (type, apiUrl, category, isRequired,
--              etc.) are preserved untouched. Idempotent (re-running just re-applies the
--              same infoDialog value).
--
-- Two separate targets, because this app clones questions per program at creation time
-- (see src/program/program.service.ts — manager.save(Question, ...)) rather than reading
-- the template live:
--   1) template_question — the reusable template, so any Entrainment/HDB program created
--      from now on picks this up automatically.
--   2) hdb_question — the actual per-program question rows already cloned for programs
--      that exist today (hdb_question has no program_id of its own; reached via the
--      hdb_program_question mapping -> program_v1 -> program_type_v1).

UPDATE template_question tq
SET config = tq.config || '{
      "infoDialog": {
        "type": "link",
        "linkText": "Click here for the size chart",
        "title": "Size Chart",
        "key": "goodiesTshirtSizeInfoDialog",
        "content": null,
        "confirmText": "okay, got it"
      }
    }'::jsonb,
    updated_at = NOW(),
    updated_by = -2
FROM template_form_section tfs
JOIN program_template pt   ON pt.id = tfs.program_template_id
JOIN program_type_v1 pty   ON pty.id = pt.program_type_id
WHERE tq.template_form_section_id = tfs.id
  AND tq.binding_key              = 'goodiesTshirtSize'
  AND pty.key                     = 'PT_ENTRAINMENT'
  AND pt.is_active                = TRUE
  AND tq.deleted_at               IS NULL
  AND tfs.deleted_at              IS NULL
  AND pt.deleted_at               IS NULL;

-- Backfill already-created Entrainment programs' own cloned question rows, so
-- registrants on programs that already exist see the dialog too, not just future ones.
-- HDB/MSD is deliberately excluded here — only the template above was extended to
-- PT_HDBMSD (affects HDB programs created from now on); existing/live HDB programs'
-- already-cloned question rows are left untouched.
UPDATE hdb_question hq
SET config = COALESCE(hq.config, '{}'::jsonb) || '{
      "infoDialog": {
        "type": "link",
        "linkText": "Click here for the size chart",
        "title": "Size Chart",
        "key": "goodiesTshirtSizeInfoDialog",
        "content": null,
        "confirmText": "okay, got it"
      }
    }'::jsonb,
    updated_at = NOW(),
    updated_by = -2
FROM hdb_program_question pq
JOIN program_v1 p           ON p.id = pq.program_id
JOIN program_type_v1 pty    ON pty.id = p.program_type_id
WHERE pq.question_id  = hq.id
  AND hq.binding_key   = 'goodiesTshirtSize'
  AND pty.key          = 'PT_ENTRAINMENT'
  AND hq.deleted_at    IS NULL
  AND pq.deleted_at    IS NULL
  AND p.deleted_at     IS NULL;
