-- Insert workflow configuration
INSERT INTO workflow_configuration (
  workflow_key,
  workflow_name,
  program_type_key,
  program_type_id,
  workflow_version,
  lifecycle_status,
  description,
  data_query_template,
  registration_rules,
  initial_stage,
  created_by,
  updated_by,
  created_at,
  updated_at
) VALUES (
  'WF_HDBMSD_V8',
  'HDB/MSD Registration Workflow',
  'PT_HDBMSD',
  (SELECT id FROM program_type_v1 WHERE key = 'PT_HDBMSD'),
  '6.0.0',
  'ACTIVE',
  'HDB/MSD registration workflow with 4 core stages (Registration, Payment, Travel, Goodies) - No next_stages concept',
  NULL,
  '{
    "criteria": [
      {"field": "approvalStatus", "operator": "EQUALS", "value": "approved"},
      {
        "operator": "OR",
        "conditions": [
          {"field": "paymentStatus", "operator": "IN", "value": ["online_completed", "offline_completed", "completed"]},
          {"field": "isFreeSeat", "operator": "EQUALS", "value": true}
        ]
      },
      {"field": "travelInfoStatus", "operator": "EQUALS", "value": "completed"},
      {"field": "travelPlanStatus", "operator": "EQUALS", "value": "completed"}
    ],
    "message": "Registration is completed when all criteria are fulfilled."
  }'::jsonb,
  '{
    "stageKey": "REGISTRATION",
    "stageName": "Save as Draft",
    "isOptional": true,
    "description": "Save progress and resume later"
  }'::jsonb,
  -2,
  -2,
  '2026-02-12T00:00:00Z',
  '2026-02-12T00:00:00Z'
);

-- Stage 1: REGISTRATION
-- Role-based conditions moved to prerequisite_rules_config
INSERT INTO workflow_stage (
  workflow_config_id,
  stage_key,
  stage_name,
  stage_order,
  stage_category,
  is_optional,
  description,
  prerequisite_rules_config,
  execution_rules_config,
  action_buttons,
  completion_actions_config,
  created_by,
  updated_by,
  created_at,
  updated_at
) VALUES (
  (SELECT id FROM workflow_configuration WHERE workflow_key = 'WF_HDBMSD_V8'),
  'REGISTRATION',
  'Registration Submission',
  1,
  'FORM',
  false,
  'Seeker fills and submits registration form',
  '{
    "requiresAuth": true,
    "requiredStages": [],
    "conditions": [
      {
        "roles": ["ROLE_VIEWER"],
        "condition": {
          "operator": "OR",
          "conditions": [
            {"field": "approvalStatus", "operator": "EQUALS", "value": "pending"},
            {"field": "approvalStatus", "operator": "IS_NULL"},
            {"field": "basicDetailsFilled", "operator": "NOT_EQUALS", "value": "completed"},
            {"field": "basicDetailStatus", "operator": "EQUALS", "value": "draft"}
          ]
        },
        "message": "Viewers can access registration when it is in draft or pending approval"
      },
      {
        "roles": ["ROLE_ADMIN", "ROLE_SHOBA", "ROLE_RELATIONAL_MANAGER"],
        "condition": {
          "field": "basicDetailsFilled",
          "operator": "EQUALS",
          "value": "completed"
        },
        "message": "Admins can access submitted registrations"
      }
    ]
  }'::jsonb,
  '{
    "sections": [
      {
        "sectionKey": "FS_BASICDETAILS",
        "permissions": {
          "create": [{"roles": ["ROLE_VIEWER"], "condition": null}],
          "read": [{"roles": ["ALL"], "condition": null}],
          "update": [
            {
              "roles": ["ROLE_VIEWER"],
              "condition": {
                "operator": "OR",
                "conditions": [
                  {"field": "approvalStatus", "operator": "EQUALS", "value": "pending"},
                  {"field": "basicDetailsFilled", "operator": "EQUALS", "value": "draft"}
                ]
              }
            },
            {
              "roles": ["ROLE_ADMIN", "ROLE_SHOBA", "ROLE_RELATIONAL_MANAGER"],
              "condition": {"field": "basicDetailsFilled", "operator": "EQUALS", "value": "completed"}
            }
          ],
          "delete": [{"roles": [], "condition": null}]
        }
      },
      {
        "sectionKey": "FS_MAHATRIAQUESTIONS",
        "permissions": {
          "create": [{"roles": ["ROLE_VIEWER"], "condition": null}],
          "read": [{"roles": ["ROLE_VIEWER", "ROLE_MAHATRIA", "ROLE_SHOBA"], "condition": null}],
          "update": [
            {
              "roles": ["ROLE_VIEWER"],
              "condition": {
                "operator": "OR",
                "conditions": [
                  {"field": "approvalStatus", "operator": "EQUALS", "value": "pending"},
                  {"field": "basicDetailsFilled", "operator": "EQUALS", "value": "draft"}
                ]
              }
            },
            {
              "roles": ["ROLE_MAHATRIA", "ROLE_SHOBA"],
              "condition": {"field": "basicDetailsFilled", "operator": "EQUALS", "value": "completed"}
            }
          ],
          "delete": [{"roles": [], "condition": null}]
        }
      }
    ]
  }'::jsonb,
  '[
    {
      "actionKey": "CANCEL",
      "label": "Cancel",
      "type": "secondary",
      "icon": "save",
      "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": "SAVE_DRAFT",
      "label": "Save as Draft",
      "type": "secondary",
      "icon": "save",
      "apiKey": "SAVE_REGISTRATION_DRAFT",
      "permissions": [
        {
          "roles": ["ROLE_VIEWER"],
          "condition": {
            "operator": "AND",
            "conditions": [
              {"field": "allowsProxyRegistration", "operator": "EQUALS", "value": true},
              {
                "operator": "OR",
                "conditions": [
                  {"field": "registrationStatus", "operator": "EQUALS", "value": "draft"},
                  {"field": "registrationStatus", "operator": "EQUALS", "value": "save_as_draft"},
                  {"field": "basicDetailsFilled", "operator": "NOT_EQUALS", "value": "completed"}
                ]
              }
            ]
          }
        }
      ],
      "onSuccess": [
        {
          "roles": ["ROLE_VIEWER"],
          "showToast": true,
          "toast": {
            "message": "Draft saved successfully. You can continue later.",
            "type": "info"
          },
          "showSuccessScreen": false
        }
      ]
    },
    {
      "actionKey": "SUBMIT_REGISTRATION",
      "label": "Submit",
      "type": "primary",
      "icon": "send",
      "apiKey": "SUBMIT_REGISTRATION",
      "permissions": [
        {
          "roles": ["ROLE_VIEWER"],
          "condition": {
            "operator": "OR",
            "conditions": [
              {"field": "approvalStatus", "operator": "EQUALS", "value": "pending"},
              {"field": "approvalStatus", "operator": "IS_NULL"}
            ]
          }
        },
        {
          "roles": ["ROLE_ADMIN", "ROLE_SHOBA", "ROLE_RELATIONAL_MANAGER"],
          "condition": null
        }
      ],
      "onSuccess": [
        {
          "roles": ["ROLE_VIEWER"],
          "showToast": false,
          "showSuccessScreen": true,
          "successScreenConfig": {
            "scenarios": [
              {
                "scenarioKey": "REGISTRATION_SUBMITTED",
                "condition": {
                  "operator": "AND",
                  "conditions": [
                    {"field": "registrationStatus", "operator": "IN", "value": ["pending"]},
                    {"field": "approvalStatus", "operator": "IN", "value": ["pending", null]}
                  ]
                },
                "screen": {
                  "title": "{{programName}}",
                  "message": "Thanks for completing your registration.\nYour request has now been placed with Mahatria.\nYou can edit your registration until you are blessed with a seat.",
                  "variant": "success",
                  "showLogo": true,
                  "showBanner": true,
                  "logoUrl": "{{logoUrl}}",
                  "bannerImageUrl": "{{bannerImageUrl}}",
                  "actions": [
                    {
                      "actionKey": "HOME",
                      "label": "home",
                      "type": "secondary",
                      "apiKey": null
                    },
                    {
                      "actionKey": "EDIT_REGISTRATION",
                      "label": "edit form",
                      "type": "secondary",
                      "apiKey": "EDIT_REGISTRATION"
                    }
                  ]
                }
              }
            ],
            "defaultScreen": {
              "title": "{{programName}}",
              "message": "Thanks for completing your registration.\nYour request has now been placed with Mahatria.\nYou can edit your registration until you are blessed with a seat.",
              "variant": "success",
              "showLogo": true,
              "showBanner": true,
              "logoUrl": "{{logoUrl}}",
              "bannerImageUrl": "{{bannerImageUrl}}",
              "actions": [
                {
                  "actionKey": "HOME",
                  "label": "home",
                  "type": "secondary",
                  "apiKey": null
                },
                {
                  "actionKey": "EDIT_REGISTRATION",
                  "label": "edit form",
                  "type": "secondary",
                  "apiKey": "EDIT_REGISTRATION"
                }
              ]
            }
          }
        },
        {
          "roles": ["ROLE_ADMIN", "ROLE_SHOBA", "ROLE_RELATIONAL_MANAGER"],
          "showToast": false,
          "showSuccessScreen": false
        }
      ]
    }
  ]'::jsonb,
  '{
    "conditional": true,
    "conditions": [
      {
        "when": {
          "field": "registrationStatus",
          "operator": "IN",
          "value": ["draft", "save_as_draft"]
        },
        "then": {
          "stageStatus": "pending",
          "markStageComplete": false
        }
      },
      {
        "when": {
          "field": "registrationStatus",
          "operator": "IN",
          "value": ["submitted", "pending"]
        },
        "then": {
          "stageStatus": "completed",
          "markStageComplete": true
        }
      }
    ]
  }'::jsonb,
  -2,
  -2,
  '2026-02-12T00:00:00Z',
  '2026-02-12T00:00:00Z'
);

