# Demonstrates triggers: block — event-driven, cron, and schedule-based flow activation

flowmarkup:
  title: Inventory Sync
  version: 1
  description: >
    Syncs inventory levels from warehouse systems. Triggered by webhook events,
    scheduled cron runs, or human-readable schedules.
  requires:
    SERVICES: [warehouse, inventory]

  defaults:
    timeout: 30s

  triggers:
    # Event trigger — fires when a webhook delivers this event
  - event: stock_changed
    condition: "=EVENT.DATA.delta.abs() > 10"
    scope: GLOBAL

    # Cron trigger — standard cron expression
  - cron: "0 2 * * *"

    # Human-readable schedule variants
  - schedule: every 30m
  - schedule: daily at 08:00

  events:
    stock_changed:
      _notes_: Fired by warehouse system when stock levels change
      data:
        sku: STRING
        delta: INTEGER

  input:
    sku:
      $kind: STRING
      $nullable: true
      _notes_: Specific SKU to sync. Null when triggered by cron/schedule (syncs all).

  output:
    synced_count: INTEGER

  vars:
    synced: 0

  do:
  - if:
      condition: =sku != null
      then:
      - call:
          service: warehouse
          operation: get_stock
          timeout: 30s
          params: {sku: =sku}
          result: {current_level: =RESULT.quantity}
      - call:
          service: inventory
          operation: update
          timeout: 30s
          params: {sku: =sku, quantity: =current_level}
      - set: {synced: 1}
      else:
      - call:
          service: warehouse
          operation: list_all
          timeout: 30s
          result: {all_skus: =RESULT.items}
      - forEach:
          items: =all_skus
          as: item
          maxItems: 10000
          concurrent: true
          maxConcurrency: 20
          do:
          - call:
              service: inventory
              operation: update
              timeout: 30s
              params: {sku: =item.sku, quantity: =item.quantity}
      - set: {synced: =all_skus.size()}

  - return: {synced_count: =synced}
