-- =====================================================
-- PT_ENTRAINMENT — success-screen copy update (PAYMENT + TRAVEL stages)
-- 2026-08-20
-- DRAFT ONLY — not executed against any database, not yet reviewed/approved.
-- Config only. Idempotent (re-running produces the same end state).
-- =====================================================
--
-- Standalone patch bringing the PAYMENT/TRAVEL success screens in line with the actual live
-- Entrainment thank-you screens (screenshots reviewed directly), rather than editing
-- 02workflow-refresh.sql / 02workflow-update-refresh-rejected-guard-update.sql in place —
-- those files are ALREADY EXECUTED (2.0.0) / a pending patch (2.0.1) respectively, so this
-- follows the same standalone-patch pattern as
-- database/migrations/2026-08-08-02-pt-pap-success-screen-copy-update.sql.
--
-- Scope — 5 screens, all reachable via the live PT_ENTRAINMENT workflow_configuration:
--   1) PAYMENT stage, MAKE_PAYMENT action, scenario ONLINE_PAYMENT_SUCCESS (screen.title/
--      message/actions) — drops DOWNLOAD_INVOICE (still available via
--      GET /invoice/download/:registrationId), adds NEW_REGISTRATION.
--   2) PAYMENT stage, MAKE_PAYMENT action, scenario OFFLINE_PAYMENT_CHEQUE (screen.title/
--      message/actions).
--   3) PAYMENT stage, MAKE_PAYMENT action, scenario OFFLINE_PAYMENT_OTHER (screen.title/
--      message/actions) — same copy as OFFLINE_PAYMENT_CHEQUE; the live screens don't
--      distinguish cheque vs. other offline methods (unlike PT_PAP's).
--   4) PAYMENT stage, MAKE_PAYMENT action, defaultScreen (title/message/actions) — generic
--      helpline/email fallback screen.
--   5) TRAVEL stage, SAVE_TRAVEL action, flat successScreen (title/message/actions) — generic
--      helpline/email "Thank you..." screen (nothing left to confirm once travel is submitted;
--      no PROCEED_TO_TRAVEL button since that step is already done).
--
-- {{seekerName}} / {{registrationNumber}} reuse the same placeholder convention already
-- established by PT_PAP's success-screen migration (no additional variable linking needed —
-- unlike {{logoUrl}}/{{bannerImageUrl}}, PAP's migration used these directly without linking).
--
-- NEW_REGISTRATION is a brand-new actionKey (grep confirms it's used nowhere else in this
-- codebase) — like HOME, apiKey is null, implying pure frontend navigation to a fresh
-- registration form; nothing server-side needs wiring for it.
--
-- Other scenarios (PAYMENT_EDIT_REQUESTED) are deliberately left untouched — out of scope,
-- no screenshot reviewed for it.
--
-- 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,
-- PAYMENT_EDIT_REQUESTED, other roles' onSuccess entries) untouched. Mirrors PT_PAP's
-- success-screen-copy-update.sql style.
--
-- Looked up by program_type_key rather than a hardcoded workflow_key, so this applies
-- regardless of whether the live row is still 'WF_ENTRAINMENT_V8' or has since been renamed.
-- =====================================================

