{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://workouts.parat.io/schema/workout/v1.json",
  "title": "parat.io workouts workout v1",
  "description": "Structured indoor cycling workout. Power targets are fractions of FTP, never absolute watts. Cadence targets are rpm. Heart-rate targets are zones 1-5, never bpm.",
  "type": "object",
  "required": [
    "name",
    "steps"
  ],
  "additionalProperties": false,
  "properties": {
    "$schema": {
      "enum": [
        "https://workouts.parat.io/schema/workout/v1.json",
        "https://ride.parat.io/schema/workout/v1.json",
        "https://training.parat.io/schema/workout/v1.json"
      ]
    },
    "id": {
      "type": "string",
      "format": "uuid"
    },
    "name": {
      "type": "string",
      "minLength": 1,
      "maxLength": 80
    },
    "description": {
      "type": "string",
      "maxLength": 2000
    },
    "tags": {
      "type": "array",
      "items": {
        "type": "string",
        "minLength": 1,
        "maxLength": 32
      },
      "uniqueItems": true
    },
    "author": {
      "type": "string",
      "maxLength": 80
    },
    "createdAt": {
      "type": "string",
      "format": "date-time"
    },
    "steps": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/step"
      }
    }
  },
  "$defs": {
    "duration": {
      "description": "Seconds",
      "type": "integer",
      "minimum": 1,
      "maximum": 86400
    },
    "ftpFraction": {
      "description": "Fraction of FTP, e.g. 0.68 = 68 % FTP",
      "type": "number",
      "minimum": 0,
      "maximum": 3
    },
    "powerRamp": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "start",
        "end"
      ],
      "properties": {
        "start": {
          "$ref": "#/$defs/ftpFraction"
        },
        "end": {
          "$ref": "#/$defs/ftpFraction"
        }
      }
    },
    "cadenceTarget": {
      "description": "rpm, constant or range",
      "oneOf": [
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "value"
          ],
          "properties": {
            "value": {
              "$ref": "#/$defs/rpm"
            }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "min",
            "max"
          ],
          "properties": {
            "min": {
              "$ref": "#/$defs/rpm"
            },
            "max": {
              "$ref": "#/$defs/rpm"
            }
          }
        }
      ]
    },
    "rpm": {
      "type": "integer",
      "minimum": 30,
      "maximum": 200
    },
    "heartRateTarget": {
      "description": "Heart-rate zone 1-5, resolved at ride time against the rider's heart-rate zones (never bpm). With LTHR (Friel) zones, zone 5 covers 5a-5c.",
      "type": "object",
      "additionalProperties": false,
      "required": [
        "zone"
      ],
      "properties": {
        "zone": {
          "type": "integer",
          "minimum": 1,
          "maximum": 5
        }
      }
    },
    "step": {
      "oneOf": [
        {
          "$ref": "#/$defs/steadyStep"
        },
        {
          "$ref": "#/$defs/rampStep"
        },
        {
          "$ref": "#/$defs/freeStep"
        },
        {
          "$ref": "#/$defs/repeatStep"
        }
      ]
    },
    "steadyStep": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "duration",
        "power"
      ],
      "properties": {
        "type": {
          "const": "steady"
        },
        "name": {
          "type": "string",
          "maxLength": 40
        },
        "duration": {
          "$ref": "#/$defs/duration"
        },
        "power": {
          "$ref": "#/$defs/steadyPower"
        },
        "cadence": {
          "$ref": "#/$defs/cadenceTarget"
        },
        "heartRate": {
          "$ref": "#/$defs/heartRateTarget"
        }
      }
    },
    "rampStep": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "duration",
        "power"
      ],
      "properties": {
        "type": {
          "const": "ramp"
        },
        "name": {
          "type": "string",
          "maxLength": 40
        },
        "duration": {
          "$ref": "#/$defs/duration"
        },
        "power": {
          "$ref": "#/$defs/powerRamp"
        },
        "cadence": {
          "$ref": "#/$defs/cadenceTarget"
        },
        "heartRate": {
          "$ref": "#/$defs/heartRateTarget"
        }
      }
    },
    "freeStep": {
      "description": "No ERG target; trainer in resistance/level mode",
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "duration"
      ],
      "properties": {
        "type": {
          "const": "free"
        },
        "name": {
          "type": "string",
          "maxLength": 40
        },
        "duration": {
          "$ref": "#/$defs/duration"
        },
        "cadence": {
          "$ref": "#/$defs/cadenceTarget"
        },
        "heartRate": {
          "$ref": "#/$defs/heartRateTarget"
        }
      }
    },
    "repeatStep": {
      "description": "Repeats its child steps count times. One level of nesting only.",
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "count",
        "steps"
      ],
      "properties": {
        "type": {
          "const": "repeat"
        },
        "name": {
          "type": "string",
          "maxLength": 40
        },
        "count": {
          "type": "integer",
          "minimum": 2,
          "maximum": 100
        },
        "steps": {
          "type": "array",
          "minItems": 1,
          "items": {
            "oneOf": [
              {
                "$ref": "#/$defs/steadyStep"
              },
              {
                "$ref": "#/$defs/rampStep"
              },
              {
                "$ref": "#/$defs/freeStep"
              }
            ]
          }
        }
      }
    },
    "steadyPower": {
      "description": "Steady power target: exactly one of constant value, range, or zone",
      "oneOf": [
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "value"
          ],
          "properties": {
            "value": {
              "$ref": "#/$defs/ftpFraction"
            }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "min",
            "max"
          ],
          "properties": {
            "min": {
              "$ref": "#/$defs/ftpFraction"
            },
            "max": {
              "$ref": "#/$defs/ftpFraction"
            }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "zone"
          ],
          "properties": {
            "zone": {
              "type": "integer",
              "minimum": 1,
              "maximum": 7
            }
          }
        }
      ]
    }
  }
}
