{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://standards.rootblocks.com/schemas/cache/cache-refresh-decision.v1.schema.json",
  "title": "RootBlocks Cache Refresh Decision v1",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion",
    "decisionId",
    "decidedAt",
    "operation",
    "online",
    "enforcementMode",
    "cacheState",
    "cacheEntry",
    "offlinePolicy",
    "compatibility",
    "result",
    "reasonCodes",
    "requiredActions"
  ],
  "properties": {
    "schemaVersion": {
      "const": "rootblocks.cache-refresh-decision/v1"
    },
    "decisionId": {
      "type": "string",
      "pattern": "^cache-decision-[a-z0-9][a-z0-9.-]{2,127}$"
    },
    "decidedAt": {
      "type": "string",
      "format": "date-time"
    },
    "operation": {
      "$ref": "#/$defs/operation"
    },
    "online": {
      "type": "boolean"
    },
    "enforcementMode": {
      "enum": ["advisory", "enforced"]
    },
    "cacheState": {
      "$ref": "#/$defs/cacheState"
    },
    "cacheEntry": {
      "$ref": "#/$defs/bundleRef"
    },
    "offlinePolicy": {
      "type": "object",
      "additionalProperties": false,
      "required": ["allowed", "reason"],
      "properties": {
        "allowed": {
          "type": "boolean"
        },
        "reason": {
          "enum": ["online", "not-needed", "within-window", "expired", "forced-refresh", "state-blocked"]
        }
      }
    },
    "compatibility": {
      "type": "object",
      "additionalProperties": false,
      "required": ["result", "decisionPath"],
      "properties": {
        "result": {
          "enum": ["allow", "warn", "block"]
        },
        "decisionPath": {
          "$ref": "#/$defs/path"
        }
      }
    },
    "result": {
      "enum": ["use-cache", "refresh-advisory", "refresh-required", "block", "quarantine", "wait-for-lock"]
    },
    "reasonCodes": {
      "type": "array",
      "minItems": 1,
      "items": {
        "enum": [
          "CACHE_CURRENT",
          "CACHE_STALE_WARNING",
          "CACHE_STALE_CRITICAL",
          "CACHE_INVALID",
          "CACHE_QUARANTINED",
          "CACHE_REFRESHING",
          "OFFLINE_ALLOWED",
          "OFFLINE_EXPIRED",
          "COMPATIBILITY_WARN",
          "COMPATIBILITY_BLOCK",
          "SIGNATURE_INVALID",
          "ASSET_HASH_MISMATCH",
          "REMOTE_CHECK_REQUIRED",
          "LOCK_HELD"
        ]
      },
      "uniqueItems": true
    },
    "requiredActions": {
      "type": "array",
      "minItems": 1,
      "items": {
        "enum": [
          "verify-cache",
          "use-cache",
          "check-remote",
          "download-temp",
          "verify-temp",
          "acquire-lock",
          "atomic-replace",
          "emit-cache-metadata",
          "restore-last-valid",
          "quarantine-current",
          "block-operation",
          "wait-for-lock"
        ]
      },
      "uniqueItems": true
    }
  },
  "allOf": [
    {
      "if": {
        "properties": { "result": { "const": "use-cache" } },
        "required": ["result"]
      },
      "then": {
        "properties": {
          "requiredActions": { "contains": { "const": "use-cache" } }
        }
      }
    },
    {
      "if": {
        "properties": { "result": { "const": "refresh-required" } },
        "required": ["result"]
      },
      "then": {
        "properties": {
          "requiredActions": {
            "allOf": [
              { "contains": { "const": "check-remote" } },
              { "contains": { "const": "download-temp" } },
              { "contains": { "const": "verify-temp" } },
              { "contains": { "const": "acquire-lock" } },
              { "contains": { "const": "atomic-replace" } },
              { "contains": { "const": "emit-cache-metadata" } }
            ]
          }
        }
      }
    },
    {
      "if": {
        "properties": { "result": { "const": "quarantine" } },
        "required": ["result"]
      },
      "then": {
        "properties": {
          "requiredActions": {
            "allOf": [
              { "contains": { "const": "quarantine-current" } },
              { "contains": { "const": "block-operation" } }
            ]
          }
        }
      }
    },
    {
      "if": {
        "properties": { "result": { "const": "block" } },
        "required": ["result"]
      },
      "then": {
        "properties": {
          "requiredActions": { "contains": { "const": "block-operation" } }
        }
      }
    },
    {
      "if": {
        "properties": {
          "compatibility": {
            "properties": { "result": { "const": "block" } },
            "required": ["result"]
          }
        },
        "required": ["compatibility"]
      },
      "then": {
        "properties": {
          "result": { "const": "block" }
        }
      }
    }
  ],
  "$defs": {
    "id": {
      "type": "string",
      "pattern": "^[a-z0-9][a-z0-9.-]{2,127}$"
    },
    "semver": {
      "type": "string",
      "pattern": "^(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(-[A-Za-z0-9.-]+)?(\\+[A-Za-z0-9.-]+)?$"
    },
    "path": {
      "type": "string",
      "pattern": "^[A-Za-z0-9_./@-]+$"
    },
    "operation": {
      "enum": ["generate", "validate", "compile", "run", "debug", "package", "sign", "release"]
    },
    "cacheState": {
      "enum": ["current", "stale-warning", "stale-critical", "invalid", "quarantined", "refreshing"]
    },
    "bundleRef": {
      "type": "object",
      "additionalProperties": false,
      "required": ["bundleId", "bundleKind", "version", "manifestHash"],
      "properties": {
        "bundleId": { "$ref": "#/$defs/id" },
        "bundleKind": {
          "enum": [
            "rootblocks-global-standard",
            "organization-component-extension",
            "project-component-extension",
            "personal-component-extension"
          ]
        },
        "version": { "$ref": "#/$defs/semver" },
        "manifestHash": {
          "type": "string",
          "pattern": "^[a-f0-9]{64,128}$"
        }
      }
    }
  }
}
