-- ================================================
-- UPDATE: TAT TA26 Payment Stage - Acknowledgement Screens
-- Updates action_buttons for PAYMENT stage to match HDB/MSD
-- successScreenConfig with multi-scenario acknowledgement screens
-- ================================================

UPDATE workflow_stage
SET
  action_buttons = '[
    {
      "actionKey": "CANCEL",
      "label": "Cancel",
      "type": "secondary",
      "icon": "close",
      "apiKey": null,
      "permissions": [{"roles": ["ROLE_ADMIN", "ROLE_SHOBA", "ROLE_RELATIONAL_MANAGER", "ROLE_VIEWER"], "condition": null}],
      "onSuccess": [{"roles": ["ROLE_ADMIN", "ROLE_SHOBA", "ROLE_RELATIONAL_MANAGER", "ROLE_VIEWER"], "showToast": false, "showSuccessScreen": false}]
    },
    {
      "actionKey": "MAKE_PAYMENT",
      "label": "submit",
      "type": "primary",
      "icon": "payment",
      "apiKey": "MAKE_PAYMENT",
      "permissions": [{"roles": ["ROLE_VIEWER"], "condition": {"operator": "AND", "conditions": [
        {"field": "requiresPayment", "operator": "EQUALS", "value": true},
        {"field": "basicDetailsFilled", "operator": "EQUALS", "value": "completed"},
        {"field": "isWaitlisted", "operator": "EQUALS", "value": false},
        {"field": "registrationStatus", "operator": "NOT_IN", "value": ["waitlisted"]},
        {
          "operator": "OR",
          "conditions": [
            {"field": "paymentStatus", "operator": "IN", "value": ["online_pending", "failed"]},
            {"field": "paymentStatus", "operator": "IS_NULL"},
            {
              "operator": "AND",
              "conditions": [
                {"field": "paymentStatus", "operator": "EQUALS", "value": "offline_pending"},
                {"field": "editRequestStatus", "operator": "EQUALS", "value": "requested"}
              ]
            }
          ]
        }
      ]}}],
      "onSuccess": [
        {
          "roles": ["ROLE_VIEWER"],
          "showToast": false,
          "showSuccessScreen": true,
          "successScreenConfig": {
            "scenarios": [
              {
                "scenarioKey": "PAYMENT_EDIT_REQUESTED",
                "condition": {
                  "operator": "AND",
                  "conditions": [
                    {"field": "paymentMode", "operator": "EQUALS", "value": "offline"},
                    {"field": "paymentStatus", "operator": "EQUALS", "value": "offline_pending"},
                    {"field": "editRequestStatus", "operator": "EQUALS", "value": "requested"}
                  ]
                },
                "screen": {
                  "title": "{{programName}}",
                  "message": "Dear infinitheist {{seekerName}},\n\nYour payment edit request for {{programName}} has been submitted.\n\nYou can edit your payment details once the request is approved.",
                  "variant": "info",
                  "showLogo": true,
                  "showBanner": true,
                  "logoUrl": "{{logoUrl}}",
                  "bannerImageUrl": "{{bannerImageUrl}}",
                  "actions": [
                    {
                      "actionKey": "HOME",
                      "label": "home",
                      "type": "secondary",
                      "apiKey": null
                    },
                    {
                      "actionKey": "UPDATE_PAYMENT",
                      "label": "update payment",
                      "type": "secondary",
                      "apiKey": "UPDATE_PAYMENT_DETAILS"
                    }
                  ]
                }
              },
              {
                "scenarioKey": "ONLINE_PAYMENT_SUCCESS",
                "condition": {
                  "operator": "AND",
                  "conditions": [
                    {"field": "paymentMode", "operator": "EQUALS", "value": "online"},
                    {"field": "paymentStatus", "operator": "IN", "value": ["online_completed", "completed"]}
                  ]
                },
                "screen": {
                  "title": "{{programName}}",
                  "message": "Dear infinitheist {{seekerName}},\n\nPayment towards {{programName}} - {{programStartDate}} to {{programEndDate}} is successful.\n\nYou will receive joining details for the online sessions via email.",
                  "variant": "success",
                  "showLogo": true,
                  "showBanner": true,
                  "logoUrl": "{{logoUrl}}",
                  "bannerImageUrl": "{{bannerImageUrl}}",
                  "actions": [
                    {
                      "actionKey": "DOWNLOAD_INVOICE",
                      "label": "download invoice",
                      "type": "secondary",
                      "apiKey": "DOWNLOAD_INVOICE"
                    },
                    {
                      "actionKey": "HOME",
                      "label": "home",
                      "type": "secondary",
                      "apiKey": null
                    }
                  ]
                }
              },
              {
                "scenarioKey": "OFFLINE_PAYMENT_CHEQUE",
                "condition": {
                  "operator": "AND",
                  "conditions": [
                    {"field": "paymentMode", "operator": "EQUALS", "value": "offline"},
                    {"field": "offlinePaymentMethod", "operator": "EQUALS", "value": "Cheque"}
                  ]
                },
                "screen": {
                  "title": "{{programName}}",
                  "message": "Dear infinitheist {{seekerName}},\n\nWe acknowledge your payment towards {{programName}} - {{programStartDate}} to {{programEndDate}}.\n\nNote: Cheques are subject to realization.",
                  "variant": "info",
                  "showLogo": true,
                  "showBanner": true,
                  "logoUrl": "{{logoUrl}}",
                  "bannerImageUrl": "{{bannerImageUrl}}",
                  "actions": [
                    {
                      "actionKey": "HOME",
                      "label": "home",
                      "type": "secondary",
                      "apiKey": null
                    }
                  ]
                }
              },
              {
                "scenarioKey": "OFFLINE_PAYMENT_OTHER",
                "condition": {
                  "operator": "AND",
                  "conditions": [
                    {"field": "paymentMode", "operator": "EQUALS", "value": "offline"},
                    {"field": "offlinePaymentMethod", "operator": "NOT_EQUALS", "value": "Cheque"}
                  ]
                },
                "screen": {
                  "title": "{{programName}}",
                  "message": "Dear infinitheist {{seekerName}},\n\nWe acknowledge your payment towards {{programName}} - {{programStartDate}} to {{programEndDate}}.",
                  "variant": "info",
                  "showLogo": true,
                  "showBanner": true,
                  "logoUrl": "{{logoUrl}}",
                  "bannerImageUrl": "{{bannerImageUrl}}",
                  "actions": [
                    {
                      "actionKey": "HOME",
                      "label": "home",
                      "type": "secondary",
                      "apiKey": null
                    }
                  ]
                }
              },
              {
                "scenarioKey": "FREE_SEAT_REGISTRATION",
                "condition": {
                  "operator": "AND",
                  "conditions": [
                    {"field": "isFreeSeat", "operator": "EQUALS", "value": true},
                    {"field": "registrationStatus", "operator": "NOT_EQUALS", "value": "cancelled"}
                  ]
                },
                "screen": {
                  "title": "Registration Successful!",
                  "message": "Dear infinitheist {{seekerName}},\n\nYour registration for {{programName}} is successful.\n\nRegistration Number: {{registrationNumber}}\nProgram Date: {{programStartDate}} to {{programEndDate}}",
                  "variant": "success",
                  "showLogo": true,
                  "showBanner": true,
                  "logoUrl": "{{logoUrl}}",
                  "bannerImageUrl": "{{bannerImageUrl}}",
                  "actions": [
                    {
                      "actionKey": "HOME",
                      "label": "home",
                      "type": "secondary",
                      "apiKey": null
                    },
                    {
                      "actionKey": "VIEW_SUMMARY",
                      "label": "summary",
                      "type": "secondary",
                      "apiKey": "VIEW_REGISTRATION_SUMMARY"
                    }
                  ]
                }
              }
            ],
            "defaultScreen": {
              "title": "Payment Submitted!",
              "message": "Dear infinitheist {{seekerName}},\n\nYour payment for {{programName}} has been submitted successfully.\n\nRegistration Number: {{registrationNumber}}",
              "variant": "success",
              "showLogo": true,
              "showBanner": true,
              "logoUrl": "{{logoUrl}}",
              "bannerImageUrl": "{{bannerImageUrl}}",
              "actions": [
                {
                  "actionKey": "HOME",
                  "label": "home",
                  "type": "secondary",
                  "apiKey": null
                }
              ]
            }
          }
        }
      ]
    }
  ]'::jsonb,
  updated_at = NOW()
