{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://biostacks.app/schema.json",
  "title": "BiostacksBackup",
  "description": "Biostacks import/export format (mirrors internal store shape).",
  "version": "1.0.0",
  "type": "object",
  "required": ["supplements", "stacks"],
  "additionalProperties": false,
  "properties": {
    "supplements": {
      "type": "object",
      "required": ["items"],
      "additionalProperties": false,
      "properties": {
        "items": {
          "type": "array",
          "items": { "$ref": "#/definitions/Supplement" }
        }
      }
    },
    "stacks": {
      "type": "object",
      "required": ["entries", "items"],
      "additionalProperties": false,
      "properties": {
        "entries": {
          "type": "array",
          "items": { "$ref": "#/definitions/StackItem" }
        },
        "items": {
          "type": "array",
          "items": { "$ref": "#/definitions/Stack" }
        }
      }
    }
  },
  "definitions": {
    "DayOfWeek": {
      "type": "string",
      "enum": ["mon", "tue", "wed", "thu", "fri", "sat", "sun"]
    },
    "Schedule": {
      "type": "object",
      "required": ["time", "frequency"],
      "additionalProperties": false,
      "properties": {
        "time": { "type": "string", "pattern": "^[0-2][0-9]:[0-5][0-9]$" },
        "frequency": { "type": "string", "enum": ["daily", "specific"] },
        "weekdays": {
          "type": "array",
          "items": { "$ref": "#/definitions/DayOfWeek" }
        }
      }
    },
    "NutrientUnit": {
      "type": "string",
      "enum": ["mg", "mcg", "g", "IU", "ml", "fl oz", "tsp", "tbsp", "B CFU", "mcg DFE", "mg DFE", "mg AT", "mg RAE", "mg NE", "ng"]
    },
    "ServingUnit": {
      "type": "string",
      "enum": ["mg", "mcg", "g", "IU", "ml", "fl oz", "tsp", "tbsp", "B CFU", "mcg DFE", "mg DFE", "mg AT", "mg RAE", "mg NE", "ng", "capsule", "tablet", "softgel", "scoop", "drop", "dropperful", "gummy", "disc", "packet", "serving"]
    },
    "NutrientEntry": {
      "type": "object",
      "required": ["nutrient", "amount", "unit"],
      "additionalProperties": false,
      "properties": {
        "nutrient": { "type": "string" },
        "amount": { "type": "number" },
        "unit": { "$ref": "#/definitions/NutrientUnit" }
      }
    },
    "Supplement": {
      "type": "object",
      "required": ["id", "name", "nutrients", "createdAt"],
      "additionalProperties": false,
      "properties": {
        "id": { "type": "string" },
        "name": { "type": "string" },
        "brand": { "type": "string" },
        "barcode": { "type": "string" },
        "source": { "type": "string", "enum": ["nih", "off", "manual"] },
        "imageUrl": { "type": "string" },
        "nutrients": {
          "type": "array",
          "items": { "$ref": "#/definitions/NutrientEntry" }
        },
        "servingSize": { "type": "string" },
        "servingSizeUnit": { "$ref": "#/definitions/ServingUnit" },
        "containerSize": { "type": "string" },
        "containerSizeUnit": { "$ref": "#/definitions/ServingUnit" },
        "calories": { "type": "number" },
        "protein": { "type": "number" },
        "fat": { "type": "number" },
        "carbs": { "type": "number" },
        "fiber": { "type": "number" },
        "sugar": { "type": "number" },
        "sodium": { "type": "number" },
        "createdAt": { "type": "string", "format": "date-time" }
      }
    },
    "Stack": {
      "type": "object",
      "required": ["id", "schedule", "enabled"],
      "additionalProperties": false,
      "properties": {
        "id": { "type": "string" },
        "schedule": { "$ref": "#/definitions/Schedule" },
        "label": { "type": "string" },
        "enabled": { "type": "boolean" }
      }
    },
    "StackItem": {
      "type": "object",
      "required": ["id", "supplementId", "stackId", "dose", "unit", "createdAt"],
      "additionalProperties": false,
      "properties": {
        "id": { "type": "string" },
        "supplementId": { "type": "string" },
        "stackId": { "type": "string" },
        "dose": { "type": "number" },
        "unit": { "$ref": "#/definitions/ServingUnit" },
        "stackOrder": { "type": "number" },
        "createdAt": { "type": "string", "format": "date-time" }
      }
    }
  }
}
