{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://spec.blackridgegeo.com/chain-of-custody/v1/schema.json",
  "title": "Inspection Chain of Custody, version 1",
  "description": "A C2PA assertion stating how an infrastructure-inspection deliverable came to exist: who captured it, who processed it, how the finding was classified, and who approved it. Written under the assertion label io.bmip.chain-of-custody.",
  "type": "object",
  "required": [
    "schema", "version", "issuer", "deliverable_id", "organization_id",
    "capture", "processing", "approval", "attestation"
  ],
  "additionalProperties": true,
  "properties": {
    "schema": { "type": "string", "format": "uri", "description": "The published definition this assertion conforms to." },
    "version": { "type": "integer", "minimum": 1, "description": "Major version. A breaking change takes a new version and a new URL." },
    "issuer": {
      "type": "object",
      "description": "Who is making the claim. Required: the identifiers below are scoped to this issuer and are meaningless without it.",
      "required": ["name"],
      "properties": {
        "name": { "type": "string", "minLength": 1 },
        "organization_id": { "$ref": "#/$defs/identifier" }
      }
    },

    "deliverable_id": { "$ref": "#/$defs/identifier" },
    "organization_id": { "$ref": "#/$defs/identifier" },
    "project_id": { "$ref": "#/$defs/identifier" },
    "mission_id": { "$ref": "#/$defs/identifier" },
    "audit_event_id": { "$ref": "#/$defs/identifier" },

    "capture": {
      "type": "object",
      "description": "How the source data was obtained. Only raw_capture_sha256 is core; the rest is context and may be null.",
      "required": ["raw_capture_sha256"],
      "properties": {
        "operator_id": { "$ref": "#/$defs/identifier" },
        "operator_name": { "type": ["string", "null"] },
        "aircraft": { "type": ["string", "null"], "description": "Platform that carried the sensor. Null when not recorded." },
        "sensor": { "type": ["string", "null"] },
        "captured_at": { "type": ["string", "null"], "format": "date-time" },
        "airspace_authorization": {
          "type": ["object", "null"],
          "required": ["type", "id"],
          "properties": {
            "type": { "type": "string", "description": "Authorization scheme, e.g. LAANC." },
            "id": { "type": "string" },
            "ceiling_ft_agl": { "type": ["number", "null"] }
          }
        },
        "raw_capture_sha256": {
          "type": "string", "pattern": "^[0-9a-f]{64}$",
          "description": "CORE. SHA-256 of the capture as received, before any processing."
        }
      }
    },

    "processing": {
      "type": "object",
      "description": "The external provider that produced this deliverable from the capture.",
      "properties": {
        "provider_slug": { "type": ["string", "null"] },
        "provider_name": { "type": ["string", "null"] },
        "external_job_id": { "type": ["string", "null"], "description": "The provider's own job reference, so the claim can be audited against them." },
        "submitted_at": { "type": ["string", "null"], "format": "date-time" },
        "completed_at": { "type": ["string", "null"], "format": "date-time" }
      }
    },

    "finding": {
      "type": ["object", "null"],
      "description": "The observation this deliverable evidences, if it carries one.",
      "properties": {
        "finding_id": { "$ref": "#/$defs/identifier" },
        "classification": { "$ref": "#/$defs/classification" },
        "severity": {
          "type": ["string", "null"],
          "description": "Null unless the issuer operates a severity scheme. Null means not classified — never treat it as low."
        },
        "measured_value": {
          "type": ["object", "null"],
          "required": ["value", "units"],
          "properties": {
            "value": { "type": "number" },
            "units": { "type": "string" },
            "label": { "type": ["string", "null"] }
          }
        }
      }
    },

    "approval": {
      "type": "object",
      "description": "CORE. Who produced the deliverable and who signed it off. A claim without this attests to nothing.",
      "required": ["produced_by_id", "approved_by_id", "approved_at", "distinct_producer_and_approver"],
      "properties": {
        "produced_by_id": { "$ref": "#/$defs/identifier" },
        "approved_by_id": { "$ref": "#/$defs/identifier" },
        "approved_by_name": { "type": ["string", "null"] },
        "approved_at": { "type": "string", "format": "date-time" },
        "separation_of_duties_enforced": { "type": "boolean" },
        "distinct_producer_and_approver": {
          "const": true,
          "description": "Must be true. An assertion claiming otherwise is invalid: a deliverable approved by the person who produced it has not been reviewed."
        }
      }
    },

    "retention": {
      "type": ["object", "null"],
      "properties": {
        "class": { "type": "string" },
        "delete_after": { "type": ["string", "null"], "format": "date" },
        "policy": { "type": ["string", "null"] }
      }
    },

    "attestation": {
      "type": "object",
      "description": "What the issuer could NOT establish, and why. Honesty about gaps is part of the claim.",
      "required": ["complete", "absent"],
      "properties": {
        "complete": { "type": "boolean", "description": "True only when absent is empty." },
        "absent": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["field", "reason"],
            "properties": {
              "field": { "type": "string", "description": "Dotted path of the context field, e.g. capture.aircraft" },
              "reason": { "$ref": "#/$defs/absence_reason" }
            }
          }
        }
      }
    }
  },

  "$defs": {
    "identifier": {
      "type": ["string", "integer", "null"],
      "description": "Opaque within the issuer's namespace. Do not compare identifiers across issuers."
    },
    "classification": {
      "enum": ["observed", "calculated", "estimated", "inferred", "unknown"],
      "description": "How the claim was arrived at. observed: measured directly. calculated: derived deterministically from measurements. estimated: derived with modelling assumptions. inferred: concluded from indirect evidence. unknown: the issuer cannot say."
    },
    "absence_reason": {
      "enum": ["not_recorded", "extraction_failed", "unsupported"],
      "description": "not_recorded: the issuer never held this fact. extraction_failed: an extractor errored on this input. unsupported: no extractor exists for this input type."
    }
  }
}
