{
	"$schema": "http://json-schema.org/draft-07/schema#",
	"$id": "https://fastybird.com/schemas/home-assistant-mapping-v1.json",
	"title": "Home Assistant Entity Mapping Configuration",
	"description": "Schema for defining Home Assistant entity mappings to Smart Panel channels and properties",
	"type": "object",
	"required": ["version", "mappings"],
	"additionalProperties": false,
	"properties": {
		"version": {
			"type": "string",
			"pattern": "^[0-9]+\\.[0-9]+$",
			"description": "Schema version (e.g., '1.0')"
		},
		"transformers": {
			"type": "object",
			"description": "Reusable value transformers",
			"additionalProperties": {
				"$ref": "#/definitions/transformer"
			}
		},
		"domain_roles": {
			"type": "object",
			"description": "Classification of HA domains by their role",
			"properties": {
				"primary": {
					"type": "array",
					"items": { "type": "string" },
					"description": "Primary domains - these define what the device IS"
				},
				"standalone": {
					"type": "array",
					"items": { "type": "string" },
					"description": "Standalone domains - can be primary alone (sensor-only devices)"
				},
				"secondary": {
					"type": "array",
					"items": { "type": "string" },
					"description": "Secondary domains - typically supporting entities"
				}
			},
			"additionalProperties": false
		},
		"mappings": {
			"type": "array",
			"description": "Entity type mappings",
			"items": {
				"$ref": "#/definitions/mapping"
			},
			"minItems": 0
		}
	},
	"definitions": {
		"transformer": {
			"type": "object",
			"required": ["type"],
			"properties": {
				"type": {
					"type": "string",
					"enum": ["scale", "map", "formula", "boolean", "clamp", "round"],
					"description": "Type of transformer"
				},
				"direction": {
					"type": "string",
					"enum": ["bidirectional", "read_only", "write_only"],
					"default": "bidirectional",
					"description": "Direction of transformation"
				}
			},
			"allOf": [
				{
					"if": {
						"properties": { "type": { "const": "scale" } }
					},
					"then": {
						"properties": {
							"type": { "const": "scale" },
							"direction": true,
							"input_range": {
								"type": "array",
								"items": { "type": "number" },
								"minItems": 2,
								"maxItems": 2,
								"description": "Input range [min, max] (HA values)"
							},
							"output_range": {
								"type": "array",
								"items": { "type": "number" },
								"minItems": 2,
								"maxItems": 2,
								"description": "Output range [min, max] (Smart Panel values)"
							}
						},
						"required": ["type", "input_range", "output_range"],
						"additionalProperties": false
					}
				},
				{
					"if": {
						"properties": { "type": { "const": "map" } }
					},
					"then": {
						"properties": {
							"type": { "const": "map" },
							"direction": true,
							"read": {
								"type": "object",
								"description": "HA value -> Smart Panel value mapping",
								"additionalProperties": true
							},
							"write": {
								"type": "object",
								"description": "Smart Panel value -> HA value mapping",
								"additionalProperties": true
							},
							"bidirectional": {
								"type": "object",
								"description": "Bidirectional mapping (auto-creates inverse for write)",
								"additionalProperties": true
							}
						},
						"additionalProperties": false
					}
				},
				{
					"if": {
						"properties": { "type": { "const": "formula" } }
					},
					"then": {
						"properties": {
							"type": { "const": "formula" },
							"direction": true,
							"read": {
								"type": "string",
								"description": "JavaScript expression for HA -> Smart Panel (use 'value' variable)"
							},
							"write": {
								"type": "string",
								"description": "JavaScript expression for Smart Panel -> HA (use 'value' variable)"
							}
						},
						"additionalProperties": false
					}
				},
				{
					"if": {
						"properties": { "type": { "const": "boolean" } }
					},
					"then": {
						"properties": {
							"type": { "const": "boolean" },
							"direction": true,
							"true_value": {
								"description": "HA value representing true"
							},
							"false_value": {
								"description": "HA value representing false"
							},
							"invert": {
								"type": "boolean",
								"default": false,
								"description": "Invert the boolean logic"
							}
						},
						"required": ["type", "true_value", "false_value"],
						"additionalProperties": false
					}
				},
				{
					"if": {
						"properties": { "type": { "const": "clamp" } }
					},
					"then": {
						"properties": {
							"type": { "const": "clamp" },
							"direction": true,
							"min": {
								"type": "number",
								"description": "Minimum value"
							},
							"max": {
								"type": "number",
								"description": "Maximum value"
							}
						},
						"required": ["type", "min", "max"],
						"additionalProperties": false
					}
				},
				{
					"if": {
						"properties": { "type": { "const": "round" } }
					},
					"then": {
						"properties": {
							"type": { "const": "round" },
							"direction": true,
							"precision": {
								"type": "integer",
								"minimum": 0,
								"default": 0,
								"description": "Number of decimal places"
							}
						},
						"additionalProperties": false
					}
				}
			]
		},
		"mapping": {
			"type": "object",
			"required": ["name", "domain", "channel", "device_category", "property_bindings"],
			"additionalProperties": false,
			"properties": {
				"name": {
					"type": "string",
					"description": "Unique identifier for this mapping"
				},
				"description": {
					"type": "string",
					"description": "Human-readable description"
				},
				"domain": {
					"type": "string",
					"description": "Home Assistant domain (light, switch, sensor, etc.)"
				},
				"device_class": {
					"oneOf": [
						{ "type": "string" },
						{ "type": "array", "items": { "type": "string" } },
						{ "type": "null" }
					],
					"description": "HA device class filter (null = any, string = exact match, array = any of these)"
				},
				"entity_id_contains": {
					"type": "string",
					"description": "Optional entity_id pattern to match (substring match)"
				},
				"priority": {
					"type": "integer",
					"default": 50,
					"description": "Priority for matching (higher = checked first)"
				},
				"channel": {
					"$ref": "#/definitions/channelConfig",
					"description": "Smart Panel channel configuration"
				},
				"device_category": {
					"type": "string",
					"description": "Smart Panel device category"
				},
				"property_bindings": {
					"type": "array",
					"items": {
						"$ref": "#/definitions/propertyBinding"
					},
					"minItems": 1,
					"description": "Property bindings from HA attributes to SP properties"
				}
			}
		},
		"channelConfig": {
			"type": "object",
			"required": ["category"],
			"additionalProperties": false,
			"properties": {
				"category": {
					"type": "string",
					"description": "Smart Panel channel category"
				},
				"identifier": {
					"type": "string",
					"description": "Optional custom channel identifier"
				},
				"name": {
					"type": "string",
					"description": "Optional custom channel name"
				}
			}
		},
		"propertyBinding": {
			"type": "object",
			"required": ["ha_attribute", "property_category"],
			"additionalProperties": false,
			"properties": {
				"ha_attribute": {
					"type": "string",
					"description": "Home Assistant attribute name. Use 'fb.main_state' for the entity's main state value"
				},
				"property_category": {
					"type": "string",
					"description": "Smart Panel property category"
				},
				"array_index": {
					"type": "integer",
					"minimum": 0,
					"description": "For array attributes (like hs_color, rgb_color), which index to use"
				},
				"transformer": {
					"type": "string",
					"description": "Reference to named transformer for value transformation"
				}
			}
		}
	}
}
