{
    "$id": "https://alwaysai.co/to-client-message-schema.json",
    "title": "to_client_message",
    "$schema": "https://json-schema.org/draft/2019-09/schema",
    "description": "The messaging payload structure for messages sent from device agent to clients",
    "type": "object",
    "properties": {
        "timestamp": {
            "type": "string"
        },
        "deviceId": {
            "description": "Device UUID of device sending the message",
            "type": "string"
        },
        "topic": {
            "oneOf": [{ "$ref": "#/$defs/toClient" }]
        },
        "messageType": {
            "type": "string",
            "enum": [
                "device_stats",
                "app_state",
                "status_response",
                "app_logs"
            ]
        },
        "txId": {
            "$ref": "https://alwaysai.co/common-schema.json#/$defs/txId"
        },
        "payload": {
            "type": "object"
        }
    },
    "required": ["timestamp", "deviceId", "messageType", "txId", "payload"],
    "allOf": [
        {
            "if": {
                "properties": {
                    "messageType": { "const": "device_stats" }
                }
            },
            "then": {
                "properties": {
                    "payload": { "$ref": "https://alwaysai.co/device-stats-schema.json" }
                }
            }
        },
        {
            "if": {
                "properties": {
                    "messageType": { "const": "app_state" }
                }
            },
            "then": {
                "properties": {
                    "payload": { "$ref": "https://alwaysai.co/app-state-schema.json" }
                }
            }
        },
        {
            "if": {
                "properties": {
                    "messageType": { "const": "status_response" }
                }
            },
            "then": {
                "properties": {
                    "payload": { "$ref": "https://alwaysai.co/status-response-schema.json" }
                }
            }
        },
        {
            "if": {
                "properties": {
                    "messageType": { "const": "app_logs" }
                }
            },
            "then": {
                "properties": {
                    "payload": { "$ref": "https://alwaysai.co/app-logs-schema.json" }
                }
            }
        }
    ],
    "$defs": {
        "toClient": {
            "description": "Messages sent from the device agent to console client",
            "type": "string",
            "pattern": "to-client/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}$"
        }
    }
}
