{
  "apiVersion": 56,
  "label": "Credits Top-up — Auto-Assign Sequence Number",
  "description": "Before a new Top-up is committed, query siblings on the same parent (ordered by Topup_Sequence__c desc, first one only), assign this.Topup_Sequence__c = max(siblings) + 1 (first top-up = 1), then stamp Name = `Top-up #<sequence>` in the same before-save execution. Formula Resources are evaluated when referenced, so the Name formula observes the sequence assigned by the preceding element. No after-save self-update is required. Race condition on simultaneous creates is intentionally outside this latency fix (top-up volume ~10-15/year; see the issue acceptance scope).",
  "processType": "AutoLaunchedFlow",
  "status": "Active",
  "start": {
    "object": "Credits_Topup__c",
    "triggerType": "RecordBeforeSave",
    "recordTriggerType": "Create",
    "connector": {"targetReference": "Get_Max_Sibling_Sequence"}
  },
  "recordLookups": [
    {
      "name": "Get_Max_Sibling_Sequence",
      "label": "Get Max Sibling Sequence",
      "locationX": 200,
      "locationY": 100,
      "object": "Credits_Topup__c",
      "filterLogic": "and",
      "filters": [
        {"field": "Credits__c", "operator": "EqualTo", "value": {"elementReference": "$Record.Credits__c"}}
      ],
      "getFirstRecordOnly": true,
      "sortField": "Topup_Sequence__c",
      "sortOrder": "Desc",
      "storeOutputAutomatically": true,
      "assignNullValuesIfNoRecordsFound": true,
      "connector": {"targetReference": "Check_Has_Sibling"}
    }
  ],
  "decisions": [
    {
      "name": "Check_Has_Sibling",
      "label": "Sibling top-up exists on this parent?",
      "locationX": 200,
      "locationY": 200,
      "rules": [
        {
          "name": "has_sibling",
          "label": "Yes — increment from max sibling sequence",
          "conditionLogic": "and",
          "conditions": [
            {"leftValueReference": "Get_Max_Sibling_Sequence.Id", "operator": "IsNull", "rightValue": {"booleanValue": false}}
          ],
          "connector": {"targetReference": "Assign_Increment_Sequence"}
        }
      ],
      "defaultConnector": {"targetReference": "Assign_First_Sequence"},
      "defaultConnectorLabel": "No — first top-up, sequence = 1"
    }
  ],
  "formulas": [
    {
      "name": "Topup_Display_Name",
      "dataType": "String",
      "expression": "\"Top-up #\" & TEXT(ROUND(BLANKVALUE({!$Record.Topup_Sequence__c}, 1), 0))"
    }
  ],
  "assignments": [
    {
      "name": "Assign_Increment_Sequence",
      "label": "Sequence = max sibling + 1",
      "locationX": 100,
      "locationY": 300,
      "assignmentItems": [
        {"assignToReference": "$Record.Topup_Sequence__c", "operator": "Assign", "value": {"elementReference": "Get_Max_Sibling_Sequence.Topup_Sequence__c"}},
        {"assignToReference": "$Record.Topup_Sequence__c", "operator": "Add", "value": {"numberValue": 1}}
      ],
      "connector": {"targetReference": "Stamp_Display_Name"}
    },
    {
      "name": "Assign_First_Sequence",
      "label": "Sequence = 1 (no siblings)",
      "locationX": 300,
      "locationY": 300,
      "assignmentItems": [
        {"assignToReference": "$Record.Topup_Sequence__c", "operator": "Assign", "value": {"numberValue": 1}}
      ],
      "connector": {"targetReference": "Stamp_Display_Name"}
    },
    {
      "name": "Stamp_Display_Name",
      "label": "Name = Top-up #<sequence>",
      "locationX": 200,
      "locationY": 400,
      "assignmentItems": [
        {"assignToReference": "$Record.Name", "operator": "Assign", "value": {"elementReference": "Topup_Display_Name"}}
      ]
    }
  ]
}