-- Stage 2: PAYMENT
INSERT INTO workflow_stage (
  workflow_config_id,
  stage_key,
  stage_name,
  stage_order,
  stage_category,
  is_optional,
  description,
  prerequisite_rules_config,
  execution_rules_config,
  action_buttons,
  completion_actions_config,
  created_by,
  updated_by,
  created_at,
  updated_at
) VALUES (
  (SELECT id FROM workflow_configuration WHERE workflow_key = 'WF_HDBMSD_V8'),
  'PAYMENT',
  'Payment',
  2,
  'FORM',
  false,
  'Process payment (online/offline)',
  '{
    "requiresAuth": true,
    "requiredStages": ["REGISTRATION"],
    "conditions": [
      {
        "roles": ["ROLE_VIEWER"],
        "condition": {
          "operator": "AND",
          "conditions": [
            {"field": "requiresPayment", "operator": "EQUALS", "value": true},
            {"field": "approvalStatus", "operator": "EQUALS", "value": "approved"},
            {"field": "isFreeSeat", "operator": "EQUALS", "value": false},
            {
              "operator": "OR",
              "conditions": [
                {"field": "paymentStatus", "operator": "IN", "value": ["failed", "online_pending"]},
                {"field": "paymentStatus", "operator": "IS_NULL"},
                {
                  "operator": "AND",
                  "conditions": [
                    {"field": "paymentStatus", "operator": "EQUALS", "value": "offline_pending"},
                    {"field": "editRequestStatus", "operator": "EQUALS", "value": "requested"}
                  ]
                }
              ]
            }
          ]
        },
        "message": "Payment required after registration approval"
      },
      {
        "roles": ["ROLE_FINANCE_MANAGER", "ROLE_ADMIN", "ROLE_RELATIONAL_MANAGER"],
        "condition": {
          "operator": "AND",
          "conditions": [
            {"field": "approvalStatus", "operator": "EQUALS", "value": "approved"},
            {"field": "isFreeSeat", "operator": "EQUALS", "value": false}
          ]
        },
        "message": "Finance team can access payment processing"
      }
    ]
  }'::jsonb,
  '{
    "sections": [
      {
        "sectionKey": "FS_PAYMENTINVOICE",
        "permissions": {
          "create": [
            {
              "roles": ["ROLE_VIEWER"],
              "condition": {
                "operator": "AND",
                "conditions": [
                  {"field": "requiresPayment", "operator": "EQUALS", "value": true},
                  {"field": "approvalStatus", "operator": "EQUALS", "value": "approved"},
                  {"field": "isFreeSeat", "operator": "EQUALS", "value": false},
                  {
                    "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_FINANCE_MANAGER", "ROLE_ADMIN", "ROLE_RELATIONAL_MANAGER"],
              "condition": {
                "operator": "AND",
                "conditions": [
                  {"field": "approvalStatus", "operator": "EQUALS", "value": "approved"},
                  {"field": "isFreeSeat", "operator": "EQUALS", "value": false}
                ]
              }
            }
          ],
          "read": [{"roles": ["ALL"], "condition": null}],
          "update": [
            {
              "roles": ["ROLE_VIEWER"],
              "condition": {
                "operator": "AND",
                "conditions": [
                  {"field": "requiresPayment", "operator": "EQUALS", "value": true},
                  {"field": "approvalStatus", "operator": "EQUALS", "value": "approved"},
                  {"field": "isFreeSeat", "operator": "EQUALS", "value": false},
                  {
                    "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_FINANCE_MANAGER", "ROLE_ADMIN", "ROLE_RELATIONAL_MANAGER"],
              "condition": {
                "operator": "AND",
                "conditions": [
                  {"field": "approvalStatus", "operator": "EQUALS", "value": "approved"},
                  {"field": "isFreeSeat", "operator": "EQUALS", "value": false}
                ]
              }
            }
          ],
          "delete": [{"roles": [], "condition": null}]
        }
      }
    ]
  }'::jsonb,
  '[
    {
      "actionKey": "CANCEL",
      "label": "Cancel",
      "type": "secondary",
      "icon": "close",
      "apiKey": null,
      "permissions": [
        {
          "roles": ["ROLE_ADMIN", "ROLE_FINANCE_MANAGER", "ROLE_RELATIONAL_MANAGER", "ROLE_VIEWER"],
          "condition": null
        }
      ],
      "onSuccess": [
        {
          "roles": ["ROLE_VIEWER"],
          "showToast": false,
          "showSuccessScreen": false
        }
      ]
    },
    {
      "actionKey": "MAKE_PAYMENT",
      "label": "submit",
      "type": "primary",
      "icon": null,
      "apiKey": "MAKE_PAYMENT",
      "permissions": [
        {
          "roles": ["ROLE_VIEWER"],
          "condition": {
            "operator": "AND",
            "conditions": [
              {"field": "requiresPayment", "operator": "EQUALS", "value": true},
              {"field": "approvalStatus", "operator": "EQUALS", "value": "approved"},
              {"field": "isFreeSeat", "operator": "EQUALS", "value": false},
              {
                "operator": "OR",
                "conditions": [
                  {"field": "paymentStatus", "operator": "IN", "value": ["failed", "online_pending"]},
                  {"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.",
                  "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,
  '{
    "conditional": true,
    "conditions": [
      {
        "when": {
          "operator": "OR",
          "conditions": [
            {"field": "paymentStatus", "operator": "IN", "value": ["online_completed", "offline_completed", "completed"]},
            {"field": "isFreeSeat", "operator": "EQUALS", "value": true}
          ]
        },
        "then": {
          "stageStatus": "completed",
          "markStageComplete": true
        }
      },
      {
        "when": {
          "operator": "AND",
          "conditions": [
            {"field": "paymentStatus", "operator": "IN", "value": ["online_pending", "offline_pending", "failed"]},
            {"field": "isFreeSeat", "operator": "EQUALS", "value": false}
          ]
        },
        "then": {
          "stageStatus": "pending",
          "markStageComplete": false
        }
      }
    ]
  }'::jsonb,
  -2,
  -2,
  '2026-02-12T00:00:00Z',
  '2026-02-12T00:00:00Z'
);

-- Stage 3: TRAVEL
INSERT INTO workflow_stage (
  workflow_config_id,
  stage_key,
  stage_name,
  stage_order,
  stage_category,
  is_optional,
  description,
  prerequisite_rules_config,
  execution_rules_config,
  action_buttons,
  completion_actions_config,
  created_by,
  updated_by,
  created_at,
  updated_at
) VALUES (
  (SELECT id FROM workflow_configuration WHERE workflow_key = 'WF_HDBMSD_V8'),
  'TRAVEL',
  'Travel Details',
  3,
  'FORM',
  false,
  'Complete travel information and plans',
  '{
    "requiresAuth": true,
    "requiredStages": ["REGISTRATION"],
    "conditions": [
      {
        "roles": ["ROLE_ADMIN", "ROLE_RELATIONAL_MANAGER", "ROLE_SHOBA"],
        "condition": {
          "operator": "AND",
          "conditions": [
            {"field": "involvesTravel", "operator": "EQUALS", "value": true},
            {"field": "approvalStatus", "operator": "EQUALS", "value": "approved"}
          ]
        },
        "message": "Registration must be approved to access travel details"
      }
    ]
  }'::jsonb,
  '{
    "sections": [
      {
        "sectionKey": "FS_TRAVELPLAN",
        "permissions": {
          "create": [
            {
              "roles": ["ROLE_ADMIN", "ROLE_RELATIONAL_MANAGER", "ROLE_SHOBA"],
              "condition": {
                "operator": "AND",
                "conditions": [
                  {"field": "involvesTravel", "operator": "EQUALS", "value": true},
                  {"field": "approvalStatus", "operator": "EQUALS", "value": "approved"}
                ]
              }
            }
          ],
          "read": [{"roles": ["ALL"], "condition": null}],
          "update": [
            {
              "roles": ["ROLE_ADMIN", "ROLE_RELATIONAL_MANAGER", "ROLE_SHOBA"],
              "condition": {
                "operator": "AND",
                "conditions": [
                  {"field": "involvesTravel", "operator": "EQUALS", "value": true},
                  {"field": "approvalStatus", "operator": "EQUALS", "value": "approved"}
                ]
              }
            }
          ],
          "delete": [{"roles": [], "condition": null}]
        }
      }
    ]
  }'::jsonb,
  '[
    {
      "actionKey": "CANCEL",
      "label": "Cancel",
      "type": "secondary",
      "icon": "close",
      "apiKey": null,
      "permissions": [
        {
          "roles": ["ROLE_ADMIN", "ROLE_FINANCE_MANAGER", "ROLE_RELATIONAL_MANAGER"],
          "condition": null
        }
      ],
      "onSuccess": [
        {
          "roles": ["ROLE_ADMIN", "ROLE_FINANCE_MANAGER", "ROLE_RELATIONAL_MANAGER"],
          "showToast": false,
          "showSuccessScreen": false
        }
      ]
    },
    {
      "actionKey": "SAVE_TRAVEL",
      "label": "Save Travel Details",
      "type": "primary",
      "icon": "save",
      "apiKey": "SAVE_TRAVEL",
      "permissions": [
        {
          "roles": ["ROLE_ADMIN", "ROLE_RELATIONAL_MANAGER", "ROLE_SHOBA"],
          "condition": {
            "operator": "AND",
            "conditions": [
              {"field": "involvesTravel", "operator": "EQUALS", "value": true},
              {"field": "approvalStatus", "operator": "EQUALS", "value": "approved"}
            ]
          }
        }
      ],
      "onSuccess": [
        {
          "roles": ["ROLE_ADMIN", "ROLE_RELATIONAL_MANAGER", "ROLE_SHOBA"],
          "showToast": false,
          "showSuccessScreen": true,
          "successScreenConfig": {
            "scenarios": [
              {
                "scenarioKey": "TRAVEL_DETAILS_SAVED",
                "condition": {
                  "operator": "AND",
                  "conditions": [
                    {"field": "travelInfoStatus", "operator": "EQUALS", "value": "completed"},
                    {"field": "travelPlanStatus", "operator": "EQUALS", "value": "completed"}
                  ]
                },
                "screen": {
                  "title": "Travel Details Saved!",
                  "message": "Dear infinitheist {{seekerName}},\n\nYour travel details for {{programName}} have been saved successfully.\n\nRegistration Number: {{registrationNumber}}\nProgram Date: {{programStartDate}} to {{programEndDate}}\nVenue: {{programVenue}}\n\nYou''re all set for the program!",
                  "variant": "success",
                  "showLogo": true,
                  "showBanner": true,
                  "logoUrl": "{{logoUrl}}",
                  "bannerImageUrl": "{{bannerImageUrl}}",
                  "actions": [
                    {
                      "actionKey": "VIEW_SUMMARY",
                      "label": "summary",
                      "type": "secondary",
                      "apiKey": "VIEW_REGISTRATION_SUMMARY"
                    },
                    {
                      "actionKey": "HOME",
                      "label": "home",
                      "type": "secondary",
                      "apiKey": null
                    }
                  ]
                }
              }
            ],
            "defaultScreen": {
              "title": "Travel Details Saved!",
              "message": "Dear infinitheist {{seekerName}},\n\nYour travel details have been saved 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,
  '{
    "conditional": true,
    "conditions": [
      {
        "when": {
          "operator": "AND",
          "conditions": [
            {"field": "travelInfoStatus", "operator": "EQUALS", "value": "completed"},
            {"field": "travelPlanStatus", "operator": "EQUALS", "value": "completed"}
          ]
        },
        "then": {
          "stageStatus": "completed",
          "markStageComplete": true
        }
      },
      {
        "when": {
          "operator": "OR",
          "conditions": [
            {"field": "travelInfoStatus", "operator": "EQUALS", "value": "completed"},
            {"field": "travelPlanStatus", "operator": "EQUALS", "value": "completed"}
          ]
        },
        "then": {
          "stageStatus": "pending",
          "markStageComplete": false
        }
      }
    ]
  }'::jsonb,
  -2,
  -2,
  '2026-02-12T00:00:00Z',
  '2026-02-12T00:00:00Z'
);

-- Stage 4: GOODIES
INSERT INTO workflow_stage (
  workflow_config_id,
  stage_key,
  stage_name,
  stage_order,
  stage_category,
  is_optional,
  description,
  prerequisite_rules_config,
  execution_rules_config,
  action_buttons,
  completion_actions_config,
  created_by,
  updated_by,
  created_at,
  updated_at
) VALUES (
  (SELECT id FROM workflow_configuration WHERE workflow_key = 'WF_HDBMSD_V8'),
  'GOODIES',
  'Goodies Selection',
  4,
  'FORM',
  true,
  'Select program goodies and merchandise',
  '{
    "requiresAuth": true,
    "requiredStages": ["REGISTRATION"],
    "conditions": [
      {
        "roles": ["ROLE_OPERATIONAL_MANGER", "ROLE_ADMIN", "ROLE_RELATIONAL_MANAGER", "ROLE_SHOBA"],
        "condition": {
          "operator": "AND",
          "conditions": [
            {"field": "hasGoodies", "operator": "EQUALS", "value": true},
            {"field": "approvalStatus", "operator": "EQUALS", "value": "approved"}
          ]
        },
        "message": "Registration must be approved to manage goodies"
      }
    ]
  }'::jsonb,
  '{
    "sections": [
      {
        "sectionKey": "FS_GOODIES",
        "permissions": {
          "create": [
            {
              "roles": ["ROLE_OPERATIONAL_MANGER", "ROLE_ADMIN", "ROLE_RELATIONAL_MANAGER", "ROLE_SHOBA"],
              "condition": {
                "operator": "AND",
                "conditions": [
                  {"field": "hasGoodies", "operator": "EQUALS", "value": true},
                  {"field": "approvalStatus", "operator": "EQUALS", "value": "approved"}
                ]
              }
            }
          ],
          "read": [{"roles": ["ALL"], "condition": null}],
          "update": [
            {
              "roles": ["ROLE_OPERATIONAL_MANGER", "ROLE_ADMIN", "ROLE_SHOBA", "ROLE_RELATIONAL_MANAGER"],
              "condition": {
                "operator": "AND",
                "conditions": [
                  {"field": "hasGoodies", "operator": "EQUALS", "value": true},
                  {"field": "approvalStatus", "operator": "EQUALS", "value": "approved"}
                ]
              }
            }
          ],
          "delete": [{"roles": [], "condition": null}]
        }
      }
    ]
  }'::jsonb,
  '[
    {
      "actionKey": "SAVE_GOODIES",
      "label": "submit",
      "type": "primary",
      "icon": "save",
      "apiKey": "SAVE_GOODIES",
      "permissions": [
        {
          "roles": ["ROLE_OPERATIONAL_MANGER", "ROLE_ADMIN", "ROLE_RELATIONAL_MANAGER", "ROLE_SHOBA"],
          "condition": {
            "operator": "AND",
            "conditions": [
              {"field": "hasGoodies", "operator": "EQUALS", "value": true},
              {"field": "approvalStatus", "operator": "EQUALS", "value": "approved"}
            ]
          }
        }
      ],
      "onSuccess": [
        {
          "roles": ["ROLE_OPERATIONAL_MANGER", "ROLE_ADMIN", "ROLE_RELATIONAL_MANAGER", "ROLE_SHOBA"],
          "showToast": false,
          "showSuccessScreen": true,
          "successScreenConfig": {
            "scenarios": [
              {
                "scenarioKey": "GOODIES_SELECTION_SAVED",
                "condition": {
                  "operator": "AND",
                  "conditions": [
                    {"field": "goodiesStatus", "operator": "EQUALS", "value": "completed"}
                  ]
                },
                "screen": {
                  "title": "Goodies Selection Saved!",
                  "message": "Dear infinitheist {{seekerName}},\n\nYour goodies selection for {{programName}} has been saved successfully.\n\nRegistration Number: {{registrationNumber}}\n\nYou''re all set!",
                  "variant": "success",
                  "showLogo": true,
                  "showBanner": true,
                  "logoUrl": "{{logoUrl}}",
                  "bannerImageUrl": "{{bannerImageUrl}}",
                  "actions": [
                    {
                      "actionKey": "VIEW_SUMMARY",
                      "label": "summary",
                      "type": "secondary",
                      "apiKey": "VIEW_REGISTRATION_SUMMARY"
                    },
                    {
                      "actionKey": "HOME",
                      "label": "home",
                      "type": "secondary",
                      "apiKey": null
                    }
                  ]
                }
              }
            ],
            "defaultScreen": {
              "title": "Goodies Selection Saved!",
              "message": "Dear infinitheist {{seekerName}},\n\nYour goodies selection has been saved 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,
  '{
    "conditional": true,
    "conditions": [
      {
        "when": {"field": "goodiesStatus", "operator": "EQUALS", "value": "completed"},
        "then": {"stageStatus": "completed", "markStageComplete": true}
      }
    ]
  }'::jsonb,
  -2,
  -2,
  '2026-02-12T00:00:00Z',
  '2026-02-12T00:00:00Z'
);


-- Insert workflow actions (Independent Actions)
-- Action 1: SEAT_APPROVAL
INSERT INTO workflow_action (
  workflow_config_id,
  action_key,
  action_name,
  description,
  permissions,
  endpoints,
  created_by,
  updated_by,
  created_at,
  updated_at
) VALUES (
  (SELECT id FROM workflow_configuration WHERE workflow_key = 'WF_HDBMSD_V8'),
  'SEAT_APPROVAL',
  'Seat Approval',
  'Mahatria/Shoba approves or rejects registration',
  '[
    {
      "roles": ["ROLE_MAHATRIA", "ROLE_SHOBA"],
      "condition": {
        "operator": "AND",
        "conditions": [
          {"field": "basicDetailsFilled", "operator": "EQUALS", "value": "completed"},
          {"field": "registrationStatus", "operator": "NOT_EQUALS", "value": "draft"},
          {"field": "registrationStatus", "operator": "NOT_EQUALS", "value": "save_as_draft"}
        ]
      }
    }
  ]'::jsonb,
  '[
    {
      "method": "PUT",
      "path": "/registration-approval/{approvalId}",
      "description": "Approve or reject registration"
    }
  ]'::jsonb,
  -2,
  -2,
  '2026-02-12T00:00:00Z',
  '2026-02-12T00:00:00Z'
);

-- Action 2: DEFAULTER_MARKING
INSERT INTO workflow_action (
  workflow_config_id,
  action_key,
  action_name,
  description,
  permissions,
  endpoints,
  created_by,
  updated_by,
  created_at,
  updated_at
) VALUES (
  (SELECT id FROM workflow_configuration WHERE workflow_key = 'WF_HDBMSD_V8'),
  'DEFAULTER_MARKING',
  'Defaulter Marking',
  'Mark seeker as defaulter',
  '[
    {
      "roles": ["ROLE_MAHATRIA", "ROLE_SHOBA", "ROLE_ADMIN"],
      "condition": {
        "operator": "AND",
        "conditions": [
          {"field": "registrationStatus", "operator": "NOT_EQUALS", "value": "draft"},
          {"field": "registrationStatus", "operator": "NOT_EQUALS", "value": "save_as_draft"},
          {"field": "basicDetailsFilled", "operator": "EQUALS", "value": "completed"},
          {"field": "isDefaulter", "operator": "EQUALS", "value": false}
        ]
      }
    }
  ]'::jsonb,
  '[
    {"method": "POST", "path": "/seeker-defaulter"},
    {"method": "PUT", "path": "/seeker-defaulter/{id}"}
  ]'::jsonb,
  -2,
  -2,
  '2026-02-12T00:00:00Z',
  '2026-02-12T00:00:00Z'
);

-- Action 3: UNMARK_DEFAULTER
INSERT INTO workflow_action (
  workflow_config_id,
  action_key,
  action_name,
  description,
  permissions,
  endpoints,
  created_by,
  updated_by,
  created_at,
  updated_at
) VALUES (
  (SELECT id FROM workflow_configuration WHERE workflow_key = 'WF_HDBMSD_V8'),
  'UNMARK_DEFAULTER',
  'Unmark Defaulter',
  'Remove defaulter marking from seeker',
  '[
    {
      "roles": ["ROLE_MAHATRIA", "ROLE_SHOBA", "ROLE_ADMIN"],
      "condition": {
        "operator": "AND",
        "conditions": [
          {"field": "registrationStatus", "operator": "NOT_EQUALS", "value": "draft"},
          {"field": "registrationStatus", "operator": "NOT_EQUALS", "value": "save_as_draft"},
          {"field": "basicDetailsFilled", "operator": "EQUALS", "value": "completed"},
          {"field": "isDefaulter", "operator": "EQUALS", "value": true}
        ]
      }
    }
  ]'::jsonb,
  '[
    {"method": "PUT", "path": "/seeker-defaulter/{id}"}
  ]'::jsonb,
  -2,
  -2,
  '2026-02-12T00:00:00Z',
  '2026-02-12T00:00:00Z'
);

-- Action 4: SEND_INVOICE
INSERT INTO workflow_action (
  workflow_config_id,
  action_key,
  action_name,
  description,
  permissions,
  endpoints,
  created_by,
  updated_by,
  created_at,
  updated_at
) VALUES (
  (SELECT id FROM workflow_configuration WHERE workflow_key = 'WF_HDBMSD_V8'),
  'SEND_INVOICE',
  'Send Invoice',
  'Send invoice to seeker via email',
  '[
    {
      "roles": ["ROLE_MAHATRIA", "ROLE_SHOBA", "ROLE_ADMIN", "ROLE_FINANCE_MANAGER", "ROLE_RELATIONAL_MANAGER"],
      "condition": {
        "operator": "AND",
        "conditions": [
          {"field": "paymentStatus", "operator": "IN", "value": ["offline_completed", "online_completed", "completed"]},
          {"field": "approvalStatus", "operator": "EQUALS", "value": "approved"}
        ]
      }
    }
  ]'::jsonb,
  '[
    {"method": "POST", "path": "/invoice/send/{registrationId}", "description": "Send invoice email to seeker"}
  ]'::jsonb,
  -2,
  -2,
  '2026-02-12T00:00:00Z',
  '2026-02-12T00:00:00Z'
);

-- Action 5: DOWNLOAD_INVOICE
INSERT INTO workflow_action (
  workflow_config_id,
  action_key,
  action_name,
  description,
  permissions,
  endpoints,
  created_by,
  updated_by,
  created_at,
  updated_at
) VALUES (
  (SELECT id FROM workflow_configuration WHERE workflow_key = 'WF_HDBMSD_V8'),
  'DOWNLOAD_INVOICE',
  'Download Invoice',
  'Download invoice PDF',
  '[
    {
      "roles": ["ROLE_MAHATRIA", "ROLE_SHOBA", "ROLE_ADMIN", "ROLE_FINANCE_MANAGER", "ROLE_RELATIONAL_MANAGER"],
      "condition": {
        "operator": "AND",
        "conditions": [
          {"field": "paymentStatus", "operator": "IN", "value": ["offline_completed", "online_completed", "completed"]},
          {"field": "invoiceGenerated", "operator": "EQUALS", "value": true},
          {"field": "approvalStatus", "operator": "EQUALS", "value": "approved"}
        ]
      }
    }
  ]'::jsonb,
  '[
    {"method": "GET", "path": "/invoice/download/{registrationId}", "description": "Download invoice PDF"}
  ]'::jsonb,
  -2,
  -2,
  '2026-02-12T00:00:00Z',
  '2026-02-12T00:00:00Z'
);

-- Action 6: DOWNLOAD_PROFORMA_INVOICE
INSERT INTO workflow_action (
  workflow_config_id,
  action_key,
  action_name,
  description,
  permissions,
  endpoints,
  created_by,
  updated_by,
  created_at,
  updated_at
) VALUES (
  (SELECT id FROM workflow_configuration WHERE workflow_key = 'WF_HDBMSD_V8'),
  'DOWNLOAD_PROFORMA_INVOICE',
  'Download Proforma Invoice',
  'Download proforma invoice PDF',
  '[
    {
      "roles": ["ROLE_MAHATRIA", "ROLE_SHOBA", "ROLE_ADMIN", "ROLE_FINANCE_MANAGER", "ROLE_RELATIONAL_MANAGER"],
      "condition": {
        "operator": "AND",
        "conditions": [
          {"field": "proFormaInvoicePdfUrl", "operator": "IS_NOT_NULL"},
          {"field": "approvalStatus", "operator": "EQUALS", "value": "approved"}
        ]
      }
    }
  ]'::jsonb,
  '[
    {"method": "GET", "path": "/invoice/proforma/{registrationId}", "description": "Download proforma invoice PDF"}
  ]'::jsonb,
  -2,
  -2,
  '2026-02-12T00:00:00Z',
  '2026-02-12T00:00:00Z'
);

-- Action 7: RESEND_BLESSED_EMAIL
INSERT INTO workflow_action (
  workflow_config_id,
  action_key,
  action_name,
  description,
  permissions,
  endpoints,
  created_by,
  updated_by,
  created_at,
  updated_at
) VALUES (
  (SELECT id FROM workflow_configuration WHERE workflow_key = 'WF_HDBMSD_V8'),
  'RESEND_BLESSED_EMAIL',
  'Resend Blessed Email',
  'Resend approval/blessed email to seeker',
  '[
    {
      "roles": ["ROLE_SHOBA"],
      "condition": {
        "operator": "AND",
        "conditions": [
          {"field": "approvalStatus", "operator": "EQUALS", "value": "approved"}
        ]
      }
    }
  ]'::jsonb,
  '[
    {"method": "POST", "path": "/registration/{registrationId}/resend-blessed-email", "description": "Resend blessed email notification"}
  ]'::jsonb,
  -2,
  -2,
  '2026-02-12T00:00:00Z',
  '2026-02-12T00:00:00Z'
);

