{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://itakeapart.com/api/teardown.schema.json",
  "title": "iTakeApart teardown specification",
  "description": "One device's teardown as deterministic data. Millimetres; y up, x right, z towards the viewer; positions at step 0 of the teardown order, the device closed and not exploded. Mirrors types/teardown.ts.",
  "type": "object",
  "additionalProperties": false,
  "required": ["schemaVersion", "slug", "url", "title", "device", "category", "kind", "units", "sourceUnits", "reconstructionNote", "affiliate", "parts", "screws", "dangerZones", "steps", "sources"],
  "properties": {
    "schemaVersion": { "const": "1.0.0" },
    "slug": { "type": "string", "pattern": "^[a-z0-9]+(-[a-z0-9]+)*$" },
    "url": { "type": "string", "pattern": "^https://" },
    "title": { "type": "string", "minLength": 1 },
    "device": { "type": "string", "minLength": 1 },
    "category": { "enum": ["phones", "gaming", "laptops", "appliances", "cars", "peripherals", "how"] },
    "kind": { "enum": ["howto", "explainer", "wheelnut"] },
    "risk": { "enum": ["LOW", "MED", "HIGH"] },
    "units": { "const": "mm" },
    "sourceUnits": { "enum": ["mm", "cm"] },
    "difficulty": { "type": "string" },
    "estimatedTime": { "type": "string", "pattern": "^P(T(\\d+H)?(\\d+M)?(\\d+S)?|\\d+D.*)$" },
    "trademarks": { "type": "string" },
    "reconstructionNote": { "type": "string", "minLength": 1 },
    "affiliate": { "type": "boolean" },
    "parts": { "type": "array", "items": { "$ref": "#/$defs/PartSpec" } },
    "screws": { "type": "array", "items": { "$ref": "#/$defs/ScrewSpec" } },
    "dangerZones": { "type": "array", "items": { "$ref": "#/$defs/DangerZoneSpec" } },
    "steps": { "type": "array", "minItems": 1, "items": { "$ref": "#/$defs/TeardownStep" } },
    "sources": { "type": "array", "items": { "$ref": "#/$defs/SourceSpec" } }
  },
  "$defs": {
    "Vec3": { "type": "array", "minItems": 3, "maxItems": 3, "items": { "type": "number" } },
    "PartSpec": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "name"],
      "properties": {
        "id": { "type": "string", "minLength": 1 },
        "name": { "type": "string", "minLength": 1 },
        "kind": { "type": "string" },
        "layer": { "type": "number" }
      }
    },
    "ScrewSpec": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "code", "headType", "colorCode", "lengthMm", "position"],
      "properties": {
        "id": { "type": "string", "minLength": 1 },
        "code": { "type": "string", "minLength": 1 },
        "headType": { "type": "string", "minLength": 1 },
        "colorCode": { "type": "string", "pattern": "^#[0-9a-fA-F]{6}$" },
        "lengthMm": { "type": "number", "minimum": 0 },
        "position": { "$ref": "#/$defs/Vec3" },
        "warning": { "type": "string" },
        "toolAffiliateUrl": { "type": "string", "pattern": "^https://" }
      }
    },
    "DangerZoneSpec": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "label", "severity", "description", "boundingBox"],
      "properties": {
        "id": { "type": "string", "minLength": 1 },
        "label": { "type": "string", "minLength": 1 },
        "severity": { "enum": ["critical", "warning"] },
        "maxInsertionDepthMm": { "type": "number", "minimum": 0 },
        "description": { "type": "string", "minLength": 1 },
        "boundingBox": {
          "type": "object",
          "additionalProperties": false,
          "required": ["center", "size"],
          "properties": { "center": { "$ref": "#/$defs/Vec3" }, "size": { "$ref": "#/$defs/Vec3" } }
        }
      }
    },
    "AffiliatePartCard": {
      "type": "object",
      "additionalProperties": false,
      "required": ["partId", "title", "priceEstimate", "vendor", "affiliateUrl", "bundledTools"],
      "properties": {
        "partId": { "type": "string", "minLength": 1 },
        "title": { "type": "string", "minLength": 1 },
        "sku": { "type": "string" },
        "priceEstimate": { "type": "string" },
        "vendor": { "enum": ["ifixit", "amazon", "oem"] },
        "affiliateUrl": { "type": "string", "pattern": "^https://" },
        "bundledTools": { "type": "array", "items": { "type": "string" } },
        "whileYouAreInsideUpgrade": {
          "type": "object",
          "additionalProperties": false,
          "required": ["title", "affiliateUrl"],
          "properties": { "title": { "type": "string", "minLength": 1 }, "affiliateUrl": { "type": "string", "pattern": "^https://" } }
        }
      }
    },
    "TeardownStep": {
      "type": "object",
      "additionalProperties": false,
      "required": ["stepNumber", "title", "instruction", "cameraPosition", "cameraTarget", "activePartIds", "explodeVectors", "screwsRemovedInStep", "dangerZonesActive", "xrayRecommended"],
      "properties": {
        "stepNumber": { "type": "integer", "minimum": 1 },
        "title": { "type": "string", "pattern": "^Step \\d+: " },
        "instruction": { "type": "string", "minLength": 1 },
        "cameraPosition": { "$ref": "#/$defs/Vec3" },
        "cameraTarget": { "$ref": "#/$defs/Vec3" },
        "activePartIds": { "type": "array", "items": { "type": "string" } },
        "explodeVectors": { "type": "object", "additionalProperties": { "$ref": "#/$defs/Vec3" } },
        "screwsRemovedInStep": { "type": "array", "items": { "type": "string" } },
        "dangerZonesActive": { "type": "array", "items": { "type": "string" } },
        "xrayRecommended": { "type": "boolean" },
        "contextualPartOffer": { "$ref": "#/$defs/AffiliatePartCard" }
      }
    },
    "SourceSpec": {
      "type": "object",
      "additionalProperties": false,
      "required": ["label", "url"],
      "properties": { "label": { "type": "string" }, "url": { "type": "string" } }
    }
  }
}
