-- =====================================================
-- PT_PAP — success-screen copy update (REGISTRATION + PAYMENT stages)
-- 2026-08-08
-- DRAFT ONLY — not executed against any database, not yet reviewed/approved.
-- Config only. Idempotent (re-running produces the same end state).
-- =====================================================
--
-- Standalone patch for the success-screen text edits made to
-- database/migrations/2026-08-07-01-pt-pap-program-type-workflow.sql, per explicit request —
-- that file's workflow_stage rows are plain INSERTs, so if it already ran once (creating the
-- rows), re-running the edited file won't touch the existing rows at all (no ON CONFLICT /
-- upsert there). This UPDATE applies the same copy directly.
--
-- Scope — 3 screens, all reachable via WF_PAP_V1:
--   1) REGISTRATION stage, SUBMIT_REGISTRATION action, scenario REGISTRATION_SUBMITTED
--      (screen.message) AND defaultScreen.message — "First Page Completion" screen.
--   2) PAYMENT stage, MAKE_PAYMENT action, scenario OFFLINE_PAYMENT_CHEQUE (screen.message)
--      and OFFLINE_PAYMENT_OTHER (screen.message) — "Offline Payment Completion" screen.
--      Cheque keeps its own "Cheques are subject to realization" note distinct from the
--      generic "Payment is subject to realization" used for OFFLINE_PAYMENT_OTHER, per
--      explicit instruction.
--   3) PAYMENT stage, MAKE_PAYMENT action, scenario ONLINE_PAYMENT_SUCCESS (screen.message)
--      — "Online Payment Completion" screen.
--
-- Rejected-state / other scenarios (PAYMENT_EDIT_REQUESTED, FREE_SEAT_REGISTRATION, the
-- PAYMENT stage's defaultScreen "Payment Submitted!") are deliberately left untouched — out
-- of scope for this pass per explicit instruction.
--
-- Targets only the named actionKey/scenarioKey elements inside action_buttons (a jsonb
-- array), via nested jsonb_agg + CASE over jsonb_array_elements at each nesting level
-- (action_buttons -> onSuccess -> successScreenConfig.scenarios), leaving every other
-- element (CANCEL, SAVE_DRAFT, other roles' onSuccess entries, other scenarios) untouched.
-- Mirrors database/migrations/2026-08-07-05-pt-pap-save-draft-condition-update.sql's style.
-- =====================================================

-- 1) REGISTRATION stage — SUBMIT_REGISTRATION success screen (REGISTRATION_SUBMITTED + defaultScreen)
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 of your submission and does not confirm your seat.'::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 of your submission and does not confirm your seat.'::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
  );

-- 2) PAYMENT stage — MAKE_PAYMENT success screens (ONLINE_PAYMENT_SUCCESS, OFFLINE_PAYMENT_CHEQUE, OFFLINE_PAYMENT_OTHER)
UPDATE workflow_stage ws
SET action_buttons = (
        SELECT jsonb_agg(
            CASE
                WHEN elem->>'actionKey' = 'MAKE_PAYMENT' THEN
                    jsonb_set(
                        elem,
                        '{onSuccess}',
                        (
                            SELECT jsonb_agg(
                                CASE
                                    WHEN oe ? 'successScreenConfig' THEN
                                        jsonb_set(
                                            oe,
                                            '{successScreenConfig,scenarios}',
                                            (
                                                SELECT jsonb_agg(
                                                    CASE
                                                        WHEN sc->>'scenarioKey' = 'ONLINE_PAYMENT_SUCCESS' THEN
                                                            jsonb_set(
                                                                sc,
                                                                '{screen,message}',
                                                                to_jsonb(E'Dear {{seekerName}}, congratulations on your successful registration for {{programName}}.\n\nRegistration Details:\nRegistration Number: {{registrationNumber}}\nPayment mode: {{paymentMode}}\n\nA confirmation email and WhatsApp message have been sent to your registered email address and mobile number.'::text)
                                                            )
                                                        WHEN sc->>'scenarioKey' = 'OFFLINE_PAYMENT_CHEQUE' THEN
                                                            jsonb_set(
                                                                sc,
                                                                '{screen,message}',
                                                                to_jsonb(E'Dear {{seekerName}}, we acknowledge your provisional registration for {{programName}}.\n\nRegistration Details:\nRegistration Number: {{registrationNumber}}\nPayment mode: {{paymentMode}}\n\nNote: Cheques are subject to realization. Seat confirmation is subject to availability thereon.\n\nAn acknowledgement email and WhatsApp message have been sent to your registered email address and mobile number.'::text)
                                                            )
                                                        WHEN sc->>'scenarioKey' = 'OFFLINE_PAYMENT_OTHER' THEN
                                                            jsonb_set(
                                                                sc,
                                                                '{screen,message}',
                                                                to_jsonb(E'Dear {{seekerName}}, we acknowledge your provisional registration for {{programName}}.\n\nRegistration Details:\nRegistration Number: {{registrationNumber}}\nPayment mode: {{paymentMode}}\n\nNote: Payment is subject to realization. Seat confirmation is subject to availability thereon.\n\nAn acknowledgement email and WhatsApp message have been sent to your registered email address and mobile number.'::text)
                                                            )
                                                        ELSE sc
                                                    END
                                                )
                                                FROM jsonb_array_elements(oe->'successScreenConfig'->'scenarios') AS sc
                                            )
                                        )
                                    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 = 'PAYMENT'
  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
  );