-- Action 8: MARK_PAYMENT_COMPLETED
INSERT INTO workflow_action (
  workflow_config_id,
  action_key,
  action_name,
  description,
  permissions,
  endpoints,
  created_by,
  updated_by,
  created_at,
  updated_at
) VALUES (
  (SELECT id FROM workflow_configuration WHERE workflow_key = 'WF_HDBMSD_V8'),
  'MARK_PAYMENT_COMPLETED',
  'Mark Payment as Completed',
  'Mark offline/online pending payment as completed',
  '[
    {
      "roles": ["ROLE_FINANCE_MANAGER"],
      "condition": {
        "operator": "AND",
        "conditions": [
          {"field": "approvalStatus", "operator": "EQUALS", "value": "approved"},
          {"field": "paymentStatus", "operator": "IN", "value": ["offline_pending", "online_pending", "failed"]}
        ]
      }
    }
  ]'::jsonb,
  '[
    {"method": "PUT", "path": "/payment/mark-completed/{registrationId}", "description": "Mark payment as completed by finance team"}
  ]'::jsonb,
  -2,
  -2,
  '2026-02-12T00:00:00Z',
  '2026-02-12T00:00:00Z'
);

-- Action 9: REGISTRATION_HOLD
INSERT INTO workflow_action (
  workflow_config_id,
  action_key,
  action_name,
  description,
  permissions,
  endpoints,
  created_by,
  updated_by,
  created_at,
  updated_at
) VALUES (
  (SELECT id FROM workflow_configuration WHERE workflow_key = 'WF_HDBMSD_V8'),
  'REGISTRATION_HOLD',
  'Registration Hold',
  'Move registration to hold/rejected state',
  '[
    {
      "roles": ["ROLE_MAHATRIA", "ROLE_SHOBA", "ROLE_ADMIN"],
      "condition": {
        "operator": "AND",
        "conditions": [
          {"field": "basicDetailsFilled", "operator": "EQUALS", "value": "completed"},
          {"field": "approvalStatus", "operator": "IN", "value": ["approved", "pending", "on_hold"]}
        ]
      }
    }
  ]'::jsonb,
  '[
    {"method": "PUT", "path": "/registration/{id}/hold", "description": "Update registration hold status"}
  ]'::jsonb,
  -2,
  -2,
  '2026-02-12T00:00:00Z',
  '2026-02-12T00:00:00Z'
);

