{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://muthu4all.github.io/compile-once-enterprise/spec/schemas/sphere-contract.schema.json",
  "title": "Compile-Once Sphere Contract",
  "description": "The versioned definition of a Data Sphere: how it is reached, what it contains, the rules it guarantees and who owns it. Draft v0.1.",
  "type": "object",
  "required": ["spec", "sphere", "version", "owners", "sources", "entities", "access"],
  "properties": {
    "spec": { "const": "compile-once/sphere-contract/v0.1" },
    "sphere": { "type": "string", "pattern": "^[a-z][a-z0-9-]{1,40}$", "description": "Stable sphere id, e.g. claims." },
    "version": { "type": "string", "pattern": "^v[0-9]+$", "description": "Contract version, e.g. v9. A change is a controlled event that triggers regeneration of bound capsules." },
    "title": { "type": "string" },
    "description": { "type": "string" },
    "owners": {
      "type": "array", "minItems": 1,
      "items": { "type": "object", "required": ["role", "name"], "properties": { "role": { "enum": ["data-owner", "steward", "technical-owner"] }, "name": { "type": "string" } } }
    },
    "sources": {
      "type": "array", "minItems": 1,
      "description": "Where the data comes from. Source systems stay where they are.",
      "items": {
        "type": "object", "required": ["system", "connection_ref", "freshness"],
        "properties": {
          "system": { "type": "string" },
          "connection_ref": { "type": "string", "description": "A reference resolved by the platform's secret store, never a credential." },
          "freshness": { "type": "string", "description": "e.g. streaming, every 15 min, nightly." }
        }
      }
    },
    "entities": {
      "type": "array", "minItems": 1,
      "items": {
        "type": "object", "required": ["name", "fields"],
        "properties": {
          "name": { "type": "string" },
          "key": { "type": "array", "items": { "type": "string" } },
          "fields": {
            "type": "array",
            "items": {
              "type": "object", "required": ["name", "type"],
              "properties": {
                "name": { "type": "string" },
                "type": { "enum": ["string", "integer", "number", "boolean", "date", "datetime", "money", "enum", "reference"] },
                "nullable": { "type": "boolean", "default": false },
                "sensitivity": { "enum": ["public", "internal", "confidential", "personal", "special-category"], "default": "internal" },
                "description": { "type": "string" }
              }
            }
          }
        }
      }
    },
    "rules": {
      "type": "array",
      "description": "Data-quality and business rules the sphere guarantees, stated in plain language with an id.",
      "items": { "type": "object", "required": ["id", "statement"], "properties": { "id": { "type": "string" }, "statement": { "type": "string" } } }
    },
    "access": {
      "type": "object", "required": ["default"],
      "properties": {
        "default": { "enum": ["read-only", "none"] },
        "personal_data": { "type": "string", "description": "Legal basis or policy reference for personal data use." },
        "jurisdiction_parameters": { "type": "object", "description": "Country or regulator specific settings applied as contract parameters." }
      }
    },
    "changelog": { "type": "array", "items": { "type": "object", "properties": { "version": { "type": "string" }, "date": { "type": "string", "format": "date" }, "change": { "type": "string" } } } }
  },
  "additionalProperties": false
}