WHERE workflow_config_id = (
    SELECT id FROM workflow_configuration WHERE workflow_key = 'WF_TAT_TA26_V6'
  )
  AND stage_key = 'PAYMENT';

-- ================================================
-- Link LOGO_URL and BANNER_IMAGE_URL variables
-- ================================================
DO $$
DECLARE
    v_workflow_id INT;
    v_var_id      INT;
    v_order       INT;
BEGIN
    SELECT id INTO v_workflow_id
    FROM workflow_configuration
    WHERE workflow_key = 'WF_TAT_TA26_V6'
    LIMIT 1;

    IF v_workflow_id IS NULL THEN
        RAISE NOTICE 'WF_TAT_TA26_V6 not found — skipping variable linking';
        RETURN;
    END IF;

    SELECT COALESCE(MAX(variable_order), 0) + 1 INTO v_order
    FROM workflow_configuration_variable
    WHERE workflow_config_id = v_workflow_id;

    FOR v_var_id IN
        SELECT id FROM workflow_variable_master
        WHERE variable_key IN ('LOGO_URL', 'BANNER_IMAGE_URL')
        ORDER BY variable_key
    LOOP
        INSERT INTO workflow_configuration_variable
            (workflow_config_id, variable_master_id, variable_order, is_required, created_by, updated_by)
        VALUES
            (v_workflow_id, v_var_id, v_order, false, -2, -2)
        ON CONFLICT DO NOTHING;
        v_order := v_order + 1;
    END LOOP;

    RAISE NOTICE 'Linked LOGO_URL and BANNER_IMAGE_URL to WF_TAT_TA26_V6 (ID: %)', v_workflow_id;
END $$;