-- Action 10: REGISTRATION_CANCEL
INSERT INTO workflow_action (
  workflow_config_id,
  action_key,
  action_name,
  description,
  permissions,
  endpoints,
  created_by,
  updated_by,
  created_at,
  updated_at
) VALUES (
  (SELECT id FROM workflow_configuration WHERE workflow_key = 'WF_HDBMSD_V8'),
  'REGISTRATION_CANCEL',
  'Cancel Registration',
  'Cancel registration from hold state',
  '[
    {
      "roles": ["ROLE_MAHATRIA", "ROLE_SHOBA", "ROLE_RELATIONAL_MANAGER", "ROLE_ADMIN"],
      "condition": {
        "operator": "AND",
        "conditions": [
          {"field": "registrationStatus", "operator": "EQUALS", "value": "rejected"},
          {"field": "basicDetailsFilled", "operator": "EQUALS", "value": "completed"}
        ]
      }
    }
  ]'::jsonb,
  '[
    {"method": "PUT", "path": "/registration/{id}/cancel", "description": "Cancel registration from hold state"}
  ]'::jsonb,
  -2,
  -2,
  '2026-02-12T00:00:00Z',
  '2026-02-12T00:00:00Z'
);

-- Action 11: SWAP_CREATE
INSERT INTO workflow_action (
  workflow_config_id,
  action_key,
  action_name,
  description,
  permissions,
  endpoints,
  created_by,
  updated_by,
  created_at,
  updated_at
) VALUES (
  (SELECT id FROM workflow_configuration WHERE workflow_key = 'WF_HDBMSD_V8'),
  'SWAP_CREATE',
  'Create Swap Request',
  'Create swap request to move to different program',
  '[
    {
      "roles": ["ROLE_RELATIONAL_MANAGER", "ROLE_SHOBA", "ROLE_ADMIN"],
      "condition": {
        "operator": "AND",
        "conditions": [
          {"field": "approvalStatus", "operator": "EQUALS", "value": "approved"},
          {"field": "hasActiveSwap", "operator": "EQUALS", "value": false}
        ]
      }
    }
  ]'::jsonb,
  '[
    {"method": "POST", "path": "/program-registration/swap", "description": "Create swap request (wants_swap, can_shift, or direct_swap)"}
  ]'::jsonb,
  -2,
  -2,
  '2026-02-12T00:00:00Z',
  '2026-02-12T00:00:00Z'
);

