{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://elnazkarami.github.io/N-DOS/schemas/session-metadata/0.1.json",
  "title": "NDOS Session Metadata",
  "description": "Sessions joined to the animal they came from and the procedures that preceded them, produced from reviewed spreadsheets. Every value records how it is known: observed from the filesystem, declared by a person, or computed by NDOS. Values nobody has supplied are absent rather than empty, so 'not yet entered' is never confused with 'checked and unknown'.",
  "type": "object",
  "required": ["metadata_version", "generated_at", "generator", "session_count", "sessions"],
  "properties": {
    "metadata_version": { "const": "0.1" },
    "generated_at": {
      "type": "string",
      "format": "date-time",
      "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z$"
    },
    "generator": {
      "type": "object",
      "required": ["name", "version"],
      "properties": {
        "name": { "type": "string", "minLength": 1 },
        "version": { "type": "string", "minLength": 1 }
      },
      "additionalProperties": false
    },
    "session_count": { "type": "integer", "minimum": 0 },
    "sessions": { "type": "array", "items": { "$ref": "#/$defs/session" } },
    "extensions": { "type": "object" }
  },
  "additionalProperties": false,
  "$defs": {
    "session": {
      "type": "object",
      "required": ["ndos_id", "observed", "declared"],
      "properties": {
        "ndos_id": {
          "description": "Stable identifier derived from the group's path. Renaming a directory currently yields a new identifier.",
          "type": "string",
          "pattern": "^ndos-[a-f0-9]{10}$"
        },
        "observed": { "$ref": "#/$defs/observed" },
        "declared": {
          "description": "Facts true of this recording only.",
          "type": "object",
          "propertyNames": {
            "enum": ["subject_id", "session_date", "session_type", "task", "qc_status", "notes"]
          },
          "additionalProperties": { "$ref": "#/$defs/field" }
        },
        "animal": { "$ref": "#/$defs/animal" },
        "procedures": {
          "description": "Procedures recorded for this animal, in the order entered. Present only when at least one exists.",
          "type": "array",
          "items": { "$ref": "#/$defs/procedure" }
        },
        "derived": { "$ref": "#/$defs/derived" },
        "extensions": { "type": "object" }
      },
      "additionalProperties": false
    },
    "observed": {
      "description": "Facts read from the filesystem. Never entered by hand, and refreshed on every scan.",
      "type": "object",
      "required": ["path", "file_count", "bytes"],
      "properties": {
        "path": { "type": "string" },
        "folder_subject": { "type": "string" },
        "folder_session": { "type": "string" },
        "match": {
          "description": "How well the folder names fit the shapes expected of a session. An inference, not a conclusion.",
          "type": "string",
          "enum": ["subject and session", "subject only", "session only", "no pattern match"]
        },
        "file_count": { "type": "integer", "minimum": 0 },
        "bytes": { "type": "integer", "minimum": 0 },
        "modalities": { "type": "array", "items": { "type": "string" } },
        "earliest": { "type": "string" },
        "latest": { "type": "string" }
      },
      "additionalProperties": false
    },
    "animal": {
      "description": "The subject this session was recorded from. Recorded once and reaching every session, rather than retyped per row.",
      "type": "object",
      "required": ["subject_id", "declared"],
      "properties": {
        "subject_id": { "type": "string", "minLength": 1 },
        "declared": {
          "type": "object",
          "propertyNames": {
            "enum": ["subject_id", "species", "strain", "sex", "date_of_birth", "genotype", "source", "notes"]
          },
          "additionalProperties": { "$ref": "#/$defs/field" }
        }
      },
      "additionalProperties": false
    },
    "procedure": {
      "description": "Something done to the animal: a surgery, injection, implant, drug, or training event. One procedure typically governs many later sessions.",
      "type": "object",
      "required": ["procedure_id", "declared"],
      "properties": {
        "procedure_id": { "type": "string" },
        "declared": {
          "type": "object",
          "propertyNames": {
            "enum": ["procedure_id", "subject_id", "procedure_date", "procedure_type", "target_region", "construct_or_drug", "dose", "notes"]
          },
          "additionalProperties": { "$ref": "#/$defs/field" }
        },
        "days_before_session": {
          "description": "Whole days from the procedure to the session. Negative when the procedure happened afterwards.",
          "type": "integer"
        },
        "relation": { "type": "string", "enum": ["before", "after"] }
      },
      "additionalProperties": false
    },
    "derived": {
      "description": "Intervals computed by NDOS from dates already recorded, never typed by hand. days_since_<procedure type> gives the most recent preceding procedure of that type.",
      "type": "object",
      "propertyNames": { "pattern": "^(age_days|days_since_[a-z0-9_]+)$" },
      "additionalProperties": {
        "type": "object",
        "required": ["value", "status"],
        "properties": {
          "value": { "type": "string", "pattern": "^-?\\d+$" },
          "status": { "const": "computed" }
        },
        "additionalProperties": false
      }
    },
    "field": {
      "type": "object",
      "required": ["value", "status"],
      "properties": {
        "value": { "type": "string", "minLength": 1 },
        "status": {
          "description": "'declared' is an assertion by a person; 'unknown' records that it was checked and cannot be determined.",
          "type": "string",
          "enum": ["declared", "unknown"]
        },
        "as_entered": {
          "description": "The original text, present only when NDOS mapped it onto a controlled term, so the mapping can be audited.",
          "type": "string"
        }
      },
      "additionalProperties": false
    }
  }
}
