{
	"$schema": "http://json-schema.org/draft-07/schema#",
	"$id": "shelly-ng-mapping-schema",
	"title": "Shelly NG Mapping Configuration",
	"description": "Schema for Shelly NG device mapping configuration files",
	"type": "object",
	"required": ["version", "mappings"],
	"properties": {
		"version": {
			"type": "string",
			"description": "Schema version",
			"pattern": "^\\d+\\.\\d+$"
		},
		"transformers": {
			"type": "object",
			"description": "Named transformer definitions",
			"additionalProperties": {
				"$ref": "#/definitions/transformer"
			}
		},
		"derivations": {
			"type": "object",
			"description": "Named derivation rule definitions",
			"additionalProperties": {
				"$ref": "#/definitions/derivation_definition"
			}
		},
		"mappings": {
			"type": "array",
			"description": "Mapping definitions",
			"items": {
				"$ref": "#/definitions/mapping"
			}
		}
	},
	"definitions": {
		"transformer": {
			"type": "object",
			"required": ["type"],
			"properties": {
				"type": {
					"type": "string",
					"enum": ["scale", "map", "formula", "boolean", "clamp", "round"]
				},
				"direction": {
					"type": "string",
					"enum": ["bidirectional", "read_only", "write_only"]
				},
				"input_range": {
					"type": "array",
					"items": { "type": "number" },
					"minItems": 2,
					"maxItems": 2
				},
				"output_range": {
					"type": "array",
					"items": { "type": "number" },
					"minItems": 2,
					"maxItems": 2
				},
				"read": {},
				"write": {},
				"bidirectional": {
					"type": "object"
				},
				"true_value": {},
				"false_value": {},
				"invert": {
					"type": "boolean"
				},
				"min": {
					"type": "number"
				},
				"max": {
					"type": "number"
				},
				"precision": {
					"type": "integer"
				}
			}
		},
		"derivation_definition": {
			"type": "object",
			"properties": {
				"description": {
					"type": "string"
				},
				"rule": {
					"$ref": "#/definitions/derivation"
				}
			},
			"required": ["rule"]
		},
		"derivation": {
			"type": "object",
			"required": ["type"],
			"properties": {
				"type": {
					"type": "string",
					"enum": ["threshold", "boolean_map", "position_status"]
				},
				"thresholds": {
					"type": "array",
					"items": {
						"type": "object",
						"properties": {
							"max": { "type": "number" },
							"min": { "type": "number" },
							"value": { "type": "string" }
						},
						"required": ["value"]
					}
				},
				"true_value": { "type": "string" },
				"false_value": { "type": "string" },
				"closed_value": { "type": "string" },
				"opened_value": { "type": "string" },
				"partial_value": { "type": "string" }
			}
		},
		"match_condition": {
			"type": "object",
			"properties": {
				"component_type": {
					"type": "string",
					"description": "Shelly component type"
				},
				"device_category": {
					"type": "string",
					"description": "Device category"
				},
				"model": {
					"type": "string",
					"description": "Device model ID"
				},
				"profile": {
					"type": "string",
					"description": "Device profile"
				},
				"all_of": {
					"type": "array",
					"items": { "$ref": "#/definitions/match_condition" }
				},
				"any_of": {
					"type": "array",
					"items": { "$ref": "#/definitions/match_condition" }
				}
			}
		},
		"panel_property": {
			"type": "object",
			"required": ["identifier", "data_type"],
			"properties": {
				"identifier": {
					"type": "string",
					"description": "Property category identifier"
				},
				"name": {
					"type": "string"
				},
				"data_type": {
					"type": "string",
					"enum": ["CHAR", "UCHAR", "SHORT", "USHORT", "INT", "UINT", "FLOAT", "BOOL", "STRING", "ENUM", "DATE", "TIME", "DATETIME", "COLOR", "BUTTON", "SWITCH", "UNKNOWN"]
				},
				"format": {
					"type": "array"
				},
				"unit": {
					"type": "string"
				},
				"settable": {
					"type": "boolean"
				},
				"queryable": {
					"type": "boolean"
				},
				"invalid": {}
			}
		},
		"property_mapping": {
			"type": "object",
			"required": ["shelly_property", "panel"],
			"properties": {
				"shelly_property": {
					"type": "string",
					"description": "Shelly property name"
				},
				"direction": {
					"type": "string",
					"enum": ["bidirectional", "read_only", "write_only"]
				},
				"panel": {
					"$ref": "#/definitions/panel_property"
				},
				"transformer": {
					"type": "string"
				},
				"transform": {
					"type": "object"
				}
			}
		},
		"static_property": {
			"type": "object",
			"required": ["identifier", "data_type", "value"],
			"properties": {
				"identifier": {
					"type": "string"
				},
				"name": {
					"type": "string"
				},
				"data_type": {
					"type": "string"
				},
				"format": {
					"type": "array"
				},
				"unit": {
					"type": "string"
				},
				"value": {}
			}
		},
		"derived_property": {
			"type": "object",
			"required": ["identifier", "data_type", "source_property"],
			"properties": {
				"identifier": {
					"type": "string"
				},
				"name": {
					"type": "string"
				},
				"data_type": {
					"type": "string"
				},
				"format": {
					"type": "array"
				},
				"unit": {
					"type": "string"
				},
				"source_property": {
					"type": "string"
				},
				"derivation": {
					"type": "string"
				},
				"derive": {
					"$ref": "#/definitions/derivation"
				}
			}
		},
		"channel": {
			"type": "object",
			"required": ["identifier", "category"],
			"properties": {
				"identifier": {
					"type": "string"
				},
				"name": {
					"type": "string"
				},
				"category": {
					"type": "string"
				},
				"parent_identifier": {
					"type": "string"
				},
				"properties": {
					"type": "array",
					"items": { "$ref": "#/definitions/property_mapping" }
				},
				"static_properties": {
					"type": "array",
					"items": { "$ref": "#/definitions/static_property" }
				},
				"derived_properties": {
					"type": "array",
					"items": { "$ref": "#/definitions/derived_property" }
				}
			}
		},
		"mapping": {
			"type": "object",
			"required": ["name", "match", "channels"],
			"properties": {
				"name": {
					"type": "string"
				},
				"description": {
					"type": "string"
				},
				"priority": {
					"type": "integer"
				},
				"match": {
					"$ref": "#/definitions/match_condition"
				},
				"channels": {
					"type": "array",
					"items": { "$ref": "#/definitions/channel" }
				}
			}
		}
	}
}