-- Action 12: SWAP_APPROVE
INSERT INTO workflow_action (
  workflow_config_id,
  action_key,
  action_name,
  description,
  permissions,
  endpoints,
  created_by,
  updated_by,
  created_at,
  updated_at
) VALUES (
  (SELECT id FROM workflow_configuration WHERE workflow_key = 'WF_HDBMSD_V8'),
  'SWAP_APPROVE',
  'Approve Swap',
  'Approve swap and allocate to new program',
  '[
    {
      "roles": ["ROLE_SHOBA", "ROLE_ADMIN"],
      "condition": {
        "operator": "AND",
        "conditions": [
          {"field": "hasActiveSwap", "operator": "EQUALS", "value": true},
          {"field": "swapStatus", "operator": "EQUALS", "value": "active"}
        ]
      }
    }
  ]'::jsonb,
  '[
    {"method": "PUT", "path": "/program-registration/swap/{swapId}", "description": "Approve swap and allocate to new program"}
  ]'::jsonb,
  -2,
  -2,
  '2026-02-12T00:00:00Z',
  '2026-02-12T00:00:00Z'
);

-- Action 13: SWAP_REJECT
INSERT INTO workflow_action (
  workflow_config_id,
  action_key,
  action_name,
  description,
  permissions,
  endpoints,
  created_by,
  updated_by,
  created_at,
  updated_at
) VALUES (
  (SELECT id FROM workflow_configuration WHERE workflow_key = 'WF_HDBMSD_V8'),
  'SWAP_REJECT',
  'Reject Swap',
  'Reject swap request and move to hold',
  '[
    {
      "roles": ["ROLE_SHOBA", "ROLE_ADMIN"],
      "condition": {
        "operator": "AND",
        "conditions": [
          {"field": "hasActiveSwap", "operator": "EQUALS", "value": true},
          {"field": "swapStatus", "operator": "EQUALS", "value": "active"}
        ]
      }
    }
  ]'::jsonb,
  '[
    {"method": "PUT", "path": "/program-registration/swap/{swapId}", "description": "Reject swap request and move to hold"}
  ]'::jsonb,
  -2,
  -2,
  '2026-02-12T00:00:00Z',
  '2026-02-12T00:00:00Z'
);

