{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://cat-body-studio.local/contracts/v1/api.schema.json",
  "title": "Cat Body Studio Control & Event Channel API v1.1",
  "type": "object",
  "$defs": {
    "ErrorCode": {
      "type": "string",
      "enum": [
        "INVALID_JSON",
        "MISSING_OP",
        "UNKNOWN_OP",
        "INVALID_PARAM",
        "OUT_OF_RANGE",
        "RIG_NOT_FOUND",
        "DUPLICATE_HANDLE",
        "CAT_NOT_FOUND",
        "GUIDE_LIMIT",
        "STATE_CONFLICT",
        "ENGINE_NOT_READY"
      ]
    },
    "ErrorResponse": {
      "type": "object",
      "required": ["ok", "code", "error"],
      "properties": {
        "ok": { "const": false },
        "code": { "$ref": "#/$defs/ErrorCode" },
        "error": { "type": "string" },
        "field": { "type": "string" }
      }
    },
    "Category": {
      "type": "string",
      "enum": [
        "DomesticShorthair",
        "BritishShorthair",
        "OrientalShorthair",
        "Longhair",
        "ShortLeg"
      ]
    },
    "Action": {
      "type": "string",
      "enum": ["idle", "walk", "trot", "run", "sit", "rest", "play"]
    },
    "Terrain": {
      "type": "string",
      "enum": ["flat", "ramp", "steps"]
    },
    "EventType": {
      "type": "string",
      "enum": [
        "paw_contact_changed",
        "root_stopped_changed",
        "four_paw_settled_changed",
        "stifle_limit_exceeded",
        "action_changed",
        "target_reached",
        "rebase_occurred"
      ]
    },
    "Vec2": {
      "type": "array",
      "items": { "type": "number" },
      "minItems": 2,
      "maxItems": 2
    },
    "UserTune": {
      "type": "object",
      "properties": {
        "walk_bounce": { "type": "number", "minimum": 0.0, "maximum": 0.1 },
        "run_bounce": { "type": "number", "minimum": 0.05, "maximum": 0.25 },
        "scapula_rise": { "type": "number", "minimum": 0.0, "maximum": 0.02 },
        "spine_line": { "type": "number", "minimum": 20.0, "maximum": 400.0 },
        "spine_flex": { "type": "number", "minimum": 0.2, "maximum": 2.0 },
        "tail_line": { "type": "number", "minimum": 0.0, "maximum": 2.0 },
        "ik_reach": { "type": "number", "minimum": 132.0, "maximum": 152.0 },
        "ik_soft": { "type": "number", "minimum": 130.0, "maximum": 146.0 },
        "ik_yield": { "type": "number", "minimum": 0.0, "maximum": 0.08 },
        "ik_stretch": { "type": "number", "minimum": 0.0, "maximum": 8.0 }
      }
    },
    "FormPatch": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "reset": { "type": "boolean" },
        "lines": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "dorsal_scale": { "type": "number", "minimum": 0.5, "maximum": 1.5 },
            "dorsal_lift": { "type": "number", "minimum": -0.25, "maximum": 0.25 },
            "belly_bow": { "type": "number", "minimum": -1.0, "maximum": 1.0 },
            "guides": {
              "type": "array",
              "maxItems": 4,
              "items": { "$ref": "#/$defs/Vec2" }
            },
            "add": { "$ref": "#/$defs/Vec2" },
            "clear": { "type": "boolean" }
          }
        },
        "mesh": {
          "type": "object",
          "properties": {
            "chest": { "type": "number", "minimum": -1.0, "maximum": 1.0 },
            "rump": { "type": "number", "minimum": -1.0, "maximum": 1.0 },
            "shoulder": { "type": "number", "minimum": -1.0, "maximum": 1.0 },
            "hip": { "type": "number", "minimum": -1.0, "maximum": 1.0 }
          }
        },
        "bounce": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "gain": { "type": "number", "minimum": 0.25, "maximum": 2.5 },
            "damping": { "type": "number", "minimum": 0.0, "maximum": 1.0 },
            "fore_impulse": { "type": "number", "minimum": -1.0, "maximum": 1.0 },
            "freq": { "type": "number", "minimum": 0.5, "maximum": 1.5 }
          }
        },
        "ik": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "paw_lift": {
              "type": "array",
              "minItems": 4,
              "maxItems": 4,
              "items": { "type": "number", "minimum": -0.05, "maximum": 0.2 }
            },
            "reach": { "type": "number", "minimum": 0.85, "maximum": 1.15 },
            "stifle_bias": { "type": "number", "minimum": -1.0, "maximum": 1.0 }
          }
        }
      }
    },
    "RigEvent": {
      "type": "object",
      "required": ["seq", "tick", "time_s", "handle", "type", "data"],
      "properties": {
        "seq": { "type": "integer", "minimum": 1 },
        "tick": { "type": "integer", "minimum": 0 },
        "time_s": { "type": "number", "minimum": 0.0 },
        "handle": { "type": "integer", "minimum": 1 },
        "type": { "$ref": "#/$defs/EventType" },
        "data": { "type": "object" }
      }
    },
    "LocomotionEvents": {
      "type": "object",
      "required": [
        "root_stopped",
        "root_translation_stopped",
        "com_speed_stopped",
        "four_paw_settled",
        "geometric_contact",
        "load_contact",
        "max_stifle_deg",
        "over_155_ticks",
        "froude",
        "com_speed_legs"
      ],
      "properties": {
        "root_stopped": { "type": "boolean" },
        "root_translation_stopped": { "type": "boolean" },
        "com_speed_stopped": { "type": "boolean" },
        "four_paw_settled": { "type": "boolean" },
        "geometric_contact": { "type": "array", "items": { "type": "boolean" }, "minItems": 4, "maxItems": 4 },
        "load_contact": { "type": "array", "items": { "type": "boolean" }, "minItems": 4, "maxItems": 4 },
        "max_stifle_deg": { "type": "number" },
        "over_155_ticks": { "type": "integer", "minimum": 0 },
        "froude": { "type": "number" },
        "com_speed_legs": { "type": "number" }
      }
    }
  },
  "required": ["op"],
  "properties": {
    "op": {
      "type": "string",
      "enum": [
        "capabilities", "schema", "anatomy",
        "new", "open", "roster", "select", "preview_founder", "create_founder",
        "save", "encounter", "get", "adopt", "settle", "condition", "breed_check", "breed",
        "rig_bind", "rig_new", "rig_set", "rig_get", "rig_set_target",
        "rig_step", "rig_step_crowd", "rig_drop", "rig_hind_structure",
        "rig_telemetry", "rig_drain_events", "rig_event_filter"
      ]
    }
  }
}
