{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://jishie.com/spec/demand.schema.json",
  "title": "demand.json Demand Set (v0.1-draft)",
  "type": "object",
  "required": ["demand_set", "publisher", "signature", "demands"],
  "properties": {
    "demand_set": { "const": "0.1" },
    "publisher": {
      "type": "object",
      "required": ["name", "domain"],
      "properties": {
        "name": { "type": "string", "minLength": 1 },
        "domain": { "type": "string", "minLength": 3 },
        "agent_card": { "type": "string", "format": "uri" },
        "contact": { "type": "string", "format": "uri" },
        "index_ref": { "type": "string", "pattern": "^aix_[a-z0-9]+$" }
      }
    },
    "signature": {
      "type": "object",
      "required": ["alg", "signed_at", "sig"],
      "properties": {
        "alg": { "enum": ["ed25519", "dev"] },
        "keys": { "type": "string", "format": "uri" },
        "keys_inline": { "type": "object", "description": "JWK (OKP/Ed25519) for inline verification" },
        "signed_at": { "type": "string" },
        "sig": { "type": "string", "minLength": 3, "description": "base64(ed25519(canonical_json(demands)))" }
      }
    },
    "demands": {
      "type": "array",
      "minItems": 1,
      "maxItems": 50,
      "items": { "$ref": "#/$defs/demand" }
    }
  },
  "$defs": {
    "demand": {
      "type": "object",
      "required": ["id", "status", "visibility", "capability", "window", "updated_at"],
      "properties": {
        "id": { "type": "string", "pattern": "^dmd_[a-z0-9]+$" },
        "status": { "enum": ["open", "paused", "filled", "expired"] },
        "visibility": { "enum": ["public", "index-only", "sealed"] },
        "instrument": { "type": "string", "pattern": "^[A-Z0-9-]+@[0-9.]+$" },
        "capability": {
          "type": "object",
          "properties": {
            "skill": { "type": "string", "minLength": 2 },
            "category": { "type": "string", "minLength": 2, "description": "REQUIRED when visibility=sealed (the generalized public view)" },
            "taxonomy": { "type": "string", "format": "uri" },
            "description": { "type": "string", "maxLength": 1000 },
            "input_modes": { "type": "array", "items": { "type": "string" } },
            "output_modes": { "type": "array", "items": { "type": "string" } },
            "quality_bar": {
              "type": "object",
              "properties": {
                "acceptance_test": { "type": "string", "format": "uri" },
                "min_field_accuracy": { "type": "number", "minimum": 0, "maximum": 1 }
              }
            }
          }
        },
        "commercials": {
          "type": "object",
          "required": ["pricing_model", "ceiling", "volume", "settlement"],
          "properties": {
            "pricing_model": { "type": "string" },
            "ceiling": {
              "type": "object",
              "required": ["amount", "currency"],
              "properties": { "amount": { "type": "number", "exclusiveMinimum": 0 }, "currency": { "type": "string" } }
            },
            "volume": {
              "type": "object",
              "required": ["expected", "period"],
              "properties": {
                "expected": { "type": "number", "exclusiveMinimum": 0 },
                "period": { "enum": ["hour", "day", "week", "month"] },
                "recurring": { "type": "boolean" }
              }
            },
            "budget_cap": {
              "type": "object",
              "properties": {
                "amount": { "type": "number", "exclusiveMinimum": 0 },
                "currency": { "type": "string" },
                "period": { "enum": ["day", "week", "month"] }
              }
            },
            "settlement": { "type": "array", "minItems": 1, "items": { "type": "string", "minLength": 2 } }
          }
        },
        "constraints": {
          "type": "object",
          "properties": {
            "regions": { "type": "array", "items": { "type": "string" } },
            "data_residency": { "type": "string" },
            "max_p95_ms": { "type": "number", "exclusiveMinimum": 0 },
            "protocols": { "type": "array", "items": { "enum": ["mcp", "a2a", "http"] } },
            "compliance": { "type": "array", "items": { "type": "string" } },
            "min_provider_depth": { "type": "integer", "minimum": 0, "maximum": 3 }
          }
        },
        "window": {
          "type": "object",
          "required": ["opens", "expires"],
          "properties": { "opens": { "type": "string" }, "expires": { "type": "string" } },
          "description": "expires MUST be after opens and at most 90 days later"
        },
        "offer_endpoint": { "type": "string", "format": "uri" },
        "escrow_required": { "type": "boolean" },
        "auto_match": { "type": "boolean" },
        "updated_at": { "type": "string" }
      }
    }
  }
}
