{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://schemas.revenexx.com/settings.schema.json",
  "title": "revenexx App Settings",
  "description": "Declarative per-tenant and per-market configuration for a revenexx App. Console stores setting values, Cockpit auto-renders a settings form from this schema. Each setting declares its scope: 'tenant' (one value for the entire Tenant) or 'market' (configurable per Market, with fallback to tenant-level default).",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "$schema": {
      "type": "string",
      "description": "Reference to this JSON Schema for editor validation and autocompletion."
    },
    "settings": {
      "type": "object",
      "description": "Map of setting key to setting definition. Keys must be lowercase with underscores.",
      "additionalProperties": {
        "$ref": "#/$defs/setting"
      },
      "propertyNames": {
        "pattern": "^[a-z][a-z0-9_]*$"
      }
    }
  },
  "$defs": {
    "setting": {
      "type": "object",
      "required": ["title", "type", "scope"],
      "additionalProperties": false,
      "properties": {
        "title": {
          "type": "string",
          "description": "Human-readable label displayed in the Cockpit settings form."
        },
        "description": {
          "type": "string",
          "description": "Help text displayed below the form field."
        },
        "type": {
          "type": "string",
          "enum": ["string", "number", "boolean", "enum", "json"],
          "description": "Setting value type. Determines the Cockpit form input: string → text input, number → number input, boolean → toggle, enum → dropdown, json → code editor."
        },
        "scope": {
          "type": "string",
          "enum": ["tenant", "market"],
          "description": "'tenant' — one value per Tenant, applies globally. 'market' — configurable per Market with fallback to the tenant-level default. Market-scoped settings require the 'markets' app as a dependency."
        },
        "default": {
          "description": "Default value used when no explicit value is set. Type must match the setting type."
        },
        "enum_values": {
          "type": "array",
          "items": {
            "type": "string"
          },
          "minItems": 1,
          "uniqueItems": true,
          "description": "Allowed values when type is 'enum'. Each value is a selectable option in the Cockpit dropdown."
        },
        "enum_labels": {
          "type": "object",
          "description": "Optional human-readable labels for enum values. Keys are enum_values entries, values are display labels. If omitted, the raw enum value is displayed.",
          "additionalProperties": {
            "type": "string"
          }
        },
        "required": {
          "type": "boolean",
          "default": false,
          "description": "Whether this setting must have a value before the app can be used."
        },
        "sensitive": {
          "type": "boolean",
          "default": false,
          "description": "If true, the value is stored encrypted and masked in the Cockpit UI. Use for API keys, credentials, and secrets."
        },
        "validation": {
          "type": "object",
          "description": "Optional validation rules.",
          "additionalProperties": false,
          "properties": {
            "min": {
              "type": "number",
              "description": "Minimum value for number settings."
            },
            "max": {
              "type": "number",
              "description": "Maximum value for number settings."
            },
            "min_length": {
              "type": "integer",
              "description": "Minimum length for string settings."
            },
            "max_length": {
              "type": "integer",
              "description": "Maximum length for string settings."
            },
            "pattern": {
              "type": "string",
              "description": "Regex pattern for string settings."
            }
          }
        },
        "group": {
          "type": "string",
          "description": "Optional grouping key. Settings with the same group are displayed together under a collapsible section in Cockpit."
        }
      }
    }
  }
}