-- Action 14: SWAP_UPDATE
INSERT INTO workflow_action (
  workflow_config_id,
  action_key,
  action_name,
  description,
  permissions,
  endpoints,
  created_by,
  updated_by,
  created_at,
  updated_at
) VALUES (
  (SELECT id FROM workflow_configuration WHERE workflow_key = 'WF_HDBMSD_V8'),
  'SWAP_UPDATE',
  'Update Swap Request',
  'Update active swap request details',
  '[
    {
      "roles": ["ROLE_RELATIONAL_MANAGER", "ROLE_SHOBA", "ROLE_ADMIN"],
      "condition": {
        "operator": "AND",
        "conditions": [
          {"field": "hasActiveSwap", "operator": "EQUALS", "value": true},
          {"field": "swapStatus", "operator": "EQUALS", "value": "active"}
        ]
      }
    }
  ]'::jsonb,
  '[
    {"method": "PUT", "path": "/program-registration/swap/{swapId}", "description": "Update swap request details"}
  ]'::jsonb,
  -2,
  -2,
  '2026-02-12T00:00:00Z',
  '2026-02-12T00:00:00Z'
);

-- Action 15: SWAP_CANCEL
INSERT INTO workflow_action (
  workflow_config_id,
  action_key,
  action_name,
  description,
  permissions,
  endpoints,
  created_by,
  updated_by,
  created_at,
  updated_at
) VALUES (
  (SELECT id FROM workflow_configuration WHERE workflow_key = 'WF_HDBMSD_V8'),
  'SWAP_CANCEL',
  'Cancel Swap Request',
  'Cancel active swap request',
  '[
    {
      "roles": ["ROLE_RELATIONAL_MANAGER", "ROLE_SHOBA", "ROLE_ADMIN"],
      "condition": {
        "operator": "AND",
        "conditions": [
          {"field": "hasActiveSwap", "operator": "EQUALS", "value": true},
          {"field": "swapStatus", "operator": "EQUALS", "value": "active"}
        ]
      }
    }
  ]'::jsonb,
  '[
    {"method": "PUT", "path": "/program-registration/swap/{swapId}/cancel", "description": "Cancel active swap request"}
  ]'::jsonb,
  -2,
  -2,
  '2026-02-12T00:00:00Z',
  '2026-02-12T00:00:00Z'
);

