{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://dealeragentprotocol.com/spec/v0.1/schemas/common.schema.json",
  "title": "Dealer Agent Protocol common types",
  "$defs": {
    "identifier": {
      "type": "string",
      "minLength": 1,
      "maxLength": 128,
      "pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]*$"
    },
    "timestamp": {
      "type": "string",
      "format": "date-time"
    },
    "date": {
      "type": "string",
      "format": "date"
    },
    "currency": {
      "type": "string",
      "pattern": "^[A-Z]{3}$"
    },
    "money": {
      "type": "object",
      "additionalProperties": false,
      "required": ["amount_minor", "currency"],
      "properties": {
        "amount_minor": {
          "type": "integer",
          "description": "Exact amount in the ISO 4217 currency minor unit."
        },
        "currency": { "$ref": "#/$defs/currency" }
      }
    },
    "nonNegativeMoney": {
      "allOf": [
        { "$ref": "#/$defs/money" },
        {
          "properties": {
            "amount_minor": { "minimum": 0 }
          }
        }
      ]
    },
    "jurisdiction": {
      "type": "object",
      "additionalProperties": false,
      "required": ["country"],
      "properties": {
        "country": { "type": "string", "pattern": "^[A-Z]{2}$" },
        "subdivision": { "type": "string", "pattern": "^[A-Z]{2}-[A-Z0-9]{1,3}$" },
        "locality": { "type": "string", "minLength": 1, "maxLength": 128 },
        "postal_code": { "type": "string", "minLength": 1, "maxLength": 32 }
      }
    },
    "address": {
      "type": "object",
      "additionalProperties": false,
      "required": ["lines", "locality", "country"],
      "properties": {
        "lines": {
          "type": "array",
          "minItems": 1,
          "maxItems": 5,
          "items": { "type": "string", "minLength": 1, "maxLength": 160 }
        },
        "locality": { "type": "string", "minLength": 1, "maxLength": 128 },
        "region": { "type": "string", "minLength": 1, "maxLength": 64 },
        "postal_code": { "type": "string", "minLength": 1, "maxLength": 32 },
        "country": { "type": "string", "pattern": "^[A-Z]{2}$" }
      }
    },
    "source": {
      "type": "object",
      "additionalProperties": false,
      "required": ["source_name", "authority", "observed_at"],
      "properties": {
        "source_name": { "type": "string", "minLength": 1, "maxLength": 128 },
        "source_uri": { "type": "string", "format": "uri" },
        "source_record_id": { "type": "string", "minLength": 1, "maxLength": 256 },
        "authority": {
          "type": "string",
          "enum": [
            "authoritative_dealer_system",
            "dealer_asserted",
            "oem",
            "government",
            "third_party",
            "derived",
            "unknown"
          ]
        },
        "observed_at": { "$ref": "#/$defs/timestamp" },
        "content_digest": { "type": "string", "pattern": "^sha256:[a-f0-9]{64}$" }
      }
    },
    "provenance": {
      "type": "object",
      "additionalProperties": false,
      "required": ["sources", "authority_status"],
      "properties": {
        "sources": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/$defs/source" }
        },
        "authority_status": {
          "type": "string",
          "enum": ["authoritative", "asserted", "derived", "unknown"]
        },
        "transformed_at": { "$ref": "#/$defs/timestamp" },
        "transformations": {
          "type": "array",
          "maxItems": 32,
          "items": { "type": "string", "minLength": 1, "maxLength": 160 }
        }
      }
    },
    "freshness": {
      "type": "object",
      "additionalProperties": false,
      "required": ["observed_at", "state"],
      "properties": {
        "observed_at": { "$ref": "#/$defs/timestamp" },
        "valid_until": { "$ref": "#/$defs/timestamp" },
        "state": {
          "type": "string",
          "enum": ["current", "stale", "unknown", "withdrawn"]
        },
        "max_age_seconds": { "type": "integer", "minimum": 0, "maximum": 2592000 }
      }
    },
    "uncertainty": {
      "type": "object",
      "additionalProperties": false,
      "required": ["status"],
      "properties": {
        "status": { "type": "string", "enum": ["exact", "estimated", "unknown", "not_applicable"] },
        "reason": { "type": "string", "minLength": 1, "maxLength": 500 },
        "assumptions": {
          "type": "array",
          "maxItems": 32,
          "items": { "type": "string", "minLength": 1, "maxLength": 500 }
        }
      }
    },
    "pageRequest": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "cursor": { "type": "string", "minLength": 1, "maxLength": 2048 },
        "limit": { "type": "integer", "minimum": 1, "maximum": 100 }
      }
    },
    "pageInfo": {
      "type": "object",
      "additionalProperties": false,
      "required": ["has_more"],
      "properties": {
        "next_cursor": { "type": "string", "minLength": 1, "maxLength": 2048 },
        "has_more": { "type": "boolean" }
      }
    },
    "traceId": {
      "type": "string",
      "minLength": 8,
      "maxLength": 128,
      "pattern": "^[A-Za-z0-9._:-]+$"
    },
    "extensionBag": {
      "type": "object",
      "propertyNames": {
        "pattern": "^[a-z0-9]+(?:[.-][a-z0-9]+)+$"
      },
      "additionalProperties": true
    }
  }
}
