{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://canpuff.org/schemas/v1/supply.json",
  "title": "CanPUFF supply v1",
  "type": "object",
  "properties": {
    "type": {
      "const": "supply"
    },
    "version": {
      "const": 1
    },
    "id": {
      "$ref": "#/$defs/uuid"
    },
    "created": {
      "$ref": "#/$defs/timestamp"
    },
    "updated": {
      "$ref": "#/$defs/timestamp"
    },
    "ext": {
      "$ref": "#/$defs/ext"
    },
    "name": {
      "type": "string",
      "minLength": 1
    },
    "shortName": {
      "type": "string"
    },
    "variety": {
      "enum": [
        "indica",
        "sativa",
        "hybrid"
      ]
    },
    "thc": {
      "$ref": "#/$defs/percent"
    },
    "cbd": {
      "$ref": "#/$defs/percent"
    },
    "originalThc": {
      "$ref": "#/$defs/percent"
    },
    "originalCbd": {
      "$ref": "#/$defs/percent"
    },
    "terpenes": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "name": {
            "type": "string",
            "minLength": 1
          },
          "percentage": {
            "$ref": "#/$defs/percent"
          }
        },
        "required": [
          "name",
          "percentage"
        ]
      }
    },
    "totalTerpenes": {
      "$ref": "#/$defs/percent"
    },
    "grams": {
      "$ref": "#/$defs/grams"
    },
    "gramsStart": {
      "$ref": "#/$defs/grams"
    },
    "cost": {
      "$ref": "#/$defs/money"
    },
    "currency": {
      "$ref": "#/$defs/currency"
    },
    "taxes": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/uuid"
      }
    },
    "purchased": {
      "$ref": "#/$defs/dateish"
    },
    "packaged": {
      "$ref": "#/$defs/dateish"
    },
    "expiry": {
      "$ref": "#/$defs/dateish"
    },
    "shop": {
      "$ref": "#/$defs/uuid"
    },
    "brand": {
      "$ref": "#/$defs/uuid"
    },
    "rating": {
      "$ref": "#/$defs/rating5"
    },
    "photo": {
      "$ref": "#/$defs/hashref"
    },
    "lowThreshold": {
      "$ref": "#/$defs/grams"
    },
    "finished": {
      "type": "boolean",
      "default": false
    },
    "finishedAt": {
      "$ref": "#/$defs/timestamp"
    },
    "favorite": {
      "type": "boolean",
      "default": false
    },
    "sharedSupply": {
      "type": "boolean",
      "default": false
    },
    "notes": {
      "type": "string"
    }
  },
  "required": [
    "type",
    "version",
    "id",
    "name"
  ],
  "$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"
      }
    }
  },
  "dependentRequired": {
    "cost": [
      "currency"
    ]
  }
}
