{
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "format": {
      "const": "workout-studio"
    },
    "version": {
      "const": 1
    },
    "workouts": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/workout"
      },
      "minItems": 1,
      "maxItems": 100
    }
  },
  "required": [
    "format",
    "version",
    "workouts"
  ],
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://workoutstudio.phoskee.dev/prompt/workout.schema.json",
  "title": "Workout Studio import format v1",
  "description": "Public interchange format, not the private project archive. Times in seconds, distances in metres, energy in kcal. Apple compatibility is checked by the app. Maximum input size 2 MB.",
  "$defs": {
    "workout": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "title": {
          "type": "string",
          "minLength": 1
        },
        "activity": {
          "type": "string",
          "minLength": 1
        },
        "location": {
          "enum": [
            "indoor",
            "outdoor",
            "unknown"
          ]
        },
        "family": {
          "enum": [
            "strength",
            "custom",
            "goal",
            "pacer",
            "multisport"
          ]
        },
        "exercises": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/exercise"
          },
          "minItems": 1,
          "maxItems": 1000
        },
        "blocks": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/block"
          },
          "minItems": 1,
          "maxItems": 1000
        },
        "warmup": {
          "$ref": "#/$defs/step"
        },
        "cooldown": {
          "$ref": "#/$defs/step"
        },
        "goal": {
          "$ref": "#/$defs/goal"
        },
        "pacerMeters": {
          "type": "number",
          "exclusiveMinimum": 0
        },
        "pacerSeconds": {
          "type": "number",
          "exclusiveMinimum": 0
        },
        "legs": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/leg"
          },
          "minItems": 1,
          "maxItems": 1000
        },
        "notes": {
          "type": "string"
        }
      },
      "required": [
        "title",
        "activity",
        "location",
        "family"
      ],
      "allOf": [
        {
          "if": {
            "properties": {
              "family": {
                "const": "strength"
              }
            }
          },
          "then": {
            "required": [
              "exercises"
            ],
            "not": {
              "anyOf": [
                {
                  "required": [
                    "blocks"
                  ]
                },
                {
                  "required": [
                    "cooldown"
                  ]
                },
                {
                  "required": [
                    "goal"
                  ]
                },
                {
                  "required": [
                    "legs"
                  ]
                },
                {
                  "required": [
                    "pacerMeters"
                  ]
                },
                {
                  "required": [
                    "pacerSeconds"
                  ]
                },
                {
                  "required": [
                    "warmup"
                  ]
                }
              ]
            },
            "properties": {
              "activity": {
                "enum": [
                  "traditionalStrengthTraining",
                  "functionalStrengthTraining"
                ]
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "family": {
                "const": "custom"
              }
            }
          },
          "then": {
            "required": [
              "blocks"
            ],
            "not": {
              "anyOf": [
                {
                  "required": [
                    "exercises"
                  ]
                },
                {
                  "required": [
                    "goal"
                  ]
                },
                {
                  "required": [
                    "legs"
                  ]
                },
                {
                  "required": [
                    "pacerMeters"
                  ]
                },
                {
                  "required": [
                    "pacerSeconds"
                  ]
                }
              ]
            }
          }
        },
        {
          "if": {
            "properties": {
              "family": {
                "const": "goal"
              }
            }
          },
          "then": {
            "required": [
              "goal"
            ],
            "not": {
              "anyOf": [
                {
                  "required": [
                    "blocks"
                  ]
                },
                {
                  "required": [
                    "cooldown"
                  ]
                },
                {
                  "required": [
                    "exercises"
                  ]
                },
                {
                  "required": [
                    "legs"
                  ]
                },
                {
                  "required": [
                    "pacerMeters"
                  ]
                },
                {
                  "required": [
                    "pacerSeconds"
                  ]
                },
                {
                  "required": [
                    "warmup"
                  ]
                }
              ]
            }
          }
        },
        {
          "if": {
            "properties": {
              "family": {
                "const": "pacer"
              }
            }
          },
          "then": {
            "required": [
              "pacerMeters",
              "pacerSeconds"
            ],
            "not": {
              "anyOf": [
                {
                  "required": [
                    "blocks"
                  ]
                },
                {
                  "required": [
                    "cooldown"
                  ]
                },
                {
                  "required": [
                    "exercises"
                  ]
                },
                {
                  "required": [
                    "goal"
                  ]
                },
                {
                  "required": [
                    "legs"
                  ]
                },
                {
                  "required": [
                    "warmup"
                  ]
                }
              ]
            }
          }
        },
        {
          "if": {
            "properties": {
              "family": {
                "const": "multisport"
              }
            }
          },
          "then": {
            "required": [
              "legs"
            ],
            "not": {
              "anyOf": [
                {
                  "required": [
                    "blocks"
                  ]
                },
                {
                  "required": [
                    "cooldown"
                  ]
                },
                {
                  "required": [
                    "exercises"
                  ]
                },
                {
                  "required": [
                    "goal"
                  ]
                },
                {
                  "required": [
                    "pacerMeters"
                  ]
                },
                {
                  "required": [
                    "pacerSeconds"
                  ]
                },
                {
                  "required": [
                    "warmup"
                  ]
                }
              ]
            }
          }
        }
      ]
    },
    "exercise": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "name": {
          "type": "string",
          "minLength": 1
        },
        "sets": {
          "type": "integer",
          "minimum": 1,
          "maximum": 50
        },
        "reps": {
          "type": "string",
          "minLength": 1,
          "description": "Positive reps or increasing range, optionally /lato, /gamba, /side, /leg; explicit max/amrap/to failure allowed."
        },
        "durationSeconds": {
          "type": "number",
          "exclusiveMinimum": 0,
          "maximum": 86400
        },
        "restSeconds": {
          "type": "number",
          "minimum": 0,
          "maximum": 86400
        }
      },
      "required": [
        "name",
        "sets",
        "restSeconds"
      ],
      "oneOf": [
        {
          "required": [
            "reps"
          ],
          "not": {
            "required": [
              "durationSeconds"
            ]
          }
        },
        {
          "required": [
            "durationSeconds"
          ],
          "not": {
            "required": [
              "reps"
            ]
          }
        }
      ]
    },
    "block": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "iterations": {
          "type": "integer",
          "minimum": 1,
          "maximum": 1000
        },
        "steps": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/step"
          },
          "minItems": 1,
          "maxItems": 1000
        }
      },
      "required": [
        "iterations",
        "steps"
      ]
    },
    "step": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "name": {
          "type": "string",
          "minLength": 1
        },
        "recovery": {
          "type": "boolean"
        },
        "goal": {
          "$ref": "#/$defs/goal"
        },
        "reps": {
          "type": "string",
          "minLength": 1
        },
        "alert": {
          "$ref": "#/$defs/alert"
        }
      },
      "required": [
        "name",
        "recovery",
        "goal"
      ],
      "allOf": [
        {
          "if": {
            "required": [
              "reps"
            ]
          },
          "then": {
            "properties": {
              "goal": {
                "properties": {
                  "kind": {
                    "const": "open"
                  }
                }
              }
            }
          }
        }
      ]
    },
    "goal": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "kind": {
          "enum": [
            "open",
            "time",
            "distance",
            "energy",
            "swimDistanceTime"
          ]
        },
        "value": {
          "type": "number",
          "exclusiveMinimum": 0
        },
        "seconds": {
          "type": "number",
          "exclusiveMinimum": 0
        }
      },
      "required": [
        "kind"
      ],
      "oneOf": [
        {
          "properties": {
            "kind": {
              "const": "open"
            }
          },
          "not": {
            "anyOf": [
              {
                "required": [
                  "value"
                ]
              },
              {
                "required": [
                  "seconds"
                ]
              }
            ]
          }
        },
        {
          "properties": {
            "kind": {
              "enum": [
                "time",
                "distance",
                "energy"
              ]
            }
          },
          "required": [
            "value"
          ],
          "not": {
            "required": [
              "seconds"
            ]
          }
        },
        {
          "properties": {
            "kind": {
              "const": "swimDistanceTime"
            }
          },
          "required": [
            "value",
            "seconds"
          ]
        }
      ]
    },
    "alert": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "kind": {
          "enum": [
            "none",
            "heartRange",
            "heartZone",
            "cadence",
            "cadenceRange",
            "power",
            "powerRange",
            "powerZone",
            "speed",
            "speedRange"
          ]
        },
        "low": {
          "type": "number"
        },
        "high": {
          "type": "number"
        },
        "average": {
          "type": "boolean"
        }
      },
      "required": [
        "kind",
        "low"
      ],
      "allOf": [
        {
          "if": {
            "properties": {
              "kind": {
                "enum": [
                  "heartRange",
                  "cadenceRange",
                  "powerRange",
                  "speedRange"
                ]
              }
            }
          },
          "then": {
            "required": [
              "high"
            ]
          },
          "else": {
            "not": {
              "required": [
                "high"
              ]
            }
          }
        },
        {
          "if": {
            "properties": {
              "average": {
                "const": true
              }
            },
            "required": [
              "average"
            ]
          },
          "then": {
            "properties": {
              "kind": {
                "enum": [
                  "power",
                  "powerRange",
                  "speed",
                  "speedRange"
                ]
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "kind": {
                "const": "none"
              }
            }
          },
          "then": {
            "properties": {
              "low": {
                "const": 0
              }
            }
          }
        }
      ]
    },
    "leg": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "activity": {
          "enum": [
            "swimming",
            "cycling",
            "running"
          ]
        },
        "location": {
          "enum": [
            "indoor",
            "outdoor",
            "unknown"
          ]
        },
        "swimmingLocation": {
          "enum": [
            "unknown",
            "pool",
            "openWater"
          ]
        }
      },
      "required": [
        "activity",
        "location"
      ],
      "allOf": [
        {
          "if": {
            "properties": {
              "activity": {
                "enum": [
                  "cycling",
                  "running"
                ]
              }
            }
          },
          "then": {
            "not": {
              "required": [
                "swimmingLocation"
              ]
            }
          }
        }
      ]
    }
  }
}
