# Example Configuration for the Universal Webhook Gateway
#
# This file demonstrates how to set up the WebhookGatewayApp and configure
# various webhook endpoints with different authentication methods, payload formats,
# and artifact saving options.
#
# Required Environment Variables (if not using defaults):
# - NAMESPACE: The A2A topic namespace (e.g., "myorg/dev").
# - SOLACE_BROKER_URL: URL of the Solace broker (e.g., "ws://localhost:8080").
# - SOLACE_BROKER_USERNAME: Username for the Solace broker.
# - SOLACE_BROKER_PASSWORD: Password for the Solace broker.
# - SOLACE_BROKER_VPN: VPN name for the Solace broker.
# - ARTIFACT_BASE_PATH: Base path for the filesystem artifact service (e.g., "/mnt/shared_artifacts" or "/tmp/a2a_artifacts").
#
# Optional Environment Variables for RBAC example:
# - ROLE_DEFINITIONS_PATH
# - USER_ASSIGNMENTS_PATH
#
# Environment Variables for Endpoint Secrets (examples):
# - INGEST_API_TOKEN
# - LOG_USER
# - LOG_PASSWORD
# - ALERT_API_KEY
# - IMAGE_UPLOAD_SECRET

# Global log settings for the Solace AI Connector
log:
  stdout_log_level: INFO # Or DEBUG for more verbose logging
  # file_log_level: DEBUG
  # log_file: "sac_webhook_gateway.log"

# Shared SAM config
!include shared_config.yaml

