# Plugin Metadata:
# Name: sam-event-mesh-gateway
# Version: 0.1.0
# Description: Solace Agent Mesh Gateway plugin for integrating with Solace event brokers.
# Author: SolaceLabs <solacelabs@solace.com>

log:
  stdout_log_level: INFO
  log_file_level: DEBUG
  log_file: em_gateway.log

!include ../shared_config.yaml


apps:
  - name: event-mesh-gateway-app
    app_module: sam_event_mesh_gateway.app
    broker:
      <<: *broker_connection

    app_config:
      namespace: "${NAMESPACE}" 
      gateway_id: "event-mesh-gw-01" # Unique ID for this gateway instance

      artifact_service: *default_artifact_service
      
      # --- Event Mesh Gateway Specific Parameters ---
      event_mesh_broker_config: # For the data plane Solace client
        broker_url: ${SOLACE_BROKER_URL} # Can be same or different from control plane
        broker_vpn: ${SOLACE_BROKER_VPN}
        broker_username: ${SOLACE_BROKER_USERNAME}
        broker_password: ${SOLACE_BROKER_PASSWORD}
        # Other data plane client settings (e.g., client_name, reconnection_strategy)

      ##############################
      # 1. UPDATE REQUIRED - START #
      ##############################
      
      event_handlers: # List of handlers for incoming Solace messages
        - name: "generic_json_event_handler"
          subscriptions:
            - topic: "acme/events/>"
              qos: 1
          input_expression: "template:Summarize this data: {{json://input.payload}}"
          payload_encoding: "utf-8"
          payload_format: "json"
          on_success: "success_response_handler"
          on_error: "error_response_handler"
          user_identity_expression: "input.user_properties:initiator_id" # Optional
          default_user_identity: "anonymous_event_mesh_user" # If no identity from event
          target_agent_name: "OrchestratorAgent" # Static target agent
          # target_agent_name_expression: "input.user_properties:target_agent" # Or dynamic
          forward_context: # Optional: Forward data from input to output handlers
            correlation_id: "input.user_properties:correlation_id" # Example: forward a correlation ID

        # Workflow handler - workflows use structured invocation automatically
        # Use target_workflow_name instead of target_agent_name for workflows
        - name: "order_workflow_handler"
          subscriptions:
            - topic: "acme/orders/>"
              qos: 1
          target_workflow_name: "OrderProcessingWorkflow"
          input_expression: "input.payload"
          payload_encoding: "utf-8"
          payload_format: "json"
          on_success: "workflow_success_handler"
          on_error: "error_response_handler"
          default_user_identity: "anonymous_event_mesh_user"
          forward_context:
            correlation_id: "input.user_properties:correlation_id"

        # Structured invocation to a regular agent (gateway provides schemas)
        # Use this when you want schema-validated input/output with a non-workflow agent
        - name: "structured_agent_handler"
          subscriptions:
            - topic: "acme/data/validate/>"
              qos: 1
          target_agent_name: "DataValidationAgent"
          structured_invocation:
            input_schema:
              type: object
              properties:
                data:
                  type: object
                  additionalProperties: true
              required: [data]
            output_schema:
              type: object
              properties:
                valid:
                  type: boolean
                errors:
                  type: array
              required: [valid]
          input_expression: "input.payload"
          payload_encoding: "utf-8"
          payload_format: "json"
          on_success: "workflow_success_handler"
          on_error: "error_response_handler"
          default_user_identity: "anonymous_event_mesh_user"
          forward_context:
            correlation_id: "input.user_properties:correlation_id"

        # Example of additional handlers - add more as needed for your use case
        # - name: "text_event_to_specific_agent"
        #   subscriptions:
        #     - topic: "external/systemB/events/text/>"
        #   input_expression: "template:User query from System B: {{text://input.payload}}"
        #   payload_encoding: "utf-8" # Or "none" if payload is already string
        #   payload_format: "text"
        #   on_success: "text_response_to_systemB"
        #   target_agent_name_expression: "static:TextAnalysisAgent" # Example of static via expression

      output_handlers: # List of handlers for publishing A2A responses
        - name: "success_response_handler"
          max_file_size_for_base64_bytes: 5242880 # 5MB limit for embedded files
          topic_expression: "template:acme/responses/{{text://user_data.forward_context:correlation_id}}"
          payload_expression: "task_response:text" # Use the text field from the response
          payload_encoding: "utf-8"
          payload_format: "text"

        - name: "error_response_handler"
          topic_expression: "template:acme/errors/{{text://user_data.forward_context:correlation_id}}"
          payload_expression: "task_response:a2a_task_response.error"
          payload_encoding: "utf-8"
          payload_format: "json"

        # Output handler for structured responses (workflows and structured agent invocations)
        - name: "workflow_success_handler"
          topic_expression: "template:acme/responses/{{text://user_data.forward_context:correlation_id}}"
          payload_expression: "input.payload:structured_output"  # Extract structured_output field
          payload_encoding: "utf-8"
          payload_format: "json"

      ############################
      # 1. UPDATE REQUIRED - END #
      ############################