{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://bearsunday.github.io/BEAR.QueryRepository/schemas/repository-log.json",
  "title": "BEAR.QueryRepository Log Entry",
  "description": "JSON log entry format for BEAR.QueryRepository cache operations. For conceptual documentation (Donut Cache, Cache Dependencies), see: https://bearsunday.github.io/llms-full.txt and docs/llms-full.txt",
  "type": "object",
  "required": ["op"],
  "properties": {
    "op": {
      "type": "string",
      "description": "Operation name",
      "enum": [
        "request-start",
        "save-value",
        "save-view",
        "save-etag",
        "save-donut",
        "save-donut-view",
        "invalidate-etag",
        "put-query-repository",
        "purge-query-repository",
        "put-donut",
        "try-donut-view",
        "found-donut-view",
        "try-donut",
        "no-donut-found",
        "refresh-donut",
        "cache-hit",
        "cache-miss",
        "depends-on"
      ]
    },
    "uri": {
      "type": "string",
      "description": "Resource URI (e.g., page://self/user, app://self/posts)",
      "pattern": "^(page|app)://self/.+"
    },
    "tags": {
      "type": "array",
      "description": "Cache tags for invalidation",
      "items": {
        "type": "string"
      }
    },
    "surrogateKeys": {
      "type": "array",
      "description": "Surrogate keys for CDN cache invalidation",
      "items": {
        "type": "string"
      }
    },
    "etag": {
      "type": "string",
      "description": "Entity tag for cache validation"
    },
    "ttl": {
      "type": ["integer", "null"],
      "description": "Time to live in seconds",
      "minimum": 0
    },
    "sMaxAge": {
      "type": ["integer", "null"],
      "description": "Shared cache max age in seconds (for CDN)",
      "minimum": 0
    },
    "parent": {
      "type": "string",
      "description": "Parent resource URI that depends on the child",
      "pattern": "^(page|app)://self/.+"
    },
    "child": {
      "type": "string",
      "description": "Child resource URI that the parent depends on",
      "pattern": "^(page|app)://self/.+"
    },
    "childTags": {
      "type": "array",
      "description": "Tags from child resource that enable dependency invalidation",
      "items": {
        "type": "string"
      }
    },
    "method": {
      "type": "string",
      "description": "HTTP method or operation type (used with request-start)",
      "enum": ["get", "post", "put", "patch", "delete", "purge", "invalidate"]
    }
  },
  "allOf": [
    {
      "if": {
        "properties": { "op": { "const": "save-value" } }
      },
      "then": {
        "required": ["uri", "tags", "ttl"]
      }
    },
    {
      "if": {
        "properties": { "op": { "const": "save-view" } }
      },
      "then": {
        "required": ["uri", "ttl"]
      }
    },
    {
      "if": {
        "properties": { "op": { "const": "save-etag" } }
      },
      "then": {
        "required": ["uri", "etag", "surrogateKeys"]
      }
    },
    {
      "if": {
        "properties": { "op": { "const": "save-donut" } }
      },
      "then": {
        "required": ["uri", "sMaxAge"]
      }
    },
    {
      "if": {
        "properties": { "op": { "const": "save-donut-view" } }
      },
      "then": {
        "required": ["uri", "surrogateKeys", "sMaxAge"]
      }
    },
    {
      "if": {
        "properties": { "op": { "const": "invalidate-etag" } }
      },
      "then": {
        "required": ["tags"]
      }
    },
    {
      "if": {
        "properties": { "op": { "const": "put-donut" } }
      },
      "then": {
        "required": ["uri", "ttl"]
      }
    },
    {
      "if": {
        "properties": { "op": { "const": "cache-hit" } }
      },
      "then": {
        "required": ["uri"]
      }
    },
    {
      "if": {
        "properties": { "op": { "const": "cache-miss" } }
      },
      "then": {
        "required": ["uri"]
      }
    },
    {
      "if": {
        "properties": { "op": { "const": "depends-on" } }
      },
      "then": {
        "required": ["parent", "child", "childTags"]
      }
    }
  ],
  "examples": [
    {
      "op": "request-start",
      "uri": "page://self/user"
    },
    {
      "op": "request-start",
      "uri": "page://self/user",
      "method": "purge"
    },
    {
      "op": "save-value",
      "uri": "page://self/user",
      "tags": ["etag123", "_user_"],
      "ttl": 3600
    },
    {
      "op": "save-etag",
      "uri": "page://self/user",
      "etag": "etag123",
      "surrogateKeys": ["_user_", "user-tag"]
    },
    {
      "op": "invalidate-etag",
      "tags": ["_user_"]
    },
    {
      "op": "put-query-repository",
      "uri": "app://self/posts"
    },
    {
      "op": "try-donut-view",
      "uri": "page://self/blog"
    },
    {
      "op": "put-donut",
      "uri": "page://self/blog",
      "ttl": 600,
      "sMaxAge": 3600
    },
    {
      "op": "cache-hit",
      "uri": "page://self/user"
    },
    {
      "op": "cache-miss",
      "uri": "page://self/user"
    },
    {
      "op": "depends-on",
      "parent": "page://self/blog",
      "child": "app://self/comment",
      "childTags": ["_comment_id=1"]
    }
  ]
}
