{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://www.obsidian-sec.com/obsflow/obsflow.schema.json",
  "title": "Programa OBSFlow (.obsflow)",
  "description": "Diagrama de flujo estructurado de OBSFlow (OBSIDIAN-SEC). La primera función es Main.",
  "type": "object",
  "required": ["format", "version", "functions"],
  "properties": {
    "format": { "const": "obsflow" },
    "version": { "const": 1 },
    "title": { "type": "string" },
    "author": { "type": "string" },
    "description": { "type": "string" },
    "functions": {
      "type": "array",
      "minItems": 1,
      "items": { "$ref": "#/$defs/funcion" },
      "description": "functions[0] es Main: sin parámetros y returnType \"None\"."
    }
  },
  "$defs": {
    "tipo": { "enum": ["Integer", "Real", "String", "Boolean"] },
    "identificador": { "type": "string", "pattern": "^[A-Za-zÀ-ɏ_][A-Za-z0-9À-ɏ_]*$" },
    "expresion": { "type": "string", "description": "Expresión OBSFlow (ver sección Expresiones del documento)." },
    "funcion": {
      "type": "object",
      "required": ["name", "body"],
      "properties": {
        "id": { "type": "string" },
        "name": { "$ref": "#/$defs/identificador" },
        "params": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["name", "type"],
            "properties": {
              "name": { "$ref": "#/$defs/identificador" },
              "type": { "$ref": "#/$defs/tipo" },
              "isArray": { "type": "boolean", "default": false }
            },
            "additionalProperties": false
          }
        },
        "returnType": { "enum": ["None", "Integer", "Real", "String", "Boolean"], "default": "None" },
        "returnVar": { "type": "string", "description": "Variable local (declarada en el cuerpo) cuyo valor se retorna." },
        "body": { "$ref": "#/$defs/bloques" }
      },
      "additionalProperties": false
    },
    "bloques": { "type": "array", "items": { "$ref": "#/$defs/bloque" } },
    "bloque": {
      "type": "object",
      "required": ["type"],
      "properties": {
        "id": { "type": "string", "description": "Opcional al crear; OBSFlow asigna uno." },
        "breakpoint": { "type": "boolean" },
        "breakpointCondition": { "type": "string", "description": "Condición de paro: el punto de interrupción sólo pausa si es Verdadera." },
        "type": { "enum": ["declare", "assign", "input", "output", "if", "while", "do", "for", "call", "comment"] }
      },
      "oneOf": [
        {
          "properties": {
            "type": { "const": "declare" },
            "names": { "type": "string", "description": "Uno o varios nombres separados por coma." },
            "varType": { "$ref": "#/$defs/tipo" },
            "isArray": { "type": "boolean" },
            "size": { "$ref": "#/$defs/expresion" }
          },
          "required": ["names", "varType"]
        },
        {
          "properties": { "type": { "const": "assign" }, "target": { "type": "string" }, "expr": { "$ref": "#/$defs/expresion" } },
          "required": ["target", "expr"]
        },
        {
          "properties": {
            "type": { "const": "input" },
            "target": { "type": "string" },
            "prompt": { "type": "string" },
            "required": { "type": "boolean", "default": true }
          },
          "required": ["target"]
        },
        {
          "properties": { "type": { "const": "output" }, "expr": { "$ref": "#/$defs/expresion" }, "newline": { "type": "boolean", "default": true } },
          "required": ["expr"]
        },
        {
          "properties": {
            "type": { "const": "if" },
            "cond": { "$ref": "#/$defs/expresion" },
            "yes": { "$ref": "#/$defs/bloques" },
            "no": { "$ref": "#/$defs/bloques" }
          },
          "required": ["cond"]
        },
        {
          "properties": { "type": { "enum": ["while", "do"] }, "cond": { "$ref": "#/$defs/expresion" }, "body": { "$ref": "#/$defs/bloques" } },
          "required": ["cond"]
        },
        {
          "properties": {
            "type": { "const": "for" },
            "variable": { "$ref": "#/$defs/identificador" },
            "from": { "$ref": "#/$defs/expresion" },
            "to": { "$ref": "#/$defs/expresion" },
            "step": { "$ref": "#/$defs/expresion" },
            "body": { "$ref": "#/$defs/bloques" }
          },
          "required": ["variable", "from", "to"]
        },
        {
          "properties": { "type": { "const": "call" }, "name": { "$ref": "#/$defs/identificador" }, "args": { "type": "string", "description": "Argumentos separados por coma." } },
          "required": ["name"]
        },
        {
          "properties": { "type": { "const": "comment" }, "text": { "type": "string" } },
          "required": ["text"]
        }
      ]
    }
  }
}
