-- =====================================================================================
-- ROW UPDATE — TAT TA26 (WF_TAT_TA26_V6) PAYMENT stage
-- Allow admin-released waitlisted seekers (reserved_link = true) to reach MAKE_PAYMENT.
-- -------------------------------------------------------------------------------------
-- Updates the existing workflow_stage row in place (does NOT touch the source INSERT).
-- The four ROLE_VIEWER gates (prerequisite, section create, section update, MAKE_PAYMENT)
-- now use  OR(registrationStatus NOT_IN [waitlisted], reservedLink = true)  instead of the
-- previous  {isWaitlisted = false} + {registrationStatus NOT_IN [waitlisted]}.
-- The three jsonb columns below mirror workflow-config-tat-ta26-insert.sql exactly.
--
-- Requires: the RESERVED_LINK workflow variable (alias 'reservedLink') in
--           workflow_variable_master; otherwise the field resolves to NULL and released
--           waitlisters stay blocked.
-- =====================================================================================

UPDATE workflow_stage
SET
  prerequisite_rules_config = '{
    "requiresAuth": true,
    "requiredStages": ["REGISTRATION"],
    "conditions": [
      {"roles": ["ROLE_VIEWER"], "condition": {"operator": "AND", "conditions": [
        {"field": "requiresPayment", "operator": "EQUALS", "value": true},
        {"field": "basicDetailsFilled", "operator": "EQUALS", "value": "completed"},
        {"operator": "OR", "conditions": [
          {"field": "registrationStatus", "operator": "NOT_IN", "value": ["waitlisted"]},
          {"field": "reservedLink", "operator": "EQUALS", "value": true}
        ]},
        {
          "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"}
              ]
            }
          ]
        }
      ]}, "message": "Basic details must be completed, not waitlisted, and payment must be pending or have an active edit request"},
      {"roles": ["ROLE_ADMIN", "ROLE_SHOBA", "ROLE_RELATIONAL_MANAGER"], "condition": null, "message": null}
    ]
  }'::jsonb,
  execution_rules_config = '{
    "sections": [
      {
        "sectionKey": "FS_PAYMENTINVOICE",
        "permissions": {
          "create": [{"roles": ["ROLE_VIEWER"], "condition": {"operator": "AND", "conditions": [
            {"field": "requiresPayment", "operator": "EQUALS", "value": true},
            {"field": "basicDetailsFilled", "operator": "EQUALS", "value": "completed"},
            {"operator": "OR", "conditions": [
              {"field": "registrationStatus", "operator": "NOT_IN", "value": ["waitlisted"]},
              {"field": "reservedLink", "operator": "EQUALS", "value": true}
            ]}
          ]}}],
          "read": [{"roles": ["ALL"], "condition": null}],
          "update": [
            {
              "roles": ["ROLE_VIEWER"],
              "condition": {
                "operator": "AND",
                "conditions": [
                  {"field": "requiresPayment", "operator": "EQUALS", "value": true},
                  {"field": "basicDetailsFilled", "operator": "EQUALS", "value": "completed"},
                  {"operator": "OR", "conditions": [
                    {"field": "registrationStatus", "operator": "NOT_IN", "value": ["waitlisted"]},
                    {"field": "reservedLink", "operator": "EQUALS", "value": true}
                  ]},
                  {
                    "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"}
                        ]
                      }
                    ]
                  }
                ]
              }
            },
            {"roles": ["ROLE_ADMIN", "ROLE_SHOBA", "ROLE_RELATIONAL_MANAGER"], "condition": null}
          ],
          "delete": [{"roles": [], "condition": null}]
        }
      }
    ]
  }'::jsonb,
  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"},
        {"operator": "OR", "conditions": [
          {"field": "registrationStatus", "operator": "NOT_IN", "value": ["waitlisted"]},
          {"field": "reservedLink", "operator": "EQUALS", "value": true}
        ]},
        {
          "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": true,
        "toast": {"message": "Payment of {{programFee}} submitted successfully!", "type": "success"},
        "showSuccessScreen": true,
        "successScreen": {
          "title": "Payment Completed!",
          "message": "Your payment for {{programName}} ({{programStartDate}} to {{programEndDate}}) has been completed successfully. Your registration is now complete. You will receive joining details for the online sessions via email.",
          "icon": "check_circle",
          "actions": [
            {"actionKey": "HOME", "label": "Go to Home", "type": "secondary", "icon": "home", "apiKey": null, "condition": null}
          ]
        }
      }]
    }
  ]'::jsonb,
  updated_by = -2,
  updated_at = NOW()
WHERE workflow_config_id = (SELECT id FROM workflow_configuration WHERE workflow_key = 'WF_TAT_TA26_V6')
  AND stage_key = 'PAYMENT';
