-- =====================================================
-- PT_ENTRAINMENT — Mark selected questions optional (config.isRequired -> false)
-- 2026-08-18
-- DRAFT ONLY — not executed against any database, not yet reviewed/approved.
-- Updates ONLY config.isRequired; rest of config (validation, dependsOn, etc.) untouched.
-- Idempotent (plain UPDATE ... SET, safe to re-run; no-op wherever no matching row exists).
-- =====================================================
--
-- Per explicit instruction: the following FS_TRAVELPLAN fields should not be required for
-- Entrainment registrants (visa/passport/international ID and onward/return travel details
-- only apply to a subset of participants), and profileUrl (Upload photo, FS_BASICDETAILS) is
-- optional too. Mirrors the profileUrl isRequired -> false pattern already established for
-- PT_TAT in database/program-configuration/06_tat_label_overrides_update.sql STEP 4 (and
-- intentionally NOT mirrored for PT_PAP per that program's own label-overrides migration
-- header), scoped here to PT_ENTRAINMENT and extended to this larger set of binding_keys.
--
-- Binding keys affected:
--   visa, passport, internationalId, goodiesTshirtApplicable, uploadOnwardJourneyTicket,
--   onwardInfo, returnInfo, uploadReturnJourneyTicket, alternatePhoneNumber, profileUrl
--
-- Applies to both template_question (the PT_ENTRAINMENT template built by
-- 03template-refresh.sql) and hdb_question (any already-existing Entrainment program's
-- cloned questions), matching this folder's established two-table update pattern (see
-- 04terms-conditions-content-update.sql).
-- =====================================================

DO $$
DECLARE
    p_optional_binding_keys TEXT[] := ARRAY[
        'profileUrl'
    ];
BEGIN
    -- 1) Entrainment templates
    UPDATE template_question tq
    SET config     = jsonb_set(COALESCE(tq.config, '{}'::jsonb), '{isRequired}', 'false'::jsonb),
        updated_at = NOW(),
        updated_by = -2
    WHERE tq.binding_key = ANY(p_optional_binding_keys)
      AND tq.deleted_at IS NULL
      AND tq.template_form_section_id IN (
            SELECT tfs.id
            FROM template_form_section tfs
            JOIN program_template ptpl ON ptpl.id = tfs.program_template_id
            JOIN program_type_v1   pt  ON pt.id   = ptpl.program_type_id
            WHERE pt.key = 'PT_ENTRAINMENT' AND pt.deleted_at IS NULL
              AND ptpl.deleted_at IS NULL AND tfs.deleted_at IS NULL
      );

    -- 2) Entrainment program questions
    UPDATE hdb_question hq
    SET config     = jsonb_set(COALESCE(hq.config, '{}'::jsonb), '{isRequired}', 'false'::jsonb),
        updated_at = NOW(),
        updated_by = -2
    WHERE hq.binding_key = ANY(p_optional_binding_keys)
      AND hq.deleted_at IS NULL
      AND hq.id IN (
            SELECT hpq.question_id
            FROM hdb_program_question hpq
            JOIN program_v1      p  ON p.id  = hpq.program_id
            JOIN program_type_v1 pt ON pt.id = p.program_type_id
            WHERE pt.key = 'PT_ENTRAINMENT' AND pt.deleted_at IS NULL
              AND p.deleted_at IS NULL AND hpq.deleted_at IS NULL
      );
END $$;
