-- =====================================================
-- PT_PAP — Question LABEL overrides
-- 2026-08-08
-- DRAFT ONLY — not executed against any database, not yet reviewed/approved.
-- Labels + one config field only. Idempotent (plain UPDATE ... SET, no DO block).
-- =====================================================
--
-- Mirrors database/program-configuration/06_tat_label_overrides_update.sql exactly, scoped
-- to PT_PAP instead of PT_TAT. Applies to the same binding_keys because PAP's program
-- template (database/migrations/2026-08-07-02-pt-pap-program-template.sql) clones both
-- FS_BASICDETAILS and FS_PAYMENTINVOICE from the SAME source program (1082) that TAT's own
-- 05_tat_template.sql was built from, and none of these binding_keys are excluded by either
-- template's blacklist/whitelist:
--   - otherCityName, rmContact, otherInfinitheismContact, profileUrl — FS_BASICDETAILS,
--     explicitly present in PAP's p_basicdetails_included_binding_keys whitelist.
--   - invoiceName, invoiceEmail, isGstRegistered, invoiceAddress, gstNumber, paymentMode,
--     invoiceConfirmation — FS_PAYMENTINVOICE, cloned in full for PAP (blacklist approach,
--     per migration 02's header: "Payment form: left untouched, cloned in full from HDB");
--     none of these 7 keys appear in PAP's p_excluded_questions blacklist.
--   NOT included here: pro-forma invoice fields (pro_forma_invoice_name,
--   proFormaBillingAddress, proFormaIsGstRegistered, proFormaZip, proFormaGstNumber) — these
--   are separate, PAP-only binding_keys (FS_BASICDETAILS) distinct from the FS_PAYMENTINVOICE
--   keys above (e.g. proFormaIsGstRegistered vs isGstRegistered), not covered by this request.
--
-- 06's STEP 3 (T&C content) and STEP 4 (profileUrl isRequired -> false) are intentionally NOT
-- mirrored here: PAP already has its own PAP-specific Terms & Conditions copy injected by
-- migration 02 STEP 5, and profileUrl's required-ness was not requested to change for PAP.
-- =====================================================

-- 1) PAP templates
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 = 'PT_PAP' AND pt.deleted_at IS NULL
          AND ptpl.deleted_at IS NULL AND tfs.deleted_at IS NULL
  );

-- 2) PAP program questions
UPDATE hdb_question hq
SET label      = 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 hq.binding_key = overrides.bk
  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_PAP' AND pt.deleted_at IS NULL
          AND p.deleted_at IS NULL AND hpq.deleted_at IS NULL
  );


-- =====================================================
-- 3) Override isGstRegistered showDialog.dialogueContent for PT_PAP
--    Applies to existing PAP templates AND PAP program questions.
--    Updates ONLY config.showDialog.dialogueContent. Mirrors STEP 5 of
--    06_tat_label_overrides_update.sql. Idempotent.
-- =====================================================

-- PAP templates
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 = 'PT_PAP' AND pt.deleted_at IS NULL
          AND ptpl.deleted_at IS NULL AND tfs.deleted_at IS NULL
  );

-- PAP program questions
UPDATE hdb_question hq
SET config     = jsonb_set(
                    hq.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 hq.binding_key = 'isGstRegistered'
  AND hq.deleted_at IS NULL
  AND hq.config ? 'showDialog'
  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_PAP' AND pt.deleted_at IS NULL
          AND p.deleted_at IS NULL AND hpq.deleted_at IS NULL
  );
