{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://univip.org/schemas/v0.2/command.schema.json",
  "title": "UNIVIP command v0.2",
  "type": "object",
  "required": ["schema", "command_id", "type", "issued_at", "params"],
  "properties": {
    "schema": { "type": "string", "pattern": "^univip\\.command\\.v0\\.[0-9]+$", "description": "Any 0.x MINOR is accepted (Part 01 §1.5)" },
    "command_id": { "$ref": "https://univip.org/schemas/v0.2/common.schema.json#/$defs/Id" },
    "type": { "$ref": "https://univip.org/schemas/v0.2/common.schema.json#/$defs/CommandType" },
    "issued_at": { "$ref": "https://univip.org/schemas/v0.2/common.schema.json#/$defs/Timestamp" },
    "expires_at": { "$ref": "https://univip.org/schemas/v0.2/common.schema.json#/$defs/Timestamp" },
    "lease_id": { "type": "string" },
    "payload_id": { "$ref": "https://univip.org/schemas/v0.2/common.schema.json#/$defs/Id" },
    "operation_id": { "$ref": "https://univip.org/schemas/v0.2/common.schema.json#/$defs/Id", "description": "Cross-vehicle operation correlation identifier (Part 15 §15.3.4)" },
    "params": { "type": "object" },
    "signature": { "$ref": "https://univip.org/schemas/v0.2/common.schema.json#/$defs/DetachedJws", "description": "Detached JWS by the lease holder's registered key over the canonical command body (Part 13 §13.14)" },
    "signatures": {
      "type": "array",
      "minItems": 2,
      "items": { "$ref": "https://univip.org/schemas/v0.2/common.schema.json#/$defs/DetachedJws" },
      "description": "M-of-N dual-control detached JWS signatures for Tier 3 high-consequence commands (Part 13 §13.21)"
    }
  },
  "allOf": [
    { "if": { "properties": { "type": { "pattern": "^(camera|payload)\\." } } },
      "then": { "required": ["payload_id"] } },
    { "if": { "properties": { "type": { "pattern": "^vehicle\\.", "not": { "const": "vehicle.fleet.set_corridor" } } } },
      "then": { "required": ["lease_id"] } },
    { "if": { "properties": { "type": { "const": "camera.gimbal.set_orientation" } } },
      "then": { "properties": { "params": { "$ref": "https://univip.org/schemas/v0.2/common.schema.json#/$defs/GimbalOrientation" } } } },
    { "if": { "properties": { "type": { "const": "camera.gimbal.move_continuous" } } },
      "then": { "required": ["expires_at"], "properties": { "params": { "type": "object", "properties": {
        "roll_rate_degps": { "type": "number" }, "pitch_rate_degps": { "type": "number" }, "yaw_rate_degps": { "type": "number" } } } } } },
    { "if": { "properties": { "type": { "const": "camera.zoom.set" } } },
      "then": { "properties": { "params": { "required": ["zoom_ratio"], "properties": { "zoom_ratio": { "type": "number", "exclusiveMinimum": 0 } } } } } },
    { "if": { "properties": { "type": { "const": "vehicle.goto" } } },
      "then": { "properties": { "params": {
        "required": ["latitude_deg", "longitude_deg"],
        "oneOf": [ { "required": ["altitude_msl_m"] }, { "required": ["altitude_agl_m"] }, { "required": ["altitude_rel_m"] }, { "required": ["depth_m"] } ],
        "properties": {
          "latitude_deg": { "$ref": "https://univip.org/schemas/v0.2/common.schema.json#/$defs/Latitude" },
          "longitude_deg": { "$ref": "https://univip.org/schemas/v0.2/common.schema.json#/$defs/Longitude" },
          "altitude_msl_m": { "type": "number" },
          "altitude_agl_m": { "type": "number", "minimum": 0 },
          "altitude_rel_m": { "type": "number" },
          "depth_m": { "type": "number", "minimum": 0 },
          "ground_speed_mps": { "type": "number", "exclusiveMinimum": 0 },
          "heading_deg": { "$ref": "https://univip.org/schemas/v0.2/common.schema.json#/$defs/Degrees360" },
          "hold_on_arrival_s": { "$ref": "https://univip.org/schemas/v0.2/common.schema.json#/$defs/NonNegative" } } } } } },
    { "if": { "properties": { "type": { "const": "vehicle.move_relative" } } },
      "then": { "properties": { "params": { "required": ["frame"], "properties": {
        "frame": { "enum": ["BODY", "NED"] },
        "forward_m": { "type": "number" }, "right_m": { "type": "number" },
        "north_m": { "type": "number" }, "east_m": { "type": "number" },
        "down_m": { "type": "number" }, "yaw_deg": { "$ref": "https://univip.org/schemas/v0.2/common.schema.json#/$defs/Degrees180" },
        "ground_speed_mps": { "type": "number", "exclusiveMinimum": 0 } } } } } },
    { "if": { "properties": { "type": { "const": "vehicle.dive_to" } } },
      "then": { "properties": { "params": { "required": ["depth_m"], "properties": { "depth_m": { "type": "number", "minimum": 0 } } } } } },
    { "if": { "properties": { "type": { "const": "vehicle.return_home" } } },
      "then": { "properties": { "params": { "properties": {
        "altitude_rel_m": { "type": "number", "minimum": 0 }, "land_on_arrival": { "type": "boolean" } } } } } },
    { "if": { "properties": { "type": { "const": "vehicle.mission.upload" } } },
      "then": { "properties": { "params": { "required": ["mission"], "properties": { "mission": { "$ref": "https://univip.org/schemas/v0.2/mission.schema.json" } } } } } },
    { "if": { "properties": { "type": { "pattern": "^vehicle\\.mission\\.(start|pause|resume|abort)$" } } },
      "then": { "properties": { "params": { "required": ["mission_id"], "properties": { "mission_id": { "type": "string" }, "from_waypoint": { "type": "integer", "minimum": 0 } } } } } },
    { "if": { "properties": { "type": { "const": "vehicle.autonomy.assign_goal" } } },
      "then": { "properties": { "params": {
        "required": ["goal_id", "goal_type"],
        "properties": {
          "goal_id": { "$ref": "https://univip.org/schemas/v0.2/common.schema.json#/$defs/Id" },
          "goal_type": { "type": "string", "minLength": 1 },
          "target_area": { "$ref": "https://univip.org/schemas/v0.2/common.schema.json#/$defs/GeoJSONPolygon" },
          "target_id": { "type": "string" },
          "constraints": { "type": "object" }
        }
      } } } },
    { "if": { "properties": { "type": { "const": "vehicle.autonomy.update_constraints" } } },
      "then": { "properties": { "params": {
        "required": ["goal_id", "constraints"],
        "properties": {
          "goal_id": { "$ref": "https://univip.org/schemas/v0.2/common.schema.json#/$defs/Id" },
          "constraints": { "type": "object" }
        }
      } } } },
    { "if": { "properties": { "type": { "const": "vehicle.autonomy.assist_response" } } },
      "then": { "properties": { "params": {
        "required": ["event_id", "decision"],
        "properties": {
          "event_id": { "$ref": "https://univip.org/schemas/v0.2/common.schema.json#/$defs/Id" },
          "decision": { "enum": ["APPROVE_OPTION", "SKIP_OBJECTIVE", "HOLD", "RETURN_HOME", "ABORT"] },
          "option_id": { "type": "string" }
        }
      } } } },
    { "if": { "properties": { "type": { "const": "vehicle.fleet.set_corridor" } } },
      "then": { "properties": { "params": {
        "required": ["corridor_id", "valid_until"],
        "properties": {
          "corridor_id": { "$ref": "https://univip.org/schemas/v0.2/common.schema.json#/$defs/Id" },
          "valid_until": { "$ref": "https://univip.org/schemas/v0.2/common.schema.json#/$defs/Timestamp" },
          "allowed_volume": { "$ref": "https://univip.org/schemas/v0.2/common.schema.json#/$defs/GeoJSONPolygon" },
          "keep_out_zones": { "type": "array", "items": { "$ref": "https://univip.org/schemas/v0.2/common.schema.json#/$defs/GeoJSONPolygon" } }
        }
      } } } },
    { "if": { "properties": { "type": { "const": "vehicle.fleet.handoff" } } },
      "then": { "properties": { "params": {
        "required": ["handoff_id", "operation_id", "role", "peer_device_id", "on_relieved"],
        "properties": {
          "handoff_id": { "$ref": "https://univip.org/schemas/v0.2/common.schema.json#/$defs/Id" },
          "operation_id": { "$ref": "https://univip.org/schemas/v0.2/common.schema.json#/$defs/Id" },
          "role": { "enum": ["PRIMARY", "RELIEF"] },
          "peer_device_id": { "$ref": "https://univip.org/schemas/v0.2/common.schema.json#/$defs/DeviceId" },
          "watch_target": { "$ref": "https://univip.org/schemas/v0.2/common.schema.json#/$defs/GeoPoint" },
          "target_id": { "type": "string" },
          "on_relieved": { "enum": ["RETURN_HOME", "DOCK", "HOLD"] }
        }
      } } } }
  ]
}
