{
  "$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" }
        }
      }
    },
    "intake_logs": {
      "type": "object",
      "required": ["items"],
      "additionalProperties": false,
      "properties": {
        "items": {
          "type": "array",
          "items": { "$ref": "#/definitions/IntakeLog" }
        }
      }
    }
  },
  "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" }
        }
      }
    },
    "NutrientEntry": {
      "type": "object",
      "required": ["nutrient", "amount", "unit"],
      "additionalProperties": false,
      "properties": {
        "nutrient": { "type": "string" },
        "amount": { "type": "number" },
        "unit": { "type": "string" }
      }
    },
    "Supplement": {
      "type": "object",
      "required": ["id", "name", "nutrients", "createdAt"],
      "additionalProperties": false,
      "properties": {
        "id": { "type": "string" },
        "name": { "type": "string" },
        "brand": { "type": "string" },
        "barcode": { "type": "string" },
        "imageUrl": { "type": "string" },
        "nutrients": {
          "type": "array",
          "items": { "$ref": "#/definitions/NutrientEntry" }
        },
        "servingSize": { "type": "string" },
        "servingSizeUnit": { "type": "string" },
        "containerSize": { "type": "string" },
        "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", "source", "createdAt"],
      "additionalProperties": false,
      "properties": {
        "id": { "type": "string" },
        "supplementId": { "type": "string" },
        "stackId": { "type": "string" },
        "dose": { "type": "number" },
        "unit": { "type": "string" },
        "stackOrder": { "type": "number" },
        "source": { "type": "string", "enum": ["off", "deduced", "manual"] },
        "createdAt": { "type": "string", "format": "date-time" }
      }
    },
    "IntakeLog": {
      "type": "object",
      "required": ["supplementId", "stackId", "date", "time", "status", "loggedAt"],
      "additionalProperties": false,
      "properties": {
        "supplementId": { "type": "string" },
        "stackId": { "type": "string" },
        "date": { "type": "string", "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}$" },
        "time": { "type": "string", "pattern": "^[0-2][0-9]:[0-5][0-9]$" },
        "status": { "type": "string", "enum": ["taken", "missed"] },
        "loggedAt": { "type": "string", "format": "date-time" }
      }
    }
  }
}
