{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://getlwyl.app/routines/lwyl-routines.schema.json",
  "title": "LWYL routines file",
  "description": "Routines (workout plans) for the LWYL app, optionally as a plan spread over the days you train. Save as .lwyl or .json.",
  "type": "object",
  "required": [
    "templates"
  ],
  "properties": {
    "version": {
      "type": "integer",
      "const": 1
    },
    "unit": {
      "enum": [
        "kg",
        "lb"
      ],
      "default": "kg",
      "description": "Unit for every weight in the file."
    },
    "plan": {
      "$ref": "#/$defs/plan"
    },
    "templates": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/routine"
      }
    }
  },
  "$defs": {
    "plan": {
      "type": "object",
      "required": [
        "workouts"
      ],
      "description": "Makes the routines a plan. No dates: LWYL asks for a start day and the days the user trains, and spreads the workouts over them in order.",
      "properties": {
        "name": {
          "type": "string",
          "description": "Plan name; the user can change it on import."
        },
        "days": {
          "type": "array",
          "description": "Days of the week the plan is made for; preselected on import.",
          "items": {
            "oneOf": [
              {
                "enum": [
                  "Mon",
                  "Tue",
                  "Wed",
                  "Thu",
                  "Fri",
                  "Sat",
                  "Sun"
                ]
              },
              {
                "type": "integer",
                "minimum": 1,
                "maximum": 7,
                "description": "1 = Monday … 7 = Sunday"
              }
            ]
          }
        },
        "workouts": {
          "type": "array",
          "minItems": 1,
          "description": "Every workout of the plan in order, by routine name (one per training day).",
          "items": {
            "type": "string"
          }
        }
      }
    },
    "routine": {
      "type": "object",
      "required": [
        "name",
        "exercises"
      ],
      "properties": {
        "name": {
          "type": "string",
          "description": "Routine name. If the user already has one with this name, LWYL adds it as 'Name (2)'; it never overwrites."
        },
        "notes": {
          "type": "string"
        },
        "schedule": {
          "type": "array",
          "description": "Fixed days for this routine (optional; ignored when the file has a plan). Local dates, no time zone. Past dates are skipped.",
          "items": {
            "$ref": "#/$defs/scheduleEntry"
          }
        },
        "exercises": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/exercise"
          }
        }
      }
    },
    "scheduleEntry": {
      "oneOf": [
        {
          "type": "string",
          "pattern": "^\\d{4}-\\d{2}-\\d{2}$",
          "description": "YYYY-MM-DD (local date). Plans are day only."
        },
        {
          "type": "object",
          "required": [
            "date"
          ],
          "properties": {
            "date": {
              "type": "string",
              "pattern": "^\\d{4}-\\d{2}-\\d{2}$"
            },
            "note": {
              "type": "string",
              "description": "Short note shown on the scheduled card, e.g. 'Deload'."
            }
          }
        }
      ]
    },
    "exercise": {
      "type": "object",
      "required": [
        "name",
        "sets"
      ],
      "properties": {
        "name": {
          "type": "string",
          "description": "Exercise name; matched against LWYL's catalog (e.g. 'Barbell Bench Press')."
        },
        "muscleGroup": {
          "enum": [
            "Chest",
            "Back",
            "Legs",
            "Shoulders",
            "Arms",
            "Core",
            "Other"
          ],
          "description": "Required for exercises not in LWYL's catalog."
        },
        "sets": {
          "oneOf": [
            {
              "type": "array",
              "items": {
                "$ref": "#/$defs/set"
              }
            },
            {
              "type": "object",
              "required": [
                "count"
              ],
              "properties": {
                "count": {
                  "type": "integer",
                  "minimum": 1
                },
                "reps": {
                  "type": "integer"
                },
                "weight": {
                  "type": "number"
                },
                "seconds": {
                  "type": "integer"
                },
                "km": {
                  "type": "number"
                },
                "restSeconds": {
                  "type": "integer"
                }
              }
            }
          ]
        }
      }
    },
    "set": {
      "type": "object",
      "properties": {
        "reps": {
          "type": "integer"
        },
        "weight": {
          "type": "number"
        },
        "seconds": {
          "type": "integer"
        },
        "km": {
          "type": "number"
        },
        "restSeconds": {
          "type": "integer"
        }
      }
    }
  }
}