-- 1)-3)-4) PAYMENT stage — MAKE_PAYMENT success screens (ONLINE_PAYMENT_SUCCESS,
-- OFFLINE_PAYMENT_CHEQUE, OFFLINE_PAYMENT_OTHER, defaultScreen)
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(
                                            jsonb_set(
                                                oe,
                                                '{successScreenConfig,scenarios}',
                                                (
                                                    SELECT jsonb_agg(
                                                        CASE
                                                            WHEN sc->>'scenarioKey' = 'ONLINE_PAYMENT_SUCCESS' THEN
                                                                jsonb_set(
                                                                    jsonb_set(
                                                                        jsonb_set(
                                                                            sc,
                                                                            '{screen,title}',
                                                                            to_jsonb('Registration Confirmed!'::text)
                                                                        ),
                                                                        '{screen,message}',
                                                                        to_jsonb(E'Thank you {{seekerName}}. Your registration for a residential experience with Mahatria is confirmed.\n\nRegistration number is {{registrationNumber}}.\n\nLet this residential experience magnetise the best of everything in life.'::text)
                                                                    ),
                                                                    '{screen,actions}',
                                                                    '[
                                                                        {"actionKey": "NEW_REGISTRATION", "label": "New Registration", "type": "primary", "icon": "person_add", "apiKey": null, "condition": null},
                                                                        {"actionKey": "PROCEED_TO_TRAVEL", "label": "Go to Travel Plan", "type": "secondary", "icon": "flight", "apiKey": null, "condition": {"field": "involvesTravel", "operator": "EQUALS", "value": true}},
                                                                        {"actionKey": "HOME", "label": "Go to Home", "type": "secondary", "icon": "home", "apiKey": null, "condition": null}
                                                                    ]'::jsonb
                                                                )
                                                            WHEN sc->>'scenarioKey' IN ('OFFLINE_PAYMENT_CHEQUE', 'OFFLINE_PAYMENT_OTHER') THEN
                                                                jsonb_set(
                                                                    jsonb_set(
                                                                        jsonb_set(
                                                                            sc,
                                                                            '{screen,title}',
                                                                            to_jsonb('Registration Acknowledged!'::text)
                                                                        ),
                                                                        '{screen,message}',
                                                                        to_jsonb(E'We acknowledge your registration for a residential experience with Mahatria.\n\nYour provisional registration number is {{registrationNumber}}.\n\nNote: Payment is subject to realisation. A confirmation mail along with the invoice will follow upon payment realisation.\n\nLet this residential experience magnetise the best of everything in life.'::text)
                                                                    ),
                                                                    '{screen,actions}',
                                                                    '[
                                                                        {"actionKey": "NEW_REGISTRATION", "label": "New Registration", "type": "primary", "icon": "person_add", "apiKey": null, "condition": null},
                                                                        {"actionKey": "PROCEED_TO_TRAVEL", "label": "Go to Travel Plan", "type": "secondary", "icon": "flight", "apiKey": null, "condition": {"field": "involvesTravel", "operator": "EQUALS", "value": true}},
                                                                        {"actionKey": "HOME", "label": "Go to Home", "type": "secondary", "icon": "home", "apiKey": null, "condition": null}
                                                                    ]'::jsonb
                                                                )
                                                            ELSE sc
                                                        END
                                                    )
                                                    FROM jsonb_array_elements(oe->'successScreenConfig'->'scenarios') AS sc
                                                )
                                            ),
                                            '{successScreenConfig,defaultScreen}',
                                            jsonb_set(
                                                jsonb_set(
                                                    jsonb_set(
                                                        oe->'successScreenConfig'->'defaultScreen',
                                                        '{title}',
                                                        to_jsonb('Thank You!'::text)
                                                    ),
                                                    '{message}',
                                                    to_jsonb(E'In case of any assistance, please reach us on our Helpline: +91-9841660000 (Timings: 9:30 a.m. – 6:00 p.m. IST)\nEmail: events@infinitheism.com'::text)
                                                ),
                                                '{actions}',
                                                '[
                                                    {"actionKey": "NEW_REGISTRATION", "label": "New Registration", "type": "primary", "icon": "person_add", "apiKey": null, "condition": null},
                                                    {"actionKey": "HOME", "label": "Go to Home", "type": "secondary", "icon": "home", "apiKey": null, "condition": null}
                                                ]'::jsonb
                                            )
                                        )
                                    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 program_type_key = 'PT_ENTRAINMENT' AND deleted_at IS NULL
  );

-- 5) TRAVEL stage — SAVE_TRAVEL flat successScreen
UPDATE workflow_stage ws
SET action_buttons = (
        SELECT jsonb_agg(
            CASE
                WHEN elem->>'actionKey' = 'SAVE_TRAVEL' THEN
                    jsonb_set(
                        elem,
                        '{onSuccess}',
                        (
                            SELECT jsonb_agg(
                                CASE
                                    WHEN oe ? 'successScreen' THEN
                                        jsonb_set(
                                            oe,
                                            '{successScreen}',
                                            jsonb_set(
                                                jsonb_set(
                                                    jsonb_set(
                                                        oe->'successScreen',
                                                        '{title}',
                                                        to_jsonb('Thank You!'::text)
                                                    ),
                                                    '{message}',
                                                    to_jsonb(E'In case of any assistance, please reach us on our\n\nHelpline: +91-9841660000 (Timings: 9:30 a.m. – 6:00 p.m. IST)\n\nEmail: events@infinitheism.com'::text)
                                                ),
                                                '{actions}',
                                                '[
                                                    {"actionKey": "NEW_REGISTRATION", "label": "New Registration", "type": "primary", "icon": "person_add", "apiKey": null, "condition": null},
                                                    {"actionKey": "HOME", "label": "Go to Home", "type": "secondary", "icon": "home", "apiKey": null, "condition": null}
                                                ]'::jsonb
                                            )
                                        )
                                    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 = 'TRAVEL'
  AND ws.deleted_at IS NULL
  AND ws.workflow_config_id IN (
        SELECT id FROM workflow_configuration
        WHERE program_type_key = 'PT_ENTRAINMENT' AND deleted_at IS NULL
  );

-- =====================================================
-- VERIFICATION QUERY (commented out — uncomment to run manually after migration)
-- =====================================================
-- SELECT ws.stage_key, ws.action_buttons
-- FROM workflow_stage ws
-- JOIN workflow_configuration wc ON wc.id = ws.workflow_config_id
-- WHERE wc.program_type_key = 'PT_ENTRAINMENT' AND wc.deleted_at IS NULL
--   AND ws.stage_key IN ('PAYMENT', 'TRAVEL') AND ws.deleted_at IS NULL;
