{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://regionly.dimsylaisolutions.com/schema/regionly-v1.schema.json",
  "title": "Regionly export v1",
  "description": "Labeled image regions with geometry in both image pixels and normalized 0-1 coordinates. Origin is the image's top-left corner; x increases rightward, y increases downward.",
  "type": "object",
  "required": ["format", "version", "image", "labels", "stats"],
  "properties": {
    "format": { "const": "regionly" },
    "version": { "type": "string", "pattern": "^1\\." },
    "generator": { "type": "string" },
    "exportedAt": { "type": "string", "format": "date-time" },
    "image": {
      "type": "object",
      "required": ["name", "width", "height"],
      "properties": {
        "name": { "type": "string" },
        "width": { "type": "number", "exclusiveMinimum": 0 },
        "height": { "type": "number", "exclusiveMinimum": 0 }
      }
    },
    "coordinateSystem": {
      "type": "object",
      "properties": {
        "pixels": { "type": "string" },
        "normalized": { "type": "string" }
      }
    },
    "labels": {
      "type": "array",
      "items": {
        "type": "object",
        "required": ["label", "regions"],
        "properties": {
          "label": { "type": "string" },
          "color": { "type": ["string", "null"] },
          "regions": {
            "type": "array",
            "items": { "$ref": "#/$defs/region" }
          }
        }
      }
    },
    "stats": {
      "type": "object",
      "required": ["labelCount", "regionCount"],
      "properties": {
        "labelCount": { "type": "integer", "minimum": 0 },
        "regionCount": { "type": "integer", "minimum": 0 }
      }
    }
  },
  "$defs": {
    "region": {
      "type": "object",
      "required": ["id", "type", "pixels", "normalized", "boundingBox"],
      "properties": {
        "id": { "type": "string" },
        "type": { "enum": ["rect", "circle", "polygon", "line", "point"] },
        "source": { "enum": ["polygon", "freeform"] },
        "arrow": { "type": "boolean" },
        "note": { "type": "string" },
        "pixels": { "$ref": "#/$defs/geometry" },
        "normalized": { "$ref": "#/$defs/geometry" },
        "boundingBox": {
          "type": "object",
          "required": ["pixels", "normalized"],
          "properties": {
            "pixels": { "$ref": "#/$defs/box" },
            "normalized": { "$ref": "#/$defs/box" }
          }
        }
      }
    },
    "geometry": {
      "type": "object",
      "description": "Shape-dependent: rect {x,y,width,height} · circle {cx,cy,radius | radiusX,radiusY} · polygon {points: [[x,y],...]} · line {x1,y1,x2,y2} · point {x,y}"
    },
    "box": {
      "type": "object",
      "required": ["x", "y", "width", "height"],
      "properties": {
        "x": { "type": "number" },
        "y": { "type": "number" },
        "width": { "type": "number" },
        "height": { "type": "number" }
      }
    }
  }
}
