{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://coach.app/schemas/workout.schema.json",
  "title": "Coach Workout",
  "description": "Workout template consumed by the Coach iOS/watchOS app. Paste this schema into any AI chatbot along with your training goals and it will return JSON that imports directly into Coach via iCloud Drive. Forward-compatible: unknown properties are ignored by the decoder.",
  "type": "object",
  "additionalProperties": true,
  "required": ["id", "version", "name", "createdAt", "blocks"],
  "properties": {
    "id": {
      "type": "string",
      "format": "uuid",
      "description": "UUIDv4. Stable identity across versions."
    },
    "version": {
      "type": "integer",
      "minimum": 1,
      "description": "Monotonic per id. Higher version overwrites lower on import."
    },
    "name": { "type": "string", "minLength": 1 },
    "createdBy": {
      "type": "string",
      "description": "Free-form author label, e.g. 'chatgpt', 'claude', or your name."
    },
    "createdAt": {
      "type": "string",
      "format": "date-time",
      "description": "ISO-8601 UTC timestamp."
    },
    "tags": {
      "type": "array",
      "items": { "type": "string" },
      "description": "Free-form labels. Recognized: rehab, strength, cardio, mobility, knee, upper, lower, intervals. The 'rehab' tag activates Coach's rehab-specific features (session-end pain gate + next-morning reactivity check)."
    },
    "notes": {
      "type": "string",
      "description": "High-level coaching notes. For rehab workouts, document pain gate / stop conditions here."
    },
    "blocks": {
      "type": "array",
      "minItems": 1,
      "items": { "$ref": "#/$defs/block" }
    }
  },
  "$defs": {
    "block": {
      "type": "object",
      "additionalProperties": true,
      "required": ["type", "name", "exercises"],
      "properties": {
        "type": { "enum": ["warmup", "main", "cooldown"] },
        "name": { "type": "string", "minLength": 1 },
        "exercises": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/$defs/exercise" }
        }
      }
    },
    "exercise": {
      "type": "object",
      "additionalProperties": true,
      "required": ["exerciseId", "name", "sets"],
      "properties": {
        "exerciseId": {
          "type": "string",
          "pattern": "^[a-z0-9-]+$",
          "description": "Stable kebab-case slug, e.g. 'spanish-squat'."
        },
        "name": { "type": "string", "minLength": 1 },
        "videoUrl": { "type": "string", "format": "uri" },
        "coachNotes": { "type": "string" },
        "muscleGroups": {
          "type": "array",
          "uniqueItems": true,
          "items": { "$ref": "#/$defs/muscleGroup" },
          "description": "Predefined muscle groups primarily targeted by this exercise. Optional; flat list (no primary/secondary split). Tokens are stable English kebab-case."
        },
        "sets": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/$defs/set" }
        }
      }
    },
    "muscleGroup": {
      "type": "string",
      "enum": [
        "chest",
        "upper-back",
        "lats",
        "lower-back",
        "shoulders-front",
        "shoulders-side",
        "shoulders-rear",
        "biceps",
        "triceps",
        "forearms",
        "abs",
        "obliques",
        "glutes",
        "quads",
        "hamstrings",
        "calves",
        "hip-flexors",
        "adductors",
        "abductors",
        "neck"
      ],
      "description": "Closed vocabulary. Running / general cardio is not a muscle group — leave the array empty (or omit it) for running-only blocks."
    },
    "set": {
      "type": "object",
      "description": "Either rep-based (reps required) or duration-based (durationSeconds required). Exactly one of the two modes must be present.",
      "additionalProperties": true,
      "oneOf": [
        {
          "required": ["reps"],
          "properties": {
            "reps": { "type": "integer", "minimum": 1 },
            "tempo": {
              "type": "string",
              "description": "Four-digit tempo string, e.g. '3-1-1-0' (eccentric-bottom-concentric-top)."
            },
            "load": {
              "type": "string",
              "description": "Free-form load descriptor: 'bodyweight', '20kg', 'RPE 7', '60% 1RM'."
            },
            "rest": {
              "type": "integer",
              "minimum": 0,
              "description": "Rest after this set, in seconds."
            }
          }
        },
        {
          "required": ["durationSeconds"],
          "properties": {
            "durationSeconds": { "type": "integer", "minimum": 1 },
            "load": { "type": "string" },
            "rest": { "type": "integer", "minimum": 0 }
          }
        }
      ]
    }
  }
}
