{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "title": "RPC Server-side Request (MQTT v1/devices/me/rpc/request/+)",
    "description": "RPC request consumed by the gateway when the backend initiates server-side RPC. Single-file bundle (alert $defs included).",
    "type": "object",
    "required": [
        "method",
        "params"
    ],
    "properties": {
        "method": {
            "type": "string",
            "enum": [
                "refreshDevices",
                "trigAlertOnce",
                "trigAlertContinuous",
                "stopAlertAll",
                "stopAlertContinuous"
            ]
        },
        "params": {
            "type": "object",
            "description": "RPC method parameters. Schema depends on `method`."
        }
    },
    "allOf": [
        {
            "title": "RefreshDevicesMethodBinding",
            "if": {
                "properties": {
                    "method": {
                        "const": "refreshDevices"
                    }
                }
            },
            "then": {
                "properties": {
                    "params": {
                        "type": "object",
                        "description": "refreshDevices params (typically empty object).",
                        "additionalProperties": true
                    }
                }
            }
        },
        {
            "title": "TrigAlertOnceMethodBinding",
            "if": {
                "properties": {
                    "method": {
                        "const": "trigAlertOnce"
                    }
                }
            },
            "then": {
                "properties": {
                    "params": {
                        "description": "Parameters for triggering a one-shot alert.",
                        "allOf": [
                            {
                                "$ref": "#/$defs/params_trigAlertOnce"
                            }
                        ]
                    }
                }
            }
        },
        {
            "title": "TrigAlertContinuousMethodBinding",
            "if": {
                "properties": {
                    "method": {
                        "const": "trigAlertContinuous"
                    }
                }
            },
            "then": {
                "properties": {
                    "params": {
                        "description": "Parameters for starting a continuous alert.",
                        "allOf": [
                            {
                                "$ref": "#/$defs/params_trigAlertContinuous"
                            }
                        ]
                    }
                }
            }
        },
        {
            "title": "StopAlertAllMethodBinding",
            "if": {
                "properties": {
                    "method": {
                        "const": "stopAlertAll"
                    }
                }
            },
            "then": {
                "properties": {
                    "params": {
                        "description": "Parameters for stopping all active alerts.",
                        "allOf": [
                            {
                                "$ref": "#/$defs/params_stopAlertAll"
                            }
                        ]
                    }
                }
            }
        },
        {
            "title": "StopAlertContinuousMethodBinding",
            "if": {
                "properties": {
                    "method": {
                        "const": "stopAlertContinuous"
                    }
                }
            },
            "then": {
                "properties": {
                    "params": {
                        "description": "Parameters for stopping a continuous alert.",
                        "allOf": [
                            {
                                "$ref": "#/$defs/params_stopAlertContinuous"
                            }
                        ]
                    }
                }
            }
        }
    ],
    "additionalProperties": false,
    "$defs": {
        "anyObject": {
            "type": "object"
        },
        "params_trigAlertOnce": {
            "title": "ParamsTrigAlertOnce",
            "type": "object",
            "required": [
                "source",
                "scope",
                "priority",
                "signaling"
            ],
            "properties": {
                "ts": {
                    "$ref": "#/$defs/ts"
                },
                "source": {
                    "$ref": "#/$defs/source"
                },
                "scope": {
                    "$ref": "#/$defs/scope"
                },
                "priority": {
                    "$ref": "#/$defs/priority"
                },
                "signaling": {
                    "$ref": "#/$defs/signaling"
                },
                "alertRelayActivation": {
                    "$ref": "#/$defs/alertRelayActivation"
                },
                "audible": {
                    "$ref": "#/$defs/anyObject"
                },
                "visual": {
                    "$ref": "#/$defs/anyObject"
                }
            },
            "allOf": [
                {
                    "if": {
                        "properties": {
                            "signaling": {
                                "const": "visual"
                            }
                        },
                        "required": [
                            "signaling"
                        ]
                    },
                    "then": {
                        "required": [
                            "visual"
                        ],
                        "properties": {
                            "visual": {
                                "$ref": "#/$defs/visual"
                            }
                        }
                    },
                    "else": {
                        "required": [
                            "audible"
                        ],
                        "properties": {
                            "audible": {
                                "$ref": "#/$defs/audibleOnce"
                            }
                        }
                    }
                }
            ]
        },
        "params_trigAlertContinuous": {
            "title": "ParamsTrigAlertContinuous",
            "type": "object",
            "required": [
                "source",
                "scope",
                "priority",
                "alertId",
                "signaling"
            ],
            "properties": {
                "ts": {
                    "$ref": "#/$defs/ts"
                },
                "source": {
                    "$ref": "#/$defs/source"
                },
                "scope": {
                    "$ref": "#/$defs/scope"
                },
                "priority": {
                    "$ref": "#/$defs/priority"
                },
                "alertId": {
                    "$ref": "#/$defs/alertId"
                },
                "signaling": {
                    "$ref": "#/$defs/signaling"
                },
                "alertRelayActivation": {
                    "$ref": "#/$defs/alertRelayActivation"
                },
                "audible": {
                    "$ref": "#/$defs/anyObject"
                },
                "visual": {
                    "$ref": "#/$defs/anyObject"
                }
            },
            "allOf": [
                {
                    "if": {
                        "properties": {
                            "signaling": {
                                "const": "visual"
                            }
                        },
                        "required": [
                            "signaling"
                        ]
                    },
                    "then": {
                        "properties": {}
                    },
                    "else": {
                        "required": [
                            "audible"
                        ],
                        "properties": {
                            "audible": {
                                "$ref": "#/$defs/audibleContinuous"
                            }
                        }
                    }
                }
            ]
        },
        "params_stopAlertAll": {
            "title": "ParamsStopAlertAll",
            "type": "object",
            "required": [
                "source",
                "scope",
                "signaling"
            ],
            "properties": {
                "ts": {
                    "$ref": "#/$defs/ts"
                },
                "source": {
                    "$ref": "#/$defs/source"
                },
                "scope": {
                    "$ref": "#/$defs/scope"
                },
                "signaling": {
                    "$ref": "#/$defs/signaling"
                }
            }
        },
        "params_stopAlertContinuous": {
            "title": "ParamsStopAlertContinuous",
            "type": "object",
            "required": [
                "source",
                "scope",
                "alertId",
                "signaling"
            ],
            "properties": {
                "ts": {
                    "$ref": "#/$defs/ts"
                },
                "source": {
                    "$ref": "#/$defs/source"
                },
                "scope": {
                    "$ref": "#/$defs/scope"
                },
                "alertId": {
                    "$ref": "#/$defs/alertId"
                },
                "signaling": {
                    "$ref": "#/$defs/signaling"
                }
            }
        },
        "scope": {
            "type": "object",
            "required": [
                "type"
            ],
            "properties": {
                "type": {
                    "type": "string",
                    "enum": [
                        "global",
                        "group"
                    ]
                },
                "groupId": {
                    "type": "integer",
                    "minimum": 0,
                    "maximum": 15
                }
            },
            "allOf": [
                {
                    "if": {
                        "properties": {
                            "type": {
                                "const": "group"
                            }
                        },
                        "required": [
                            "type"
                        ]
                    },
                    "then": {
                        "required": [
                            "groupId"
                        ]
                    }
                }
            ]
        },
        "source": {
            "type": "object",
            "required": [
                "kind",
                "id"
            ],
            "properties": {
                "kind": {
                    "type": "string",
                    "enum": [
                        "device",
                        "user",
                        "service",
                        "system"
                    ]
                },
                "id": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 63
                }
            }
        },
        "priority": {
            "type": "string",
            "enum": [
                "highest",
                "high",
                "medium",
                "low",
                "lowest"
            ]
        },
        "ts": {
            "type": "integer",
            "minimum": 0,
            "maximum": 18446744073709551615,
            "description": "Optional event timestamp in milliseconds since epoch (uint64)."
        },
        "alertId": {
            "type": "integer",
            "minimum": 0,
            "maximum": 255
        },
        "signaling": {
            "type": "string",
            "enum": [
                "combined",
                "audible",
                "visual"
            ]
        },
        "alertRelayActivation": {
            "type": "string",
            "enum": [
                "on",
                "off"
            ]
        },
        "sound": {
            "type": "object",
            "required": [
                "bank",
                "fileId",
                "attenuationInDB"
            ],
            "properties": {
                "bank": {
                    "type": "string",
                    "enum": [
                        "standard",
                        "custom"
                    ]
                },
                "fileId": {
                    "type": "integer",
                    "minimum": 0,
                    "maximum": 127
                },
                "attenuationInDB": {
                    "type": "integer",
                    "minimum": 0,
                    "maximum": 40
                }
            }
        },
        "soundWithRepeats": {
            "allOf": [
                {
                    "$ref": "#/$defs/sound"
                },
                {
                    "properties": {
                        "repeats": {
                            "type": "integer",
                            "minimum": 0,
                            "maximum": 255
                        }
                    },
                    "required": [
                        "repeats"
                    ]
                }
            ]
        },
        "audibleOnce": {
            "type": "object",
            "required": [
                "sounds",
                "sequenceRepeats"
            ],
            "properties": {
                "sounds": {
                    "type": "array",
                    "minItems": 1,
                    "maxItems": 2,
                    "items": {
                        "$ref": "#/$defs/soundWithRepeats"
                    }
                },
                "sequenceRepeats": {
                    "type": "integer",
                    "minimum": 0,
                    "maximum": 65535
                }
            },
            "allOf": [
                {
                    "$comment": "sounds.length MUST be 1 or 2 (enforced by minItems/maxItems above)."
                }
            ]
        },
        "audibleContinuous": {
            "type": "object",
            "required": [
                "sounds"
            ],
            "properties": {
                "sounds": {
                    "oneOf": [
                        {
                            "title": "SingleContinuousSound",
                            "type": "array",
                            "minItems": 1,
                            "maxItems": 1,
                            "items": {
                                "$ref": "#/$defs/sound"
                            }
                        },
                        {
                            "title": "DualContinuousSoundsWithRepeats",
                            "type": "array",
                            "minItems": 2,
                            "maxItems": 2,
                            "items": {
                                "$ref": "#/$defs/soundWithRepeats"
                            }
                        }
                    ]
                }
            },
            "allOf": [
                {
                    "$comment": "sounds.length MUST be 1 or 2 (enforced by minItems/maxItems above)."
                }
            ]
        },
        "visual": {
            "type": "object",
            "required": [
                "durationInSec"
            ],
            "properties": {
                "durationInSec": {
                    "type": "integer",
                    "minimum": 1,
                    "maximum": 65534
                }
            }
        }
    }
}
