{
	"$schema": "http://json-schema.org/draft-07/schema#",
	"title": "Subscription router configuration",
	"type": "object",
	"required": ["profiles"],
	"properties": {
		"profiles": {
			"type": "object",
			"propertyNames": { "pattern": "^[a-z0-9][a-z0-9._-]*$" },
			"additionalProperties": { "$ref": "#/definitions/profile" }
		},
		"providers": {
			"type": "object",
			"propertyNames": { "$ref": "#/definitions/providerId" },
			"additionalProperties": { "$ref": "#/definitions/provider" }
		}
	},
	"definitions": {
		"providerId": { "type": "string", "pattern": "^[^\\s/]+$" },
		"collectorId": { "type": "string", "pattern": "^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$", "maxLength": 64 },
		"nonBlank": { "type": "string", "pattern": "\\S" },
		"positive": { "type": "number", "exclusiveMinimum": 0 },
		"provider": {
			"type": "object",
			"additionalProperties": false,
			"properties": {
				"label": {
					"type": "object",
					"additionalProperties": false,
					"required": ["full", "compact"],
					"properties": {
						"full": { "$ref": "#/definitions/nonBlank" },
						"compact": { "$ref": "#/definitions/nonBlank" }
					}
				},
				"quotaGroup": { "$ref": "#/definitions/nonBlank" },
				"evidence": { "type": "array", "uniqueItems": true, "items": { "enum": ["read", "failure", "observed"] } },
				"fallbackOnReadError": { "type": "boolean" },
				"failure": {
					"type": "object",
					"additionalProperties": false,
					"properties": {
						"parser": { "enum": ["generic", "google-rpc"] },
						"minHintSeconds": { "type": "number", "minimum": 0 },
						"maxBlockMinutes": { "type": "number", "exclusiveMinimum": 0, "maximum": 1440 },
						"repeat": {
							"type": "object",
							"additionalProperties": false,
							"required": ["count", "withinMinutes"],
							"properties": {
								"count": { "type": "integer", "minimum": 1, "maximum": 9007199254740991 },
								"withinMinutes": { "$ref": "#/definitions/positive" }
							}
						}
					}
				},
				"windows": { "type": "array", "items": { "$ref": "#/definitions/declaredWindow" } },
				"collector": {
					"type": "object",
					"additionalProperties": false,
					"properties": {
						"id": { "$ref": "#/definitions/collectorId" },
						"options": { "type": "object" }
					}
				}
			}
		},
		"declaredWindow": {
			"type": "object",
			"additionalProperties": false,
			"required": ["name", "kind", "unit", "scope"],
			"properties": {
				"name": { "$ref": "#/definitions/nonBlank" },
				"kind": { "enum": ["rolling", "calendar"] },
				"unit": { "enum": ["requests", "tokens", "usd"] },
				"scope": { "enum": ["provider", "model"] },
				"limits": {},
				"durationMinutes": { "$ref": "#/definitions/positive" },
				"resetRule": { "$ref": "#/definitions/calendarReset" }
			},
			"allOf": [
				{
					"if": { "properties": { "kind": { "const": "rolling" } } },
					"then": { "required": ["durationMinutes"], "not": { "required": ["resetRule"] } },
					"else": { "required": ["resetRule"], "not": { "required": ["durationMinutes"] } }
				},
				{
					"if": { "properties": { "scope": { "const": "provider" } } },
					"then": { "properties": { "limits": { "$ref": "#/definitions/positive" } } },
					"else": {
						"properties": {
							"limits": {
								"type": "object",
								"propertyNames": { "$ref": "#/definitions/nonBlank" },
								"additionalProperties": { "$ref": "#/definitions/positive" }
							}
						}
					}
				}
			]
		},
		"calendarReset": {
			"type": "object",
			"additionalProperties": false,
			"required": ["at", "timeZone", "every"],
			"properties": {
				"at": { "type": "string", "pattern": "^(?:[01][0-9]|2[0-3]):[0-5][0-9]$" },
				"timeZone": {
					"type": "string",
					"pattern": "^(?!(?!(?:UTC|GMT)$)[A-Za-z]{2,5}$)[A-Za-z][A-Za-z0-9_+-]*(?:/[A-Za-z0-9_+-]+)*$",
					"description": "Named IANA time zone. The loader also checks the name against Intl."
				},
				"every": { "const": "day" }
			}
		},
		"remaining": {
			"type": "object",
			"additionalProperties": false,
			"required": ["metric", "window", "direction"],
			"properties": {
				"metric": { "const": "remaining" },
				"direction": { "const": "desc" },
				"window": {
					"oneOf": [
						{ "const": "binding" },
						{
							"type": "object",
							"required": ["name"],
							"additionalProperties": false,
							"properties": { "name": { "type": "string", "minLength": 1 } }
						}
					]
				}
			}
		},
		"chain": {
			"type": "object",
			"additionalProperties": false,
			"required": ["metric", "direction"],
			"properties": { "metric": { "const": "chain-index" }, "direction": { "const": "asc" } }
		},
		"policy": {
			"oneOf": [
				{
					"type": "object",
					"additionalProperties": false,
					"required": ["version", "kind"],
					"properties": { "version": { "const": 1 }, "kind": { "const": "strict-priority" } }
				},
				{
					"type": "object",
					"additionalProperties": false,
					"required": ["version", "kind"],
					"properties": {
						"version": { "const": 1 },
						"kind": { "const": "remaining-balanced" },
						"switchMargin": { "type": "number", "minimum": 0, "maximum": 1 },
						"stickiness": { "enum": ["none", "continuation"] }
					}
				},
				{
					"type": "object",
					"additionalProperties": false,
					"required": ["version", "kind", "by"],
					"properties": {
						"version": { "const": 1 },
						"kind": { "const": "lexicographic" },
						"switchMargin": { "type": "number", "minimum": 0, "maximum": 1 },
						"stickiness": { "enum": ["none", "continuation"] },
						"by": {
							"oneOf": [
								{ "type": "array", "minItems": 1, "maxItems": 1, "items": [{ "$ref": "#/definitions/chain" }] },
								{
									"type": "array",
									"minItems": 1,
									"maxItems": 2,
									"items": [{ "$ref": "#/definitions/remaining" }, { "$ref": "#/definitions/chain" }]
								}
							]
						}
					},
					"allOf": [
						{
							"if": { "required": ["switchMargin"] },
							"then": {
								"properties": {
									"by": { "items": [{ "$ref": "#/definitions/remaining" }, { "$ref": "#/definitions/chain" }] }
								}
							}
						}
					]
				}
			]
		},
		"profile": {
			"type": "object",
			"required": ["chain"],
			"properties": {
				"name": { "type": "string" },
				"strategy": { "enum": ["ordered", "balanced"] },
				"policy": { "$ref": "#/definitions/policy" },
				"exhaustionPolicy": { "enum": ["wait", "fail"] },
				"maxWaitMinutes": { "type": "number", "exclusiveMinimum": 0 },
				"chain": {
					"type": "array",
					"minItems": 1,
					"items": {
						"type": "object",
						"required": ["model"],
						"properties": {
							"model": { "type": "string", "pattern": "^(?!router/)[^/]+/[\\s\\S]+$" },
							"thinking": { "enum": ["off", "minimal", "low", "medium", "high", "xhigh", "max"] }
						}
					}
				}
			},
			"allOf": [
				{ "not": { "required": ["policy", "strategy"] } },
				{ "not": { "required": ["policy", "policyByCaller"] } }
			]
		}
	}
}
