{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:askmeddy:directory-batch:v1",
  "title": "askMeddy directory batch v1",
  "description": "Create or partially update directory records. Cross-record and revision checks require server validation.",
  "type": "object",
  "additionalProperties": false,
  "required": ["schemaVersion", "operations"],
  "properties": {
    "schemaVersion": { "const": "1.0" },
    "operations": { "type": "array", "minItems": 1, "maxItems": 100, "items": { "$ref": "#/$defs/operation" } }
  },
  "$defs": {
    "id": { "type": "string", "minLength": 1, "maxLength": 128, "pattern": "^[A-Za-z0-9][A-Za-z0-9_-]*$" },
    "shortText": { "type": "string", "minLength": 1, "maxLength": 300 },
    "optionalText": { "type": ["string", "null"], "maxLength": 4000 },
    "ids": { "type": "array", "maxItems": 100, "uniqueItems": true, "items": { "$ref": "#/$defs/id" } },
    "tags": { "type": "array", "maxItems": 50, "uniqueItems": true, "items": { "$ref": "#/$defs/shortText" } },
    "status": { "enum": ["draft", "published", "retired"] },
    "price": {
      "type": ["object", "null"], "additionalProperties": false,
      "required": ["amount", "currency"],
      "properties": {
        "amount": { "type": "number", "minimum": 0, "maximum": 1000000000, "multipleOf": 0.01 },
        "currency": { "type": "string", "pattern": "^[A-Z]{3}$" },
        "basis": { "type": ["string", "null"], "maxLength": 120 }
      }
    },
    "provider": {
      "type": "object", "additionalProperties": false, "minProperties": 1,
      "properties": {
        "name": { "$ref": "#/$defs/shortText" },
        "categories": { "type": "array", "minItems": 1, "maxItems": 20, "uniqueItems": true, "items": { "enum": ["diagnostics", "clinic", "hospital", "pharmacy", "specialist", "telemedicine", "other"] } },
        "description": { "$ref": "#/$defs/optionalText" },
        "website": { "type": ["string", "null"], "maxLength": 2048, "pattern": "^https?://" },
        "phone": { "type": ["string", "null"], "maxLength": 120 },
        "email": { "type": ["string", "null"], "maxLength": 254 },
        "aliases": { "$ref": "#/$defs/tags" }, "status": { "$ref": "#/$defs/status" }
      }
    },
    "location": {
      "type": "object", "additionalProperties": false, "minProperties": 1,
      "properties": {
        "providerId": { "$ref": "#/$defs/id" }, "name": { "$ref": "#/$defs/shortText" },
        "address": { "$ref": "#/$defs/optionalText" }, "area": { "type": ["string", "null"], "maxLength": 120 },
        "city": { "type": ["string", "null"], "maxLength": 120 }, "state": { "type": ["string", "null"], "maxLength": 120 },
        "countryCode": { "type": "string", "pattern": "^[A-Z]{2}$" },
        "phone": { "type": ["string", "null"], "maxLength": 120 }, "hours": { "$ref": "#/$defs/optionalText" },
        "status": { "$ref": "#/$defs/status" }
      }
    },
    "investigation": {
      "type": "object", "additionalProperties": false, "minProperties": 1,
      "properties": {
        "name": { "$ref": "#/$defs/shortText" }, "aliases": { "$ref": "#/$defs/tags" },
        "description": { "$ref": "#/$defs/optionalText" }, "method": { "type": ["string", "null"], "maxLength": 300 },
        "specimen": { "type": ["string", "null"], "maxLength": 300 }, "status": { "$ref": "#/$defs/status" }
      }
    },
    "deliveryModes": { "type": "array", "maxItems": 10, "uniqueItems": true, "items": { "enum": ["in_person", "online", "home_visit", "home_collection", "delivery", "other"] } },
    "service": {
      "type": "object", "additionalProperties": false, "minProperties": 1,
      "properties": {
        "providerId": { "$ref": "#/$defs/id" }, "name": { "$ref": "#/$defs/shortText" },
        "category": { "$ref": "#/$defs/shortText" }, "description": { "$ref": "#/$defs/optionalText" },
        "aliases": { "$ref": "#/$defs/tags" }, "locationIds": { "$ref": "#/$defs/ids" },
        "deliveryModes": { "$ref": "#/$defs/deliveryModes" }, "investigationIds": { "$ref": "#/$defs/ids" },
        "price": { "$ref": "#/$defs/price" }, "status": { "$ref": "#/$defs/status" }
      }
    },
    "inclusion": {
      "type": "object", "additionalProperties": false, "required": ["name"],
      "properties": { "name": { "$ref": "#/$defs/shortText" }, "investigationId": { "anyOf": [{ "$ref": "#/$defs/id" }, { "type": "null" }] }, "notes": { "$ref": "#/$defs/optionalText" } }
    },
    "package": {
      "type": "object", "additionalProperties": false, "minProperties": 1,
      "properties": {
        "providerId": { "$ref": "#/$defs/id" }, "name": { "$ref": "#/$defs/shortText" },
        "category": { "$ref": "#/$defs/shortText" }, "variant": { "type": ["string", "null"], "maxLength": 120 },
        "description": { "$ref": "#/$defs/optionalText" }, "aliases": { "$ref": "#/$defs/tags" },
        "locationIds": { "$ref": "#/$defs/ids" }, "deliveryModes": { "$ref": "#/$defs/deliveryModes" },
        "inclusions": { "type": "array", "maxItems": 300, "items": { "$ref": "#/$defs/inclusion" } },
        "price": { "$ref": "#/$defs/price" }, "status": { "$ref": "#/$defs/status" }
      }
    },
    "education": {
      "type": "object", "additionalProperties": false, "minProperties": 1,
      "properties": {
        "title": { "$ref": "#/$defs/shortText" }, "body": { "type": "string", "minLength": 1, "maxLength": 30000 },
        "aliases": { "$ref": "#/$defs/tags" }, "investigationIds": { "$ref": "#/$defs/ids" },
        "status": { "$ref": "#/$defs/status" }
      }
    },
    "operation": {
      "type": "object", "additionalProperties": false, "required": ["action", "entity", "id", "data"],
      "properties": {
        "action": { "enum": ["create", "update"] },
        "entity": { "enum": ["provider", "location", "service", "package", "investigation", "education"] },
        "id": { "$ref": "#/$defs/id" }, "ifRevision": { "type": "integer", "minimum": 1 }, "data": { "type": "object" }
      },
      "allOf": [
        { "if": { "properties": { "action": { "const": "update" } }, "required": ["action"] }, "then": { "required": ["ifRevision"] }, "else": { "not": { "required": ["ifRevision"] } } },
        { "if": { "properties": { "entity": { "const": "provider" } }, "required": ["entity"] }, "then": { "properties": { "data": { "$ref": "#/$defs/provider" } }, "allOf": [{ "if": { "properties": { "action": { "const": "create" } } }, "then": { "properties": { "data": { "required": ["name", "categories"] } } } }] } },
        { "if": { "properties": { "entity": { "const": "location" } }, "required": ["entity"] }, "then": { "properties": { "data": { "$ref": "#/$defs/location" } }, "allOf": [{ "if": { "properties": { "action": { "const": "create" } } }, "then": { "properties": { "data": { "required": ["providerId", "name", "countryCode"] } } } }] } },
        { "if": { "properties": { "entity": { "const": "investigation" } }, "required": ["entity"] }, "then": { "properties": { "data": { "$ref": "#/$defs/investigation" } }, "allOf": [{ "if": { "properties": { "action": { "const": "create" } } }, "then": { "properties": { "data": { "required": ["name"] } } } }] } },
        { "if": { "properties": { "entity": { "const": "service" } }, "required": ["entity"] }, "then": { "properties": { "data": { "$ref": "#/$defs/service" } }, "allOf": [{ "if": { "properties": { "action": { "const": "create" } } }, "then": { "properties": { "data": { "required": ["providerId", "name", "category", "deliveryModes"] } } } }] } },
        { "if": { "properties": { "entity": { "const": "package" } }, "required": ["entity"] }, "then": { "properties": { "data": { "$ref": "#/$defs/package" } }, "allOf": [{ "if": { "properties": { "action": { "const": "create" } } }, "then": { "properties": { "data": { "required": ["providerId", "name", "category", "deliveryModes"] } } } }] } },
        { "if": { "properties": { "entity": { "const": "education" } }, "required": ["entity"] }, "then": { "properties": { "data": { "$ref": "#/$defs/education" } }, "allOf": [{ "if": { "properties": { "action": { "const": "create" } } }, "then": { "properties": { "data": { "required": ["title", "body"] } } } }] } }
      ]
    }
  }
}
