flowmarkup:
  title: Event Signaling Patterns
  version: 1
  description: "Demonstrates emit/wait-for patterns: parallel branch signaling, context propagation, and cross-workflow correlation."
  requires:
    CONTEXT: [correlation_id, trace_level]
    GLOBAL: { read: [request_count] }
    SERVICES: [data_loader, processor, child_processor]

  input:
    prompt:
      $kind: TEXT
      _notes_: Input prompt to process
    expected_order_id:
      $kind: STRING
      _notes_: Order ID to correlate payment events with

  events:
    data_loaded:
      _notes_: Emitted when the producer branch finishes loading data
      data:
        payload: ANY
    processing_complete:
      _notes_: Emitted after processing (CONTEXT scope)
      data:
        result: ANY
    payment_success:
      _notes_: External payment success event (GLOBAL scope)
      data:
        order_id: STRING
        tx_id: STRING
    payment_failed:
      _notes_: External payment failure event (GLOBAL scope)
      data:
        order_id: STRING
        reason: STRING
    payment_confirmed:
      _notes_: Payment confirmed event (CONTEXT scope)
    shipment_ready:
      _notes_: Shipment readiness event (CONTEXT scope)

  vars:
    loaded_data:
    received_data:
    transaction_id:
    failure_reason:

  do:

    # --- Pattern 1: Parallel branch signaling (LOCAL scope) ---
  - parallel:
      _label_: Produce and consume data in parallel
      _notes_: Producer loads data and signals consumer via self-scoped event
      producer:
      - call:
          service: data_loader
          operation: load
          timeout: 30s
          params:
            query: =prompt
          result:
            loaded_data: =RESULT.data
      - emit:
          event: data_loaded
          data:
            payload: =loaded_data
      consumer:
      - waitFor:
          _label_: Wait for data to be loaded
          event: data_loaded
          capture:
            received_data: =EVENT.DATA.payload
      - call:
          service: processor
          operation: process
          timeout: 30s
          params:
            data: =received_data

    # --- Pattern 2: Context propagation through sub-flows ---
  - set:
      CONTEXT.correlation_id: "txn-{{GLOBAL.request_count}}"
      CONTEXT.trace_level: debug
  - call:
      _label_: Call child flow (inherits CONTEXT.correlation_id)
      service: child_processor
      operation: process
      timeout: 30s
      params:
        data: =received_data
  - emit:
      event: processing_complete
      scope: CONTEXT
      data:
        result: =received_data

    # --- Pattern 3: Wait for EITHER event (OR) via race ---
  - race:
      _label_: Wait for payment success or failure
      success:
      - waitFor:
          event: payment_success
          scope: GLOBAL
          source: "services/payment-service.flowmarkup.yaml"
          timeout: 1h
          condition: =EVENT.DATA.order_id == expected_order_id
          capture:
            transaction_id: =EVENT.DATA.tx_id
      failure:
      - waitFor:
          event: payment_failed
          scope: GLOBAL
          source: "services/payment-service.flowmarkup.yaml"
          timeout: 1h
          condition: =EVENT.DATA.order_id == expected_order_id
          capture:
            failure_reason: =EVENT.DATA.reason

    # --- Pattern 4: Wait for BOTH events (AND) via parallel ---
  - parallel:
      _label_: Wait for both payment and shipment readiness
      payment:
      - waitFor:
          event: payment_confirmed
          scope: CONTEXT
          timeout: 1h
      shipment:
      - waitFor:
          event: shipment_ready
          scope: CONTEXT
          timeout: 1h

  - log: "All done. Transaction: {{transaction_id}}"
