{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://aureality.info/are/0.1/schema.are.json",
  "title": "Aureality Reality Entity (.are) v0.1",
  "description": "One scale-invariant container for any entity, from an atom to the universe. Spec: RFC-0001-ARE-Format.md. Readers must keep fields they do not understand.",
  "type": "object",
  "required": [
    "header",
    "spatial",
    "graph",
    "mind_sense",
    "timeline",
    "ledger"
  ],
  "properties": {
    "header": {
      "type": "object",
      "required": [
        "did",
        "name",
        "version",
        "scale_exponent",
        "privacy_mode",
        "merkle_root"
      ],
      "properties": {
        "did": {
          "$ref": "#/$defs/did"
        },
        "name": {
          "type": "string",
          "minLength": 1
        },
        "version": {
          "type": "string",
          "pattern": "^0\\.1\\.[0-9]+$"
        },
        "scale_exponent": {
          "type": "number",
          "minimum": -35,
          "maximum": 27,
          "description": "Order of magnitude of the entity's size in metres (10^n m): -10 atom, 0 chair, 26 observable universe."
        },
        "privacy_mode": {
          "enum": [
            "LOCAL_PRIVATE",
            "ENCRYPTED_SHARED",
            "PUBLIC_ONCHAIN"
          ]
        },
        "merkle_root": {
          "anyOf": [
            {
              "const": ""
            },
            {
              "$ref": "#/$defs/hash"
            }
          ]
        },
        "created_at": {
          "type": "string",
          "format": "date-time"
        }
      }
    },
    "spatial": {
      "anyOf": [
        {
          "$ref": "#/$defs/redacted"
        },
        {
          "type": "object",
          "properties": {
            "coordinates": {
              "type": "object",
              "properties": {
                "reference_frame": {
                  "type": "string",
                  "description": "did of the parent entity, or a named frame such as 'root', 'WGS84' or 'comoving'."
                },
                "position": {
                  "type": "array",
                  "items": {
                    "type": "number"
                  },
                  "minItems": 3,
                  "maxItems": 3,
                  "description": "Metres from the origin of the reference frame."
                },
                "rotation_quaternion": {
                  "type": "array",
                  "items": {
                    "type": "number"
                  },
                  "minItems": 4,
                  "maxItems": 4
                }
              }
            },
            "geo": {
              "type": "object",
              "properties": {
                "lat": {
                  "type": "number",
                  "minimum": -90,
                  "maximum": 90
                },
                "lon": {
                  "type": "number",
                  "minimum": -180,
                  "maximum": 180
                },
                "alt_m": {
                  "type": "number"
                }
              }
            },
            "physical_properties": {
              "type": "object",
              "properties": {
                "mass_kg": {
                  "type": "number",
                  "minimum": 0
                },
                "dimensions_m": {
                  "type": "array",
                  "items": {
                    "type": "number",
                    "minimum": 0
                  },
                  "minItems": 3,
                  "maxItems": 3
                },
                "density_kg_m3": {
                  "type": "number",
                  "minimum": 0
                },
                "materials": {
                  "type": "array",
                  "items": {
                    "type": "object",
                    "properties": {
                      "part": {
                        "type": "string"
                      },
                      "did": {
                        "$ref": "#/$defs/did"
                      },
                      "share": {
                        "type": "number",
                        "minimum": 0,
                        "maximum": 1
                      }
                    }
                  }
                },
                "composition": {
                  "type": "array",
                  "items": {
                    "type": "object",
                    "required": [
                      "did"
                    ],
                    "properties": {
                      "did": {
                        "$ref": "#/$defs/did"
                      },
                      "share": {
                        "type": "number",
                        "minimum": 0,
                        "maximum": 1
                      }
                    }
                  }
                },
                "condition": {
                  "type": "object",
                  "properties": {
                    "grade": {
                      "enum": [
                        "new",
                        "like_new",
                        "good",
                        "worn",
                        "damaged",
                        "broken"
                      ]
                    },
                    "note": {
                      "type": "string"
                    }
                  }
                },
                "quantum_atomic_state": {
                  "type": "object"
                }
              }
            },
            "geometry_refs": {
              "type": "array",
              "items": {
                "type": "object",
                "required": [
                  "format",
                  "uri"
                ],
                "properties": {
                  "lod": {
                    "type": "integer",
                    "minimum": 0
                  },
                  "format": {
                    "enum": [
                      "USDZ",
                      "GLTF",
                      "GAUSSIAN_SPLAT",
                      "POINT_CLOUD",
                      "MESH_OBJ",
                      "ATOMIC_LATTICE"
                    ]
                  },
                  "uri": {
                    "type": "string"
                  },
                  "sha256": {
                    "$ref": "#/$defs/hash"
                  }
                }
              }
            }
          }
        }
      ]
    },
    "graph": {
      "anyOf": [
        {
          "$ref": "#/$defs/redacted"
        },
        {
          "type": "object",
          "properties": {
            "parent_did": {
              "anyOf": [
                {
                  "$ref": "#/$defs/did"
                },
                {
                  "type": "null"
                }
              ],
              "description": "Where this entity lives (top-down context)."
            },
            "children_dids": {
              "type": "array",
              "items": {
                "$ref": "#/$defs/did"
              },
              "description": "Entities whose parent is this one: parts and contents."
            },
            "relations": {
              "type": "array",
              "items": {
                "type": "object",
                "required": [
                  "type",
                  "target_did"
                ],
                "properties": {
                  "type": {
                    "enum": [
                      "made_of",
                      "instance_of",
                      "part_of",
                      "near",
                      "replaces",
                      "same_as"
                    ]
                  },
                  "target_did": {
                    "$ref": "#/$defs/did"
                  }
                }
              }
            }
          }
        }
      ]
    },
    "mind_sense": {
      "anyOf": [
        {
          "$ref": "#/$defs/redacted"
        },
        {
          "type": "object",
          "properties": {
            "ontology_tags": {
              "type": "array",
              "items": {
                "type": "string"
              }
            },
            "summary": {
              "type": "string"
            },
            "identifiers": {
              "type": "object",
              "description": "Ids in other registries, e.g. wikidata, gs1, isbn.",
              "additionalProperties": {
                "type": "string"
              }
            },
            "human_notes": {
              "type": "array",
              "items": {
                "anyOf": [
                  {
                    "type": "string"
                  },
                  {
                    "type": "object",
                    "properties": {
                      "text": {
                        "type": "string"
                      },
                      "audio_uri": {
                        "type": "string"
                      },
                      "author": {
                        "type": "string"
                      },
                      "created_at": {
                        "type": "string",
                        "format": "date-time"
                      }
                    }
                  }
                ]
              }
            },
            "tasks": {
              "type": "array",
              "items": {
                "type": "object",
                "required": [
                  "text"
                ],
                "properties": {
                  "text": {
                    "type": "string"
                  },
                  "done": {
                    "type": "boolean"
                  },
                  "due": {
                    "type": "string"
                  }
                }
              }
            },
            "sort_plan": {
              "enum": [
                "KEEP",
                "SELL",
                "DONATE",
                "REPAIR",
                "RECYCLE"
              ]
            },
            "ai": {
              "type": "object",
              "properties": {
                "context": {
                  "type": "string"
                },
                "confidence": {
                  "type": "number",
                  "minimum": 0,
                  "maximum": 1
                },
                "model": {
                  "type": "string"
                }
              }
            },
            "embeddings": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "model": {
                    "type": "string"
                  },
                  "uri": {
                    "type": "string"
                  }
                }
              }
            },
            "sensory_aura": {
              "type": "object",
              "properties": {
                "attention_priority": {
                  "type": "number",
                  "minimum": 0,
                  "maximum": 1
                },
                "ambient_profile": {
                  "type": "string"
                }
              }
            }
          }
        }
      ]
    },
    "timeline": {
      "type": "object",
      "required": [
        "head_commit",
        "history"
      ],
      "properties": {
        "head_commit": {
          "anyOf": [
            {
              "const": ""
            },
            {
              "$ref": "#/$defs/hash"
            }
          ]
        },
        "history": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "id",
              "event",
              "parent",
              "root",
              "timestamp"
            ],
            "properties": {
              "id": {
                "$ref": "#/$defs/hash"
              },
              "event": {
                "type": "string",
                "description": "created, moved, repaired, sold, donated, transferred, anchored, …"
              },
              "parent": {
                "anyOf": [
                  {
                    "$ref": "#/$defs/hash"
                  },
                  {
                    "type": "null"
                  }
                ]
              },
              "root": {
                "$ref": "#/$defs/hash"
              },
              "timestamp": {
                "type": "string",
                "format": "date-time"
              },
              "note": {
                "type": "string"
              }
            }
          }
        },
        "$encrypted": {
          "$ref": "#/$defs/encrypted"
        },
        "base_commit": {
          "$ref": "#/$defs/hash",
          "description": "Parent of the first visible commit, when older history is encrypted or not shared."
        }
      }
    },
    "ledger": {
      "anyOf": [
        {
          "$ref": "#/$defs/redacted"
        },
        {
          "type": "object",
          "required": [
            "encryption_scheme"
          ],
          "properties": {
            "owner": {
              "type": "string",
              "description": "did or label of the current owner."
            },
            "owner_pubkey": {
              "type": "string"
            },
            "shared_with": {
              "type": "array",
              "items": {
                "type": "string"
              }
            },
            "encryption_scheme": {
              "enum": [
                "NONE",
                "AES-256-GCM",
                "ZK-SNARK-SEALED"
              ]
            },
            "license": {
              "type": "string",
              "description": "SPDX id, e.g. CC0-1.0 for public templates."
            },
            "value": {
              "$ref": "#/$defs/money"
            },
            "asking_price": {
              "$ref": "#/$defs/money"
            },
            "sale_price": {
              "$ref": "#/$defs/money"
            },
            "storage_cid": {
              "type": [
                "string",
                "null"
              ]
            },
            "blockchain_anchor": {
              "type": [
                "object",
                "null"
              ],
              "properties": {
                "chain": {
                  "type": "string"
                },
                "tx": {
                  "type": "string"
                },
                "root": {
                  "$ref": "#/$defs/hash"
                }
              }
            },
            "key_wraps": {
              "type": "array",
              "description": "The content key, wrapped once per person in shared_with.",
              "items": {
                "type": "object",
                "required": [
                  "recipient",
                  "alg",
                  "epk",
                  "iv",
                  "wrapped"
                ],
                "properties": {
                  "recipient": {
                    "type": "string"
                  },
                  "alg": {
                    "const": "ECDH-P256+HKDF-SHA256+A256GCM"
                  },
                  "epk": {
                    "type": "string"
                  },
                  "iv": {
                    "type": "string"
                  },
                  "wrapped": {
                    "type": "string"
                  }
                }
              }
            }
          }
        }
      ]
    }
  },
  "$defs": {
    "did": {
      "type": "string",
      "pattern": "^did:are:[a-zA-Z0-9._:-]+$"
    },
    "hash": {
      "type": "string",
      "pattern": "^sha256:[0-9a-f]{64}$"
    },
    "redacted": {
      "type": "object",
      "required": [
        "$redacted"
      ],
      "properties": {
        "$redacted": {
          "$ref": "#/$defs/hash"
        },
        "$encrypted": {
          "$ref": "#/$defs/encrypted"
        }
      },
      "additionalProperties": false,
      "description": "A hidden layer: its leaf hash, and its ciphertext when encrypted."
    },
    "money": {
      "type": "object",
      "required": [
        "amount",
        "currency"
      ],
      "properties": {
        "amount": {
          "type": "number",
          "minimum": 0
        },
        "currency": {
          "type": "string",
          "pattern": "^[A-Z]{3}$"
        },
        "basis": {
          "enum": [
            "estimate",
            "receipt",
            "appraisal",
            "market"
          ]
        }
      }
    },
    "encrypted": {
      "type": "object",
      "required": [
        "alg",
        "iv",
        "ciphertext"
      ],
      "properties": {
        "alg": {
          "const": "AES-256-GCM"
        },
        "iv": {
          "type": "string"
        },
        "ciphertext": {
          "type": "string"
        },
        "kdf": {
          "type": "object",
          "required": [
            "name",
            "salt",
            "iterations"
          ],
          "properties": {
            "name": {
              "const": "PBKDF2-SHA256"
            },
            "salt": {
              "type": "string"
            },
            "iterations": {
              "type": "integer",
              "minimum": 100000
            }
          }
        }
      }
    }
  }
}
