{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "oneOf": [
    {
      "type": "object",
      "properties": {
        "schemaVersion": {
          "type": "string",
          "const": "root-plan/v1"
        },
        "status": {
          "type": "string",
          "const": "ready"
        },
        "plan": {
          "type": "object",
          "properties": {
            "title": {
              "type": "string",
              "minLength": 1,
              "maxLength": 160
            },
            "goal": {
              "type": "string",
              "enum": [
                "running",
                "strength",
                "general_fitness"
              ]
            },
            "startDate": {
              "type": "string",
              "pattern": "^\\d{4}-\\d{2}-\\d{2}$"
            },
            "endDate": {
              "type": "string",
              "pattern": "^\\d{4}-\\d{2}-\\d{2}$"
            },
            "timeZone": {
              "type": "string",
              "maxLength": 80
            },
            "days": {
              "minItems": 1,
              "maxItems": 90,
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "date": {
                    "type": "string",
                    "pattern": "^\\d{4}-\\d{2}-\\d{2}$"
                  },
                  "label": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 120
                  },
                  "rest": {
                    "type": "boolean"
                  },
                  "sessions": {
                    "maxItems": 3,
                    "type": "array",
                    "items": {
                      "type": "object",
                      "properties": {
                        "key": {
                          "type": "string",
                          "pattern": "^[a-zA-Z0-9_-]{1,40}$"
                        },
                        "type": {
                          "type": "string",
                          "enum": [
                            "run",
                            "walk",
                            "strength",
                            "mobility",
                            "cross_training"
                          ]
                        },
                        "title": {
                          "type": "string",
                          "minLength": 1,
                          "maxLength": 120
                        },
                        "durationMinutes": {
                          "type": "integer",
                          "minimum": 1,
                          "maximum": 180
                        },
                        "instructions": {
                          "type": "string",
                          "minLength": 1,
                          "maxLength": 1200
                        },
                        "effortRpe": {
                          "type": "number",
                          "minimum": 1,
                          "maximum": 10
                        },
                        "distanceKm": {
                          "anyOf": [
                            {
                              "type": "number",
                              "exclusiveMinimum": 0,
                              "maximum": 100
                            },
                            {
                              "type": "null"
                            }
                          ]
                        },
                        "intervals": {
                          "maxItems": 10,
                          "type": "array",
                          "items": {
                            "type": "object",
                            "properties": {
                              "repetitions": {
                                "type": "integer",
                                "minimum": 1,
                                "maximum": 30
                              },
                              "workSeconds": {
                                "type": "integer",
                                "minimum": 1,
                                "maximum": 1800
                              },
                              "recoverySeconds": {
                                "type": "integer",
                                "minimum": 0,
                                "maximum": 1800
                              }
                            },
                            "required": [
                              "repetitions",
                              "workSeconds",
                              "recoverySeconds"
                            ],
                            "additionalProperties": false
                          }
                        },
                        "exercises": {
                          "maxItems": 15,
                          "type": "array",
                          "items": {
                            "type": "object",
                            "properties": {
                              "name": {
                                "type": "string",
                                "minLength": 1,
                                "maxLength": 100
                              },
                              "sets": {
                                "type": "integer",
                                "minimum": 1,
                                "maximum": 10
                              },
                              "reps": {
                                "type": "string",
                                "minLength": 1,
                                "maxLength": 40
                              },
                              "restSeconds": {
                                "type": "integer",
                                "minimum": 0,
                                "maximum": 600
                              },
                              "notes": {
                                "anyOf": [
                                  {
                                    "type": "string",
                                    "minLength": 1,
                                    "maxLength": 300
                                  },
                                  {
                                    "type": "null"
                                  }
                                ]
                              }
                            },
                            "required": [
                              "name",
                              "sets",
                              "reps",
                              "restSeconds",
                              "notes"
                            ],
                            "additionalProperties": false
                          }
                        },
                        "warmUp": {
                          "type": "string",
                          "minLength": 1,
                          "maxLength": 500
                        },
                        "coolDown": {
                          "type": "string",
                          "minLength": 1,
                          "maxLength": 500
                        }
                      },
                      "required": [
                        "key",
                        "type",
                        "title",
                        "durationMinutes",
                        "instructions",
                        "effortRpe",
                        "distanceKm",
                        "intervals",
                        "exercises",
                        "warmUp",
                        "coolDown"
                      ],
                      "additionalProperties": false
                    }
                  }
                },
                "required": [
                  "date",
                  "label",
                  "rest",
                  "sessions"
                ],
                "additionalProperties": false
              }
            }
          },
          "required": [
            "title",
            "goal",
            "startDate",
            "endDate",
            "timeZone",
            "days"
          ],
          "additionalProperties": false
        },
        "assumptions": {
          "maxItems": 12,
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1,
            "maxLength": 300
          }
        },
        "warnings": {
          "maxItems": 12,
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1,
            "maxLength": 300
          }
        }
      },
      "required": [
        "schemaVersion",
        "status",
        "plan",
        "assumptions",
        "warnings"
      ],
      "additionalProperties": false
    },
    {
      "type": "object",
      "properties": {
        "schemaVersion": {
          "type": "string",
          "const": "root-plan/v1"
        },
        "status": {
          "type": "string",
          "enum": [
            "needs_input",
            "needs_review"
          ]
        },
        "questions": {
          "minItems": 1,
          "maxItems": 12,
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1,
            "maxLength": 300
          }
        }
      },
      "required": [
        "schemaVersion",
        "status",
        "questions"
      ],
      "additionalProperties": false
    }
  ]
}
