{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://autoagentprotocol.org/v3.2/schemas/inventory-search-response.schema.json",
  "title": "InventorySearchResponse",
  "description": "Typed inventory search result shared by MCP, A2A and PAP. Reports the effective filters and ordering, a continuation cursor and listings with evidence and freshness. A page-size limit never limits the total traversable matching set.",
  "type": "object",
  "properties": {
    "type": { "const": "inventory.search.response" },
    "data": {
      "type": "object",
      "properties": {
        "currency": {
          "$ref": "./_primitives/currency.schema.json",
          "default": "USD",
          "description": "Effective query currency for every matched vehicle and every price comparison. Omitted means USD, independently of request or capability defaults; CAD results MUST include this field and label every Vehicle.currency as CAD."
        },
        "mileage_unit": {
          "$ref": "./_primitives/mileage-unit.schema.json",
          "default": "mi",
          "description": "Effective query unit used for mileage_max, mileage sorting and mileage_range. Omitted means mi; km query results MUST include this field. This context never overrides a returned vehicle's native mileage_unit or the explicit _mi fields."
        },
        "total": {
          "type": ["integer", "null"],
          "minimum": 0,
          "description": "Exact number of matching vehicles across all pages, or null when an exact count is unavailable. Never report the page size, a capped count or an estimate as the total."
        },
        "skip": { "type": "integer", "minimum": 0, "description": "Echo of the request's pagination.skip." },
        "limit": {"type": "integer", "minimum": 1, "maximum": 100, "description": "Effective page-size limit, including the deployment default when the caller omitted it."},
        "vehicles": {
          "type": "array",
          "description": "Vehicles in this page, in the requested order. Items MAY span multiple vehicle types (`car`, `motorcycle`, `trailer`, `rv`, `other`); each carries its own `vehicle_type` (absent = car). Each item is a Vehicle constrained to the sale-condition vocabulary (`new`|`used`|`cpo`) — inventory listings never carry trade-in wear values — and MUST carry a `status` of `available`, `intransit`, or `pending`. Out-of-stock vehicles are never returned.",
          "items": {
            "$ref": "./inventory-listing.schema.json"
          }
        },
        "facets": {
          "$ref": "./facets.schema.json",
          "description": "Optional aggregated facets over the matching set."
        },
        "next_cursor": {
          "type": [
            "string",
            "null"
          ],
          "minLength": 1,
          "maxLength": 4096,
          "description": "Opaque token for the next page, or null only when the matching set is exhausted. An empty page with a non-null cursor can occur during concurrent inventory changes; it does not mean the search is finished."
        },
        "applied_filters": {
          "$ref": "./inventory-search-request.schema.json#/$defs/filters",
          "description": "Every effective filter after documented normalization, including defaults that restrict the matching set. Unsupported input filters cause an error rather than disappearing from this object."
        },
        "sort": {
          "$ref": "./inventory-search-request.schema.json#/properties/sort",
          "description": "Effective ordering, including the deployment default when the caller omitted sort."
        },
        "query_terms": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "string",
            "minLength": 1
          },
          "description": "Actual case-insensitive keyword tokens when applied_filters.query is present. No hidden natural-language constraints are introduced."
        }
      },
      "required": ["total", "limit", "next_cursor", "applied_filters", "sort", "vehicles"],
      "additionalProperties": false,
      "dependentSchemas": {
        "currency": {
          "description": "Explicit 3.1 query contexts require matching vehicle currencies and facet labels. Vehicle odometer units remain independent; omitted contexts and labels retain USD/mi.",
          "allOf": [
            {
              "if": { "properties": { "currency": { "const": "CAD" } }, "required": ["currency"] },
              "then": {
                "properties": {
                  "vehicles": { "items": { "properties": { "currency": { "const": "CAD" } }, "required": ["currency"] } },
                  "facets": { "properties": { "currency": { "const": "CAD" } }, "required": ["currency"] }
                }
              },
              "else": {
                "properties": {
                  "vehicles": { "items": { "properties": { "currency": { "const": "USD" } } } },
                  "facets": { "properties": { "currency": { "const": "USD" } } }
                }
              }
            },
            {
              "if": { "properties": { "mileage_unit": { "const": "km" } }, "required": ["mileage_unit"] },
              "then": { "properties": { "facets": { "properties": { "mileage_unit": { "const": "km" } }, "required": ["mileage_unit"] } } },
              "else": { "properties": { "facets": { "properties": { "mileage_unit": { "const": "mi" } } } } }
            }
          ]
        },
        "mileage_unit": { "$ref": "#/properties/data/dependentSchemas/currency" }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "applied_filters": {
                "required": [
                  "near"
                ]
              }
            },
            "required": [
              "applied_filters"
            ]
          },
          "then": {
            "properties": {
              "vehicles": {
                "items": {
                  "required": [
                    "distance_mi"
                  ]
                }
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "applied_filters": {
                "required": [
                  "query"
                ]
              }
            },
            "required": [
              "applied_filters"
            ]
          },
          "then": {
            "required": [
              "query_terms"
            ]
          }
        },
        {
          "if": {
            "properties": {
              "applied_filters": {
                "anyOf": [
                  {
                    "required": [
                      "features_all"
                    ]
                  },
                  {
                    "required": [
                      "features_any"
                    ]
                  }
                ]
              }
            },
            "required": [
              "applied_filters"
            ]
          },
          "then": {
            "properties": {
              "vehicles": {
                "items": {
                  "properties": {
                    "feature_evidence": {
                      "minItems": 1
                    }
                  },
                  "required": [
                    "feature_evidence"
                  ]
                }
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "sort": {
                "properties": {
                  "field": {
                    "const": "distance_mi"
                  }
                },
                "required": [
                  "field"
                ]
              }
            },
            "required": [
              "sort"
            ]
          },
          "then": {
            "properties": {
              "applied_filters": {
                "required": [
                  "near"
                ]
              }
            }
          }
        }
      ]
    },
    "message": {
      "type": "string",
      "description": "Optional contextual note. MAY be omitted.",
      "maxLength": 2000
    }
  },
  "required": ["type", "data"],
  "additionalProperties": false
}
