-- =====================================================
-- master_question + remaining program templates — Label overrides
-- 2026-08-12
-- DRAFT ONLY — not executed against any database, not yet reviewed/approved.
--
-- PT_TAT (database/program-configuration/06_tat_label_overrides_update.sql) and PT_PAP
-- (database/migrations/2026-08-08-04-pt-pap-label-overrides-update.sql) already carry these
-- label/config overrides on their own template_question + hdb_question rows. This script
-- brings the same values into:
--   1) master_question — the shared catalog every new program template clones from, so
--      future clones start with the corrected copy instead of the stale original.
--   2) template_question for the remaining program types (PT_HDBMSD, PT_ENTRAINMENT) —
--      confirmed via grep that neither database/program-configuration/06_ent_template.sql
--      nor the entrainment-migrations-v2 refresh overrides these binding_keys, so their
--      template_question rows still carry the original wording.
--
-- hdb_question (live, already-registered program instances) is intentionally NOT touched
-- here — PT_TAT/PT_PAP's own scripts already cover their live instances, and PT_HDBMSD /
-- PT_ENTRAINMENT have no requested change to their existing registrants' answers.
--
-- Only the overrides common to BOTH the TAT and PAP scripts are mirrored here (the 11 label
-- overrides + isGstRegistered showDialog.dialogueContent). TAT's T&C content rewrite and its
-- profileUrl isRequired -> false override are program-specific (PAP explicitly declined the
-- latter) and are NOT propagated to master or to the other program types.
-- =====================================================

-- 1) master_question — canonical labels
UPDATE master_question mq
SET question_text = overrides.lbl,
    updated_at    = NOW(),
    updated_by    = -2
FROM (VALUES
    ('otherCityName',            'City name'),
    ('rmContact',                'Contact person at infinitheism'),
    ('otherInfinitheismContact', 'Other contact person'),
    ('profileUrl',               'Upload photo'),
    ('invoiceName',              'Name to be mentioned on the invoice'),
    ('invoiceEmail',             'Email address to which invoice to be sent'),
    ('isGstRegistered',          'Are you registered under India GST?'),
    ('invoiceAddress',           'Address to be mentioned on the invoice'),
    ('gstNumber',                'GSTIN to be mentioned on the invoice'),
    ('paymentMode',              'Choose your payment method*'),
    ('invoiceConfirmation',      'I have reviewed and confirm that my invoice information is correct and acknowledge that no changes will be permitted after submission.')
) AS overrides(bk, lbl)
WHERE mq.binding_key = overrides.bk
  AND mq.deleted_at IS NULL
  AND mq.master_form_section_id IN (
        SELECT mfs.id
        FROM master_form_section mfs
        WHERE mfs.section_key IN ('FS_BASICDETAILS', 'FS_PAYMENTINVOICE')
          AND mfs.deleted_at IS NULL
  );

-- master_question — isGstRegistered showDialog.dialogueContent
UPDATE master_question mq
SET config     = jsonb_set(
                    mq.config,
                    '{showDialog,dialogueContent}',
                    to_jsonb('Important! If you do not provide your GSTIN, this transaction will be filed under ‘unregistered’ category in our GST return. This cannot be changed after submitting this form.'::text)
                 ),
    updated_at = NOW(),
    updated_by = -2
WHERE mq.binding_key = 'isGstRegistered'
  AND mq.deleted_at IS NULL
  AND mq.config ? 'showDialog'
  AND mq.master_form_section_id IN (
        SELECT mfs.id
        FROM master_form_section mfs
        WHERE mfs.section_key = 'FS_PAYMENTINVOICE'
          AND mfs.deleted_at IS NULL
  );


-- =====================================================
-- 2) Remaining templates (PT_HDBMSD, PT_ENTRAINMENT) — same label overrides
-- =====================================================

UPDATE template_question tq
SET question_text = overrides.lbl,
    updated_at    = NOW(),
    updated_by    = -2
FROM (VALUES
    ('otherCityName',            'City name'),
    ('rmContact',                'Contact person at infinitheism'),
    ('otherInfinitheismContact', 'Other contact person'),
    ('profileUrl',               'Upload photo'),
    ('invoiceName',              'Name to be mentioned on the invoice'),
    ('invoiceEmail',             'Email address to which invoice to be sent'),
    ('isGstRegistered',          'Are you registered under India GST?'),
    ('invoiceAddress',           'Address to be mentioned on the invoice'),
    ('gstNumber',                'GSTIN to be mentioned on the invoice'),
    ('paymentMode',              'Choose your payment method*'),
    ('invoiceConfirmation',      'I have reviewed and confirm that my invoice information is correct and acknowledge that no changes will be permitted after submission.')
) AS overrides(bk, lbl)
WHERE tq.binding_key = overrides.bk
  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 IN ('PT_HDBMSD', 'PT_ENTRAINMENT') AND pt.deleted_at IS NULL
          AND ptpl.deleted_at IS NULL AND tfs.deleted_at IS NULL
  );

-- Remaining templates — isGstRegistered showDialog.dialogueContent
UPDATE template_question tq
SET config     = jsonb_set(
                    tq.config,
                    '{showDialog,dialogueContent}',
                    to_jsonb('Important! If you do not provide your GSTIN, this transaction will be filed under ‘unregistered’ category in our GST return. This cannot be changed after submitting this form.'::text)
                 ),
    updated_at = NOW(),
    updated_by = -2
WHERE tq.binding_key = 'isGstRegistered'
  AND tq.deleted_at IS NULL
  AND tq.config ? 'showDialog'
  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 IN ('PT_HDBMSD', 'PT_ENTRAINMENT') AND pt.deleted_at IS NULL
          AND ptpl.deleted_at IS NULL AND tfs.deleted_at IS NULL
  );
