flowmarkup:
  title: Order Processor (Version Migration)
  version: 3
  requires:
    SERVICES: [notifier, orders, payments, mailer]

  input:
    order_id: STRING
  output:
    status: STRING
    confirmation_code: STRING
  throws:
  - OrderNotFoundError
  - PaymentDeclinedError
  - VersionIncompatibleError
  examples:
    process_order:
      _notes_: Process an order and receive a confirmation code
      input:
        order_id: ORD-12345
      output:
        status: completed
        confirmation_code: CONF-ABC789
    another_order:
      _notes_: Another order processed successfully
      input:
        order_id: ORD-99001
      output:
        status: completed
        confirmation_code: CONF-XYZ456

  onVersionChange:
    # Log the migration for observability
  - log: "Migrating flow from v{{MIGRATION.OLD_VERSION}} to v{{MIGRATION.NEW_VERSION}} (checkpoint: {{MIGRATION.CHECKPOINTED_AT}})"

    # Gracefully terminate pre-v2 checkpoints — incompatible schema changes
  - if:
      condition: =MIGRATION.OLD_VERSION < 2
      then:
      - log: "='v' + MIGRATION.OLD_VERSION + ' too old — terminating instance'"
      - throw:
          error: VersionIncompatibleError
          message: "='v' + MIGRATION.OLD_VERSION + ' too old — incompatible schema changes'"

    # v2 → v3 migration: rename user_email → customer_email
  - if:
      condition: =has(user_email)
      then:
      - set:
          customer_email: =user_email
      - log: "Renamed user_email → customer_email (v2 → v3 migration)"

    # v2 → v3 migration: initialize new priority field with default
  - if:
      condition: =!has(priority)
      then:
      - set:
          priority: normal
  - if:
      condition: =!has(retry_count)
      then:
      - set:
          retry_count: 0
  - emit:
      event: flow_migrated
      data:
        location: =MIGRATION.LOCATION
        old_version: =MIGRATION.OLD_VERSION
        new_version: =MIGRATION.NEW_VERSION
  - log: "Migration notification emitted for {{MIGRATION.LOCATION}}"

  vars:
    customer_email: ""
    priority: normal
    retry_count: 0
    confirmation_code: ""

  do:
  - log: "Processing order {{order_id}} (priority: {{priority}})"

  - call:
      _id_: fetch_order
      service: =SERVICES.orders
      operation: find
      timeout: 30s
      params:
        order_id: =order_id
      result:
        order_data: =RESULT.order
        customer_email: =RESULT.email
      errors: [OrderNotFoundError, TimeoutError]
  - call:
      _id_: process_payment
      service: =SERVICES.payments
      operation: charge
      timeout: 30s
      params:
        order_id: =order_id
        amount: =order_data.total
        priority: =priority
      result:
        confirmation_code: =RESULT.code
      retry:
        maxAttempts: 3
        delay: 2s
        backoff: EXPONENTIAL
        onErrors: [TimeoutError]
      errors: [PaymentDeclinedError, TimeoutError]
  - call:
      _id_: send_confirmation
      service: =SERVICES.mailer
      operation: send
      timeout: 30s
      params:
        to: =customer_email
        template: order_confirmation
        data:
          order_id: =order_id
          confirmation_code: =confirmation_code

  - return: {status: completed, confirmation_code: =confirmation_code}
