{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://canpuff.org/schemas/v1/consumption.json",
  "title": "CanPUFF consumption v1",
  "type": "object",
  "properties": {
    "type": {
      "const": "consumption"
    },
    "version": {
      "const": 1
    },
    "id": {
      "$ref": "#/$defs/uuid"
    },
    "created": {
      "$ref": "#/$defs/timestamp"
    },
    "updated": {
      "$ref": "#/$defs/timestamp"
    },
    "ext": {
      "$ref": "#/$defs/ext"
    },
    "at": {
      "$ref": "#/$defs/timestamp"
    },
    "supply": {
      "$ref": "#/$defs/uuid"
    },
    "method": {
      "$ref": "#/$defs/uuid"
    },
    "amount": {
      "type": "object",
      "properties": {
        "grams": {
          "$ref": "#/$defs/grams"
        }
      },
      "required": [
        "grams"
      ]
    },
    "shared": {
      "type": "object",
      "properties": {
        "people": {
          "type": "integer",
          "minimum": 0,
          "default": 0
        },
        "gift": {
          "type": "boolean",
          "default": false
        }
      }
    },
    "effects": {
      "type": "object",
      "properties": {
        "rating": {
          "type": "integer",
          "minimum": 1,
          "maximum": 10
        },
        "sideEffects": {
          "type": "string"
        }
      }
    },
    "context": {
      "type": "object",
      "properties": {
        "purpose": {
          "type": "string"
        },
        "location": {
          "type": "object",
          "properties": {
            "name": {
              "type": "string"
            },
            "lat": {
              "type": "number",
              "minimum": -90,
              "maximum": 90
            },
            "lon": {
              "type": "number",
              "minimum": -180,
              "maximum": 180
            }
          }
        }
      }
    },
    "photo": {
      "$ref": "#/$defs/hashref"
    },
    "notes": {
      "type": "string"
    }
  },
  "required": [
    "type",
    "version",
    "id",
    "at"
  ],
  "$defs": {
    "uuid": {
      "type": "string",
      "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"
    },
    "timestamp": {
      "type": "string",
      "format": "date-time",
      "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d+)?(Z|[+-]\\d{2}:\\d{2})$",
      "description": "RFC 3339 with UTC offset (Z acceptable)"
    },
    "dateish": {
      "type": "string",
      "pattern": "^\\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\\d|3[01])(T([01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d(\\.\\d+)?(Z|[+-]([01]\\d|2[0-3]):[0-5]\\d))?$",
      "description": "YYYY-MM-DD or RFC 3339 timestamp"
    },
    "hashref": {
      "type": "string",
      "pattern": "^sha256:[0-9a-f]{64}$"
    },
    "grams": {
      "type": "number",
      "minimum": 0,
      "description": "grams, max 4 decimal places"
    },
    "percent": {
      "type": "number",
      "minimum": 0,
      "maximum": 100,
      "description": "percent by weight, max 2 decimal places"
    },
    "money": {
      "type": "number",
      "minimum": 0,
      "description": "max 2 decimal places"
    },
    "currency": {
      "type": "string",
      "pattern": "^[A-Z]{3}$"
    },
    "rating5": {
      "type": "integer",
      "minimum": 0,
      "maximum": 5,
      "description": "0 = unrated"
    },
    "ext": {
      "type": "object",
      "propertyNames": {
        "pattern": "^[a-z0-9-]+$"
      },
      "additionalProperties": {
        "type": "object"
      }
    }
  }
}
