{
  "$id": "https://alwaysai.co/to-device-agent-message-schema.json",
  "$schema": "https://json-schema.org/draft/2019-09/schema",
  "title": "to_device_agent_message",
  "description":
    "A schema that describes the message payload structure for messages sent from clients to device agent",
  "type": "object",
  "properties": {
    "timestamp": {
      "type": "string"
    },
    "topic": { "$ref": "#/$defs/toDevice" },
    "messageType": {
      "type": "string",
      "enum": [
        "app_state_control",
        "app_version_control",
        "fleet_app_version_control",
        "app_install_response",
        "models_install_response",
        "live_state_updates",
        "device_action",
        "status_response"
      ]
    },
    "txId": {
      "$ref": "https://alwaysai.co/common-schema.json#/$defs/txId"
    },
    "payload": {
      "type": "object"
    }
  },
  "required": ["messageType", "timestamp", "txId", "payload"],
  "allOf":[
    {
      "if": {
        "properties": {
          "messageType": { "const": "app_state_control" }
        }
      },
      "then": {
        "properties": {
          "payload": {"$ref": "https://alwaysai.co/app-state-control-schema.json"}
        }
      }
    },
    {
      "if": {
        "properties": {
          "messageType": { "const": "app_version_control" }
        }
      },
      "then": {
        "properties": {
          "payload": {"$ref": "https://alwaysai.co/app-version-control-schema.json"}
        }
      }
    },
    {
      "if": {
        "properties": {
          "messageType": { "const": "app_install_response" }
        }
      },
      "then": {
        "properties": {
          "payload": {"$ref": "https://alwaysai.co/app-install-response-schema.json"}
        }
      }
    },
    {
      "if": {
        "properties": {
          "messageType": { "const": "models_install_response" }
        }
      },
      "then": {
        "properties": {
          "payload": {"$ref": "https://alwaysai.co/models-install-response-schema.json"}
        }
      }
    },
    {
      "if": {
        "properties": {
          "messageType": { "const": "live_state_updates" }
        }
      },
      "then": {
        "properties": {
          "payload": {"$ref": "https://alwaysai.co/live-updates-toggle-schema.json"}
        }
      }
    },
    {
      "if": {
        "properties": {
          "messageType": { "const": "device_action" }
        }
      },
      "then": {
        "properties": {
          "payload": {"$ref": "https://alwaysai.co/device-action-schema.json"}
        }
      }
    },
    {
      "if": {
        "properties": {
          "messageType": { "const": "status_response" }
        }
      },
      "then": {
        "properties": {
          "payload": {"$ref": "https://alwaysai.co/status-response-schema.json"}
        }
      }
    }
  ],
  "$defs": {
    "toDevice": {
      "description": "Topic on which messages are sent to device agent.",
      "type": "string",
      "pattern": "^to-device/aai-device/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"
    }
  }
}