-- Action 16: SWAP_MOVE_TO_DEMAND
INSERT INTO workflow_action (
  workflow_config_id,
  action_key,
  action_name,
  description,
  permissions,
  endpoints,
  created_by,
  updated_by,
  created_at,
  updated_at
) VALUES (
  (SELECT id FROM workflow_configuration WHERE workflow_key = 'WF_HDBMSD_V8'),
  'SWAP_MOVE_TO_DEMAND',
  'Move Swap to Demand Pool',
  'Move wants_swap to demand pool',
  '[
    {
      "roles": ["ROLE_RELATIONAL_MANAGER", "ROLE_SHOBA", "ROLE_ADMIN"],
      "condition": {
        "operator": "AND",
        "conditions": [
          {"field": "hasActiveSwap", "operator": "EQUALS", "value": true},
          {"field": "swapType", "operator": "EQUALS", "value": "wants_swap"},
          {"field": "swapStatus", "operator": "EQUALS", "value": "active"}
        ]
      }
    }
  ]'::jsonb,
  '[
    {"method": "PUT", "path": "/program-registration/swap/{swapId}", "description": "Move wants_swap to demand pool"}
  ]'::jsonb,
  -2,
  -2,
  '2026-02-12T00:00:00Z',
  '2026-02-12T00:00:00Z'
);