apps:
  - name: universal_webhook_gateway_instance_01
    app_base_path: . # Assuming running from project root where src/ is located
    app_module: src.solace_agent_mesh.gateway.webhook.app # Path to the WebhookGatewayApp module
    
    broker: # Broker settings for the gateway app itself
      <<: *broker_connection
      # Queue name and subscriptions are automatically generated by BaseGatewayApp

    app_config:
      # --- GDK BaseGatewayApp Common Config ---
      namespace: ${NAMESPACE, myorg/dev} # Your A2A namespace
      gateway_id: "webhook-gw-01"    # Optional: Unique ID for this gateway instance
      
      artifact_service: # Required for saving payloads/files as artifacts
        type: "filesystem" 
        base_path: ${ARTIFACT_BASE_PATH, /tmp/a2a_webhook_artifacts} # Ensure this path is writable
        artifact_scope: "namespace" # Artifacts scoped to this gateway_id

      authorization_service: # Required: For A2A scope retrieval and access control
        type: "default_rbac"
        role_definitions_path: ${ROLE_DEFINITIONS_PATH, examples/auth/dev-roles.yaml}
        user_assignments_path: ${USER_ASSIGNMENTS_PATH, examples/auth/dev-users.yaml}
      
      # enable_embed_resolution: true # Default is true (for late-stage embeds if any agent returns them)
      # gateway_artifact_content_limit_bytes: 32768 
      # gateway_recursive_embed_depth: 3

      # --- WebhookGatewayApp Specific Config ---
      webhook_server_host: "0.0.0.0" 
      webhook_server_port: 8088      
      cors_allowed_origins: ["*"]    # Or specify origins like ["http://localhost:3000", "https://myfrontend.com"]

      webhook_endpoints:
        # --- Endpoint 1: Simple JSON payload, no auth ---
        - path: "/hooks/simple_json_event"
          method: "POST" 
          target_agent_name: "echo_agent" # Agent that echoes back input
          payload_format: "json" 
          input_template: "Received JSON event. User: {{text://input.payload:user_name}}, Value: {{text://input.payload:value}}. From IP: {{text://user_data.client_host}}"
          auth:
            type: "none"
          # assumed_user_identity: "public_json_endpoint_user" # Optional

        # --- Endpoint 2: YAML payload, token auth in header ---
        - path: "/hooks/yaml_data_feed"
          method: "PUT"
          target_agent_name: "data_processor_agent"
          payload_format: "yaml"
          input_template: "Processing YAML data for source: {{text://input.payload:metadata:source_system}}. Record count: {{text://input.payload:records.#}}"
          auth:
            type: "token"
            token_config:
              location: "header"
              name: "X-Auth-Token" # Case-insensitive header name matching
              value: "${YAML_FEED_TOKEN}" # Store actual token in this env var
          assumed_user_identity: "yaml_feed_service"
          save_payload_as_artifact: false

        # --- Endpoint 3: Text payload, basic auth, save payload as artifact ---
        - path: "/hooks/log_message"
          method: "POST"
          target_agent_name: "log_archiver_agent"
          payload_format: "json"
          input_template: "Archive log message. Source: {{text://user_data.headers:x-log-source}}. Artifact URI: {{text://user_data.webhook_payload_artifact_uri}}"
          auth:
            type: "none"
            basic_auth_config:
              credentials: "${LOG_USER}:${LOG_PASSWORD}" # e.g., "logger_user:secret_pass"
          assumed_user_identity: "edward.funnekotter@solace.com"
          save_payload_as_artifact: true
          artifact_filename_template: "{{text://user_data.headers:x-log-source}}_{{text://input.user_properties:correlation_id}}.log"
          artifact_mime_type_override: "text/plain; charset=utf-8"

        # --- Endpoint 4: GET request with query params, token auth in query ---
        - path: "/hooks/trigger_alert"
          method: "GET"
          target_agent_name: "alerting_agent"
          payload_format: "text" # GET requests typically don't have bodies; payload will be empty.
          input_template: "Trigger alert: Severity {{text://input.user_properties:severity | default:'INFO'}}. Message: {{text://input.user_properties:message}}. Triggered by: {{text://user_data.headers:user-agent}}"
          auth:
            type: "token"
            token_config:
              location: "query_param"
              name: "apikey"
              value: "${ALERT_API_KEY}"
          # No assumed_user_identity, will use a generic token user ID

        # --- Endpoint 5: Form data with file uploads ---
        - path: "/hooks/submit_report"
          method: "POST"
          target_agent_name: "report_processor_agent"
          payload_format: "form_data"
          # save_payload_as_artifact: true # If true, non-file fields also saved as 'form_fields.json'
          input_template: |
            Process submitted report.
            Report Title: {{text://input.payload:report_title}}
            Submitter: {{text://input.payload:submitter_email}}
            {{#if user_data.uploaded_files}}
            Uploaded Files:
            {{#each user_data.uploaded_files}}
            - {{this.filename}} ({{this.mime_type}}): {{this.uri}} (Size: {{this.size}} bytes)
            {{/each}}
            {{else}}
            No files uploaded.
            {{/if}}
            {{#if user_data.webhook_payload_artifact_uri}}
            (Form fields also saved as artifact: {{text://user_data.webhook_payload_artifact_uri}})
            {{/if}}
          auth:
            type: "none" # Example: internal service, or auth handled by network rules
          assumed_user_identity: "report_submitter_system"

        # --- Endpoint 6: Binary payload (e.g., image upload) ---
        - path: "/hooks/upload_image"
          method: "POST"
          target_agent_name: "image_analysis_agent"
          payload_format: "binary" # Payload is always saved as artifact
          # save_payload_as_artifact: true # Implicitly true for 'binary'
          artifact_filename_template: "images/{{invoke:uuid.uuid4.hex}}.jpg" # Example: use properties if available, e.g. {{text://input.user_properties:image_id}}
          artifact_mime_type_override: "image/jpeg" # Helpful if Content-Type header might be missing/wrong
          input_template: "Analyze uploaded image. Artifact URI: {{text://user_data.binary_payload_artifact_uri}}. Original filename from Content-Disposition (if any): {{text://user_data.headers:content-disposition}}"
          auth:
            type: "token"
            token_config:
              location: "header"
              name: "X-Device-Signature" # Example: could be a signature or simple token
              value: "${IMAGE_UPLOAD_SECRET}"
          assumed_user_identity: "iot_device_uploader"

        # --- Endpoint 7: XML payload treated as text, saved as artifact ---
        - path: "/hooks/process_xml_order"
          method: "POST"
          target_agent_name: "order_parser_agent"
          payload_format: "xml" # Treated as text for payload, but can be saved as artifact
          input_template: "Process XML order. Artifact URI: {{text://user_data.webhook_payload_artifact_uri}}. Order ID from query: {{text://input.user_properties:orderId}}"
          auth:
            type: "none"
          save_payload_as_artifact: true
          artifact_filename_template: "orders/{{text://input.user_properties:orderId | default:'unknown_order'}}.xml"
          # artifact_mime_type_override: "application/xml" # Can be set if needed

  # Example of a simple echo agent for testing webhook submissions
  - name: echo_agent_app
    app_module: src.solace_agent_mesh.agent.sac.app # Path to A2A_ADK_App
    broker: # Broker settings for the agent app
      <<: *broker_connection
    app_config:
      namespace: ${NAMESPACE, myorg/dev} # Must match gateway's namespace
      agent_name: "echo_agent"
      display_name: "Echo"
      model: "gemini-1.5-flash-latest" # Or any valid ADK model string/config
      instruction: "You are an echo agent. Repeat the user's input back to them. If the input mentions an artifact URI, acknowledge that you see the URI."
      session_service: { type: "memory" }
      artifact_service: 
        type: "filesystem"
        base_path: ${ARTIFACT_BASE_PATH, /tmp/a2a_agent_artifacts} # Separate path for agent artifacts
        artifact_scope: "app" # Scoped to this agent app
      agent_card_publishing: { interval_seconds: 60 }
      agent_discovery: { enabled: true }
      inter_agent_communication: { allow_list: ["*"] }
      agent_card:
        description: "A simple agent that echoes input."
        skills:
          - id: "echo"
            name: "Echo Input"
            description: "Repeats back any text provided to it."

  # Add other agent definitions (data_processor_agent, log_archiver_agent, etc.) here
  # Ensure they are configured to connect to the same Solace broker and namespace.
