{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:mobilespec:L2.screenflows",
  "title": "Screenflow Schema (L2)",
  "type": "object",
  "required": ["screen"],
  "additionalProperties": false,

  "properties": {
    "screen": {
      "type": "object",
      "required": ["id", "name", "type", "transitions"],
      "additionalProperties": false,
      "properties": {
        "id": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9_]*$"
        },
        "name": {
          "type": "string"
        },
        "type": {
          "type": "string",
          "enum": ["screen", "choice"],
          "description": "screen: visible UI screen, choice: invisible branching node (guard-based)"
        },

        "context": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9_]*$"
        },

        "entry": {
          "type": "boolean"
        },

        "exit": {
          "type": "boolean"
        },

        "transitions": {
          "type": "array",
          "minItems": 0,
          "items": {
            "$ref": "#/$defs/transition"
          }
        }
      }
    }
  },

  "$defs": {
    "transition": {
      "type": "object",
      "required": ["id", "trigger", "target"],
      "additionalProperties": false,
      "properties": {
        "id": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9_]*$",
          "description": "Semantic action id (open_xxx, back_to_xxx, reload_xxx)"
        },
        "trigger": {
          "type": "string",
          "enum": ["tap", "auto"]
        },
        "target": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9_]*$"
        },
        "targetContext": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9_]*$"
        },

        "guard": {
          "type": "string",
          "minLength": 1,
          "description": "Guard condition name (boolean). When present, transition is taken only if guard evaluates true."
        },
        "else": {
          "type": "boolean",
          "description": "Default branch for choice node. At most one outgoing transition from a choice node may set else=true."
        }
      }
    }
  }
}