-- Action 17: RM_REVIEW
INSERT INTO workflow_action (
  workflow_config_id,
  action_key,
  action_name,
  description,
  permissions,
  endpoints,
  created_by,
  updated_by,
  created_at,
  updated_at
) VALUES (
  (SELECT id FROM workflow_configuration WHERE workflow_key = 'WF_HDBMSD_V8'),
  'RM_REVIEW',
  'RM Review',
  'Relational Manager review and rating',
  '[
    {
      "roles": ["ROLE_RELATIONAL_MANAGER", "ROLE_SHOBA", "ROLE_ADMIN"],
      "condition": {
        "operator": "AND",
        "conditions": [
          {"field": "registrationStatus", "operator": "NOT_EQUALS", "value": "draft"},
          {"field": "registrationStatus", "operator": "NOT_EQUALS", "value": "save_as_draft"},
          {"field": "basicDetailsFilled", "operator": "EQUALS", "value": "completed"}
        ]
      }
    }
  ]'::jsonb,
  '[
    {"method": "POST", "path": "/program-registration/{programRegistrationId}/rm-rating"},
    {"method": "PUT", "path": "/program-registration/{programRegistrationId}/rm-rating"}
  ]'::jsonb,
  -2,
  -2,
  '2026-02-12T00:00:00Z',
  '2026-02-12T00:00:00Z'
);

-- Action 18: RM_RECOMMENDATION
INSERT INTO workflow_action (
  workflow_config_id,
  action_key,
  action_name,
  description,
  permissions,
  endpoints,
  created_by,
  updated_by,
  created_at,
  updated_at
) VALUES (
  (SELECT id FROM workflow_configuration WHERE workflow_key = 'WF_HDBMSD_V8'),
  'RM_RECOMMENDATION',
  'RM Recommendation',
  'Relational Manager recommendation',
  '[
    {
      "roles": ["ROLE_RELATIONAL_MANAGER", "ROLE_SHOBA", "ROLE_ADMIN"],
      "condition": {
        "operator": "AND",
        "conditions": [
          {"field": "registrationStatus", "operator": "NOT_EQUALS", "value": "draft"},
          {"field": "registrationStatus", "operator": "NOT_EQUALS", "value": "save_as_draft"},
          {"field": "basicDetailsFilled", "operator": "EQUALS", "value": "completed"}
        ]
      }
    }
  ]'::jsonb,
  '[
    {"method": "POST", "path": "/program-registration/{programRegistrationId}/recommendation"},
    {"method": "PUT", "path": "/program-registration/{programRegistrationId}/recommendation"}
  ]'::jsonb,
  -2,
  -2,
  '2026-02-12T00:00:00Z',
  '2026-02-12T00:00:00Z'
);

-- Action 19: PROGRAM_EXPERIENCE
INSERT INTO workflow_action (
  workflow_config_id,
  action_key,
  action_name,
  description,
  permissions,
  endpoints,
  created_by,
  updated_by,
  created_at,
  updated_at
) VALUES (
  (SELECT id FROM workflow_configuration WHERE workflow_key = 'WF_HDBMSD_V8'),
  'PROGRAM_EXPERIENCE',
  'Program Experience',
  'Manage seeker program experience',
  '[
    {
      "roles": ["ROLE_RELATIONAL_MANAGER", "ROLE_SHOBA", "ROLE_ADMIN"],
      "condition": {
        "operator": "AND",
        "conditions": [
          {"field": "registrationStatus", "operator": "NOT_EQUALS", "value": "draft"},
          {"field": "registrationStatus", "operator": "NOT_EQUALS", "value": "save_as_draft"},
          {"field": "basicDetailsFilled", "operator": "EQUALS", "value": "completed"}
        ]
      }
    }
  ]'::jsonb,
  '[
    {"method": "POST", "path": "/seeker-program-experience"},
    {"method": "PUT", "path": "/seeker-program-experience/{id}"},
    {"method": "GET", "path": "/seeker-program-experience/seeker/{seekerId}"}
  ]'::jsonb,
  -2,
  -2,
  '2026-02-12T00:00:00Z',
  '2026-02-12T00:00:00Z'
);

-- ================================================
-- Link Variables to HDB/MSD Workflow
-- ================================================
DO $$
DECLARE
    v_workflow_id INT;
    v_var_id      INT;
    v_order       INT := 1;
BEGIN
    SELECT id INTO v_workflow_id
    FROM workflow_configuration
    WHERE workflow_key = 'WF_HDBMSD_V8'
    LIMIT 1;

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

    FOR v_var_id IN
        SELECT id FROM workflow_variable_master
        WHERE variable_key IN (
            'REGISTRATION_ID',
            'USER_ID',
            'PROGRAM_ID',
            'REGISTRATION_STATUS',
            'BASIC_DETAILS_STATUS',
            'APPROVAL_STATUS',
            'PAYMENT_STATUS',
            'PAYMENT_MODE',
            'EDIT_REQUEST_STATUS',
            'TRAVEL_INFO_STATUS',
            'TRAVEL_PLAN_STATUS',
            'GOODIES_STATUS',
            'PRO_FORMA_INVOICE_PDF_URL',
            'IS_FREE_SEAT_COMPUTED',
            'IS_WAITLISTED',
            'WAITLIST_POSITION',
            'INVOICE_GENERATED',
            'INVOICE_PDF_URL',
            'INVOICE_STATUS',
            'REGISTRATION_NUMBER',
            'SEEKER_NAME',
            'SEEKER_EMAIL',
            'SEEKER_PHONE',
            'PROGRAM_FEE',
            'PROGRAM_NAME',
            'PROGRAM_START_DATE',
            'PROGRAM_END_DATE',
            'PROGRAM_VENUE',
            -- Program flag variables (used in stage/button conditions)
            'PROGRAM_REQUIRES_PAYMENT',
            'PROGRAM_INVOLVES_TRAVEL',
            'PROGRAM_HAS_GOODIES',
            'PROGRAM_REQUIRES_APPROVAL',
            'PROGRAM_ALLOWS_PROXY_REGISTRATION',
            'PROGRAM_HAS_TERMS'
        )
        ORDER BY execution_order, 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 % variables to WF_HDBMSD_V8 (ID: %)', v_order - 1, v_workflow_id;
END $$;


update program_v1
set workflow_id = (select id from workflow_configuration where workflow_key = 'WF_HDBMSD_V8')
where program_type_id = (SELECT id FROM program_type_v1 WHERE key = 'PT_HDBMSD');