{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://github.com/Fujio-Turner/mobile_field_service/docs/schema/common.json",
  "title": "MFS shared envelope",
  "type": "object",
  "required": ["type", "audit"],
  "additionalProperties": true,
  "properties": {
    "type": { "type": "string", "minLength": 1 },
    "audit": { "$ref": "#/$defs/audit" },
    "history": {
      "type": "array",
      "maxItems": 100,
      "items": { "$ref": "#/$defs/historyEntry" }
    },
    "historyTruncated": { "type": "boolean" }
  },
  "$defs": {
    "unixSeconds": {
      "type": "integer",
      "minimum": 0,
      "description": "Unix time in seconds, not milliseconds"
    },
    "auditStamp": {
      "type": "object",
      "additionalProperties": false,
      "required": ["dt", "ver", "by"],
      "properties": {
        "dt": { "$ref": "#/$defs/unixSeconds" },
        "ver": { "type": "string" },
        "by": { "type": "string", "description": "username (audit.by)" }
      }
    },
    "audit": {
      "type": "object",
      "additionalProperties": false,
      "required": ["cr", "up"],
      "properties": {
        "cr": { "$ref": "#/$defs/auditStamp" },
        "up": { "$ref": "#/$defs/auditStamp" }
      }
    },
    "historyChange": {
      "type": "object",
      "additionalProperties": false,
      "required": ["path"],
      "properties": {
        "path": { "type": "string", "minLength": 1 },
        "from": true,
        "to": true
      }
    },
    "historyEntry": {
      "type": "object",
      "additionalProperties": false,
      "required": ["dt", "by", "ver", "op"],
      "properties": {
        "dt": { "$ref": "#/$defs/unixSeconds" },
        "lat": { "type": "number", "minimum": -90, "maximum": 90 },
        "lon": { "type": "number", "minimum": -180, "maximum": 180 },
        "accuracyM": { "type": "number", "minimum": 0 },
        "by": { "type": "string" },
        "ver": { "type": "string" },
        "op": { "type": "string", "minLength": 1 },
        "changes": {
          "type": "array",
          "items": { "$ref": "#/$defs/historyChange" }
        }
      }
    },
    "assignedTo": {
      "type": "object",
      "additionalProperties": false,
      "required": ["employeeId"],
      "properties": {
        "userId": { "type": "string" },
        "employeeId": { "type": "string", "minLength": 1 },
        "email": { "type": "string", "format": "email" },
        "username": { "type": "string" },
        "displayName": { "type": "string" }
      }
    },
    "geo": {
      "type": "object",
      "additionalProperties": false,
      "required": ["lat", "lon"],
      "properties": {
        "lat": { "type": "number", "minimum": -90, "maximum": 90 },
        "lon": { "type": "number", "minimum": -180, "maximum": 180 },
        "accuracyM": { "type": "number", "minimum": 0 }
      }
    },
    "address": {
      "type": "object",
      "additionalProperties": true,
      "properties": {
        "line1": { "type": "string" },
        "city": { "type": "string" },
        "region": { "type": "string" },
        "postal": { "type": "string" },
        "country": { "type": "string" }
      }
    },
    "site": {
      "type": "object",
      "additionalProperties": true,
      "required": ["name"],
      "properties": {
        "name": { "type": "string" },
        "address": { "$ref": "#/$defs/address" },
        "geo": { "$ref": "#/$defs/geo" }
      }
    },
    "scheduled": {
      "type": "object",
      "additionalProperties": true,
      "required": ["startDt"],
      "properties": {
        "startDt": { "$ref": "#/$defs/unixSeconds" },
        "endDt": { "$ref": "#/$defs/unixSeconds" },
        "day": { "type": "string", "pattern": "^\\d{4}-\\d{2}-\\d{2}$" }
      }
    },
    "cents": {
      "type": "integer",
      "description": "Currency minor units (integer cents). Never a binary float."
    }
  }
}
