{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://bimpdf.com/schema/bim-pdf-1.0.schema.json",
  "title": "bim-pdf sidecar format v1.0",
  "description": "Canonical JSON Schema for the bim-pdf sidecar documents embedded in a PDF/A-3 as associated files. Source of truth mirrors packages/core/src/types/*.ts. Schemas are intentionally OPEN (unknown properties allowed) so producers may add namespaced fields without breaking consumers during iteration; required fields and known field types are still enforced.",
  "definitions": {
    "FormatVersion": {
      "type": "string",
      "pattern": "^[0-9]+\\.[0-9]+$",
      "description": "Major.minor format version, e.g. \"1.0\"."
    },
    "Quantity": {
      "type": "object",
      "required": ["value", "unit"],
      "properties": {
        "value": { "type": "number" },
        "unit": {
          "type": "string",
          "description": "SI-ish unit string: m, m2, m3, mm, ft, ft2, ft3, in, etc."
        }
      }
    },
    "BBox": {
      "type": "array",
      "description": "Axis-aligned bbox in PDF points, origin bottom-left, Y-up: [x0, y0, x1, y1].",
      "items": { "type": "number" },
      "minItems": 4,
      "maxItems": 4
    },
    "Point2D": {
      "type": "array",
      "description": "2D point in PDF points: [x, y].",
      "items": { "type": "number" },
      "minItems": 2,
      "maxItems": 2
    },
    "ElementClass": {
      "type": "string",
      "description": "Class registry v1.0: 20 named classes plus 'other'. Values outside this set should use 'other' with class_hint.",
      "enum": [
        "wall", "curtain-wall", "slab", "floor", "column", "beam", "brace", "footing",
        "roof", "door", "window", "curtain-panel", "curtain-mullion",
        "ceiling", "room", "stair", "ramp",
        "furniture", "casework", "equipment",
        "other"
      ]
    },

    "ElementsDocument": {
      "type": "object",
      "required": ["bim-pdf", "meta", "source", "class_registry", "elements"],
      "properties": {
        "bim-pdf": { "$ref": "#/definitions/FormatVersion" },
        "meta": { "$ref": "#/definitions/BimPdfMeta" },
        "source": { "$ref": "#/definitions/BimPdfSource" },
        "class_registry": { "$ref": "#/definitions/FormatVersion" },
        "elements": {
          "type": "array",
          "items": { "$ref": "#/definitions/BimElement" }
        }
      }
    },
    "BimPdfMeta": {
      "type": "object",
      "required": ["project_id", "document_id", "exported_at", "producer"],
      "properties": {
        "project_id": { "type": "string" },
        "document_id": { "type": "string" },
        "exported_at": { "type": "string", "description": "ISO 8601 datetime." },
        "producer": { "type": "string" }
      }
    },
    "BimPdfSource": {
      "type": "object",
      "required": ["format", "id_strategy"],
      "properties": {
        "format": { "type": "string", "description": "Native source format, e.g. 'revit', 'ifc'." },
        "version": { "type": "string" },
        "id_strategy": { "type": "string" },
        "id_stability_notes": { "type": "string" }
      }
    },
    "BimElement": {
      "type": "object",
      "required": ["id", "class"],
      "properties": {
        "id": { "type": "string", "description": "Stable across re-exports of the same model version." },
        "class": { "$ref": "#/definitions/ElementClass" },
        "class_hint": {
          "type": ["string", "null"],
          "description": "Producer's native category name. Required when class === 'other'."
        },
        "label": { "type": "string" },
        "level": { "type": "string" },
        "level_id": { "type": "string", "description": "UUID v5 referencing an entry in levels.json." },
        "quantities": {
          "type": "object",
          "properties": {
            "area": { "$ref": "#/definitions/Quantity" },
            "volume": { "$ref": "#/definitions/Quantity" },
            "length": { "$ref": "#/definitions/Quantity" },
            "width": { "$ref": "#/definitions/Quantity" },
            "height": { "$ref": "#/definitions/Quantity" },
            "perimeter": { "$ref": "#/definitions/Quantity" },
            "count": { "$ref": "#/definitions/Quantity" },
            "weight": { "$ref": "#/definitions/Quantity" }
          }
        },
        "properties": {
          "type": "object",
          "description": "Namespaced property bag: keys prefixed by producer (e.g. 'revit.family')."
        }
      },
      "allOf": [
        {
          "$comment": "class_hint is required when class is 'other'.",
          "if": { "properties": { "class": { "const": "other" } } },
          "then": { "required": ["class_hint"] }
        }
      ]
    },

    "MarksDocument": {
      "type": "object",
      "required": ["bim-pdf", "requires", "coordinate_space", "marks"],
      "properties": {
        "bim-pdf": { "$ref": "#/definitions/FormatVersion" },
        "requires": {
          "type": "array",
          "items": { "type": "string" },
          "contains": { "const": "elements.json" }
        },
        "coordinate_space": { "type": "string" },
        "marks": {
          "type": "array",
          "items": { "$ref": "#/definitions/Mark" }
        }
      }
    },
    "Mark": {
      "type": "object",
      "required": ["element_id", "page", "bbox"],
      "properties": {
        "element_id": { "type": "string" },
        "page": { "type": "integer", "minimum": 1, "description": "1-indexed page number." },
        "bbox": { "$ref": "#/definitions/BBox" },
        "view_type": { "type": "string", "description": "plan | elevation | section | detail | schedule | 3d | (open string)." }
      }
    },

    "ScalesDocument": {
      "type": "object",
      "required": ["bim-pdf", "pages"],
      "properties": {
        "bim-pdf": { "$ref": "#/definitions/FormatVersion" },
        "pages": {
          "type": "object",
          "description": "Keys are 1-indexed page numbers as strings.",
          "additionalProperties": { "$ref": "#/definitions/PageScaleEntry" }
        }
      }
    },
    "PageScaleEntry": {
      "description": "Either a flat PageScale (v1.0, one scale per page) or a viewport-aware PageScaleSet (v1.1+).",
      "oneOf": [
        { "$ref": "#/definitions/PageScale" },
        { "$ref": "#/definitions/PageScaleSet" }
      ]
    },
    "PageScaleSet": {
      "type": "object",
      "description": "Per-page scale info for a multi-viewport sheet. Distinguished from a flat PageScale by the absence of a top-level 'type' discriminant.",
      "not": { "required": ["type"] },
      "properties": {
        "default": { "$ref": "#/definitions/PageScale" },
        "viewports": {
          "type": "array",
          "items": { "$ref": "#/definitions/ScaleViewport" }
        }
      }
    },
    "ScaleViewport": {
      "type": "object",
      "required": ["rect", "scale"],
      "properties": {
        "rect": { "$ref": "#/definitions/BBox" },
        "scale": { "$ref": "#/definitions/PageScale" },
        "name": { "type": "string" },
        "view_id": { "type": "string" }
      }
    },
    "PageScale": {
      "oneOf": [
        { "$ref": "#/definitions/RatioScale" },
        { "$ref": "#/definitions/CalibrationScale" }
      ]
    },
    "RatioScale": {
      "type": "object",
      "required": ["type", "ratio", "unit"],
      "properties": {
        "type": { "const": "ratio" },
        "ratio": { "type": "number", "description": "Denominator of the scale ratio; 100 means 1:100." },
        "unit": { "type": "string" }
      }
    },
    "CalibrationScale": {
      "type": "object",
      "required": ["type", "p1", "p2", "real_distance"],
      "properties": {
        "type": { "const": "calibration" },
        "p1": { "$ref": "#/definitions/Point2D" },
        "p2": { "$ref": "#/definitions/Point2D" },
        "real_distance": { "$ref": "#/definitions/Quantity" }
      }
    },

    "LevelsDocument": {
      "type": "object",
      "required": ["bim-pdf", "levels"],
      "properties": {
        "bim-pdf": { "$ref": "#/definitions/FormatVersion" },
        "levels": {
          "type": "array",
          "items": { "$ref": "#/definitions/BimLevel" }
        }
      }
    },
    "BimLevel": {
      "type": "object",
      "required": ["id", "name", "elevation"],
      "properties": {
        "id": { "type": "string", "description": "UUID v5; must match element.level_id references." },
        "name": { "type": "string" },
        "elevation": { "$ref": "#/definitions/Quantity" }
      }
    },

    "TitleblockDocument": {
      "type": "object",
      "required": ["bim-pdf", "sheets"],
      "properties": {
        "bim-pdf": { "$ref": "#/definitions/FormatVersion" },
        "sheets": {
          "type": "array",
          "items": { "$ref": "#/definitions/TitleBlockSheet" }
        }
      }
    },
    "TitleBlockSheet": {
      "type": "object",
      "required": ["page"],
      "properties": {
        "page": { "type": "integer", "minimum": 1, "description": "1-indexed page number." },
        "sheet_number": { "type": "string" },
        "sheet_name": { "type": "string" },
        "project_name": { "type": "string" },
        "project_number": { "type": "string" },
        "date": { "type": "string" },
        "scale": { "type": "string" },
        "drawn_by": { "type": "string" },
        "checked_by": { "type": "string" },
        "revision": { "type": "string" }
      }
    },

    "AnnotationsDocument": {
      "type": "object",
      "required": ["bim-pdf", "requires", "coordinate_space", "annotations"],
      "properties": {
        "bim-pdf": { "$ref": "#/definitions/FormatVersion" },
        "requires": {
          "type": "array",
          "items": { "type": "string" },
          "contains": { "const": "elements.json" }
        },
        "coordinate_space": { "type": "string" },
        "annotations": {
          "type": "array",
          "items": { "$ref": "#/definitions/Annotation" }
        }
      }
    },
    "AnnotationType": {
      "type": "string",
      "description": "Markup types (human/agent authored) plus producer-extracted types (dimension, tag, detail, other).",
      "enum": [
        "polygon", "polyline", "rectangle", "circle", "text",
        "arrow", "callout", "cloud", "stamp", "highlight", "freehand", "count",
        "dimension", "tag", "detail", "other"
      ]
    },
    "AnnotationAuthor": {
      "type": "object",
      "required": ["id", "display", "type"],
      "properties": {
        "id": { "type": "string" },
        "display": { "type": "string" },
        "type": { "type": "string", "enum": ["human", "agent"] }
      }
    },
    "AnnotationStyle": {
      "type": "object",
      "properties": {
        "color": { "type": "string" },
        "fill_color": { "type": "string" },
        "line_width": { "type": "number" },
        "opacity": { "type": "number" }
      }
    },
    "Annotation": {
      "type": "object",
      "required": ["id", "page", "type", "author", "created_at"],
      "properties": {
        "id": { "type": "string" },
        "page": { "type": "integer", "minimum": 1 },
        "type": { "$ref": "#/definitions/AnnotationType" },
        "points": {
          "type": "array",
          "items": { "$ref": "#/definitions/Point2D" },
          "description": "Vertices for polygon, polyline, arrow, callout."
        },
        "bbox": { "$ref": "#/definitions/BBox" },
        "label": { "type": "string" },
        "author": { "$ref": "#/definitions/AnnotationAuthor" },
        "created_at": { "type": "string", "description": "ISO 8601 datetime." },
        "element_ids": {
          "type": "array",
          "items": { "type": "string" },
          "description": "Element association: links to zero or more elements in elements.json. A single-target markup uses a one-entry array; a region markup (e.g. a revision cloud enclosing several elements) carries many. Absent/empty = freeform annotation."
        },
        "label_ref": {
          "type": ["string", "null"],
          "description": "Id of a text/callout annotation that labels this graphic markup; absent/null = inline label or proximity-inferred."
        },
        "group_id": {
          "type": "string",
          "description": "Shared id clustering annotations that form one logical markup unit (e.g. a cloud + the callout labeling it)."
        },
        "style": { "$ref": "#/definitions/AnnotationStyle" },
        "measurement": {
          "type": "object",
          "properties": {
            "area": { "$ref": "#/definitions/Quantity" },
            "perimeter": { "$ref": "#/definitions/Quantity" },
            "length": { "$ref": "#/definitions/Quantity" }
          }
        }
      }
    }
  }
}
