-- =====================================================
-- PT_PAP — R1019 — Thank You screen message correction
-- 2026-08-08
-- DRAFT ONLY — not executed against any database, not yet reviewed/approved.
-- Config only. Idempotent (plain UPDATE, safe to re-run).
-- =====================================================
--
-- database/migrations/2026-08-08-02-pt-pap-success-screen-copy-update.sql (already committed/
-- run) set the REGISTRATION stage's "First Page Completion" (Thank You) screen text, ending
-- with:
--   "Please note: This is only an acknowledgement of your submission and does not confirm
--   your seat."
-- Ticket R1019 corrects the last sentence only, to:
--   "Please note: This is only an acknowledgement and does not confirm your seat for the
--   program."
-- Rest of the message (greeting + review/payment-link paragraph) is unchanged. Kept as a
-- standalone follow-up UPDATE rather than editing 02 in place, same convention used
-- throughout these PAP migrations once a base script has already run.
--
-- Same two targets as 02's own STEP 1 (both reachable via WF_PAP_V1's REGISTRATION stage,
-- SUBMIT_REGISTRATION action): the REGISTRATION_SUBMITTED scenario's screen.message, and
-- successScreenConfig.defaultScreen.message. Same nested jsonb_agg + CASE targeting pattern,
-- so every other action/scenario is left untouched.
-- =====================================================

UPDATE workflow_stage ws
SET action_buttons = (
        SELECT jsonb_agg(
            CASE
                WHEN elem->>'actionKey' = 'SUBMIT_REGISTRATION' THEN
                    jsonb_set(
                        elem,
                        '{onSuccess}',
                        (
                            SELECT jsonb_agg(
                                CASE
                                    WHEN oe ? 'successScreenConfig' THEN
                                        jsonb_set(
                                            jsonb_set(
                                                oe,
                                                '{successScreenConfig,scenarios}',
                                                (
                                                    SELECT jsonb_agg(
                                                        CASE
                                                            WHEN sc->>'scenarioKey' = 'REGISTRATION_SUBMITTED' THEN
                                                                jsonb_set(
                                                                    sc,
                                                                    '{screen,message}',
                                                                    to_jsonb(E'Dear {{seekerName}}, we have received your details for {{programName}}.\n\nYour request will now be reviewed. If you are offered a seat, you will receive a communication with a payment link to complete your registration.\n\nPlease note: This is only an acknowledgement and does not confirm your seat for the program.'::text)
                                                                )
                                                            ELSE sc
                                                        END
                                                    )
                                                    FROM jsonb_array_elements(oe->'successScreenConfig'->'scenarios') AS sc
                                                )
                                            ),
                                            '{successScreenConfig,defaultScreen,message}',
                                            to_jsonb(E'Dear {{seekerName}}, we have received your details for {{programName}}.\n\nYour request will now be reviewed. If you are offered a seat, you will receive a communication with a payment link to complete your registration.\n\nPlease note: This is only an acknowledgement and does not confirm your seat for the program.'::text)
                                        )
                                    ELSE oe
                                END
                            )
                            FROM jsonb_array_elements(elem->'onSuccess') AS oe
                        )
                    )
                ELSE elem
            END
        )
        FROM jsonb_array_elements(ws.action_buttons) AS elem
    ),
    updated_at = NOW(),
    updated_by = -2
WHERE ws.stage_key = 'REGISTRATION'
  AND ws.deleted_at IS NULL
  AND ws.workflow_config_id IN (
        SELECT id FROM workflow_configuration
        WHERE workflow_key = 'WF_PAP_V1' AND deleted_at IS NULL
  );
