{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:odexa:storage-sessions-v1:draft3",
  "title": "Additive storage_sessions_v1 client assertion event",
  "$comment": "Structural validation only. Exact write/parent resolution, time arithmetic, authentication, accepted terms and lifecycle require semantic validation. No baseline contract is upgraded.",
  "$ref": "#/$defs/event",
  "$defs": {
    "event": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "protocol_version": {
          "const": "1.2.0-draft.3"
        },
        "origin": {
          "type": "string",
          "minLength": 1,
          "maxLength": 4096,
          "pattern": "^https://[^?#]+$"
        },
        "service_id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 4096,
          "pattern": "^https://[^?#]+$"
        },
        "issuer": {
          "type": "string",
          "minLength": 1,
          "maxLength": 4096,
          "pattern": "^https://[^?#]+$"
        },
        "delegation_id": {
          "anyOf": [
            {
              "type": "string",
              "minLength": 1,
              "maxLength": 4096,
              "pattern": "^https://[^?#]+$"
            },
            {
              "type": "null"
            }
          ]
        },
        "event_id": {
          "type": "string",
          "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
        },
        "event_type": {
          "const": "use.reported"
        },
        "occurred_at": {
          "type": "string",
          "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$"
        },
        "reporter_id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 4096,
          "pattern": "^https://[^?#]+$"
        },
        "source": {
          "const": "client_reported"
        },
        "resource_url": {
          "type": "string",
          "minLength": 1,
          "maxLength": 4096,
          "pattern": "^https://[^#]+$"
        },
        "policy_id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 4096,
          "pattern": "^https://[^?#]+$"
        },
        "policy_revision": {
          "type": "integer",
          "minimum": 1,
          "maximum": 9007199254740991
        },
        "actions": {
          "type": "array",
          "items": {
            "enum": [
              "retrieve",
              "store",
              "transform",
              "redistribute"
            ]
          },
          "minItems": 1,
          "maxItems": 1,
          "uniqueItems": true
        },
        "purposes": {
          "type": "array",
          "items": {
            "enum": [
              "search_indexing",
              "public_retrieval",
              "internal_knowledge",
              "model_training",
              "model_fine_tuning",
              "redistribution"
            ]
          },
          "minItems": 1,
          "maxItems": 6,
          "uniqueItems": true
        },
        "agreement_id": {
          "anyOf": [
            {
              "type": "string",
              "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
            },
            {
              "type": "null"
            }
          ]
        },
        "asset_ref": {
          "anyOf": [
            {
              "$ref": "#/$defs/asset_ref"
            },
            {
              "type": "null"
            }
          ]
        },
        "operation": {
          "$ref": "#/$defs/operation"
        },
        "http": {
          "type": "null"
        },
        "quantity": {
          "type": "null"
        },
        "unit": {
          "type": "null"
        },
        "related_events": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/related_event"
          },
          "minItems": 0,
          "maxItems": 64,
          "uniqueItems": true
        },
        "derived_from": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/asset_ref"
          },
          "minItems": 0,
          "maxItems": 64,
          "uniqueItems": true
        },
        "event_profile": {
          "const": "storage_sessions_v1"
        }
      },
      "required": [
        "protocol_version",
        "origin",
        "service_id",
        "issuer",
        "delegation_id",
        "event_id",
        "event_type",
        "occurred_at",
        "reporter_id",
        "source",
        "resource_url",
        "policy_id",
        "policy_revision",
        "actions",
        "purposes",
        "agreement_id",
        "asset_ref",
        "operation",
        "http",
        "quantity",
        "unit",
        "related_events",
        "derived_from",
        "event_profile"
      ],
      "allOf": [
        {
          "if": {
            "properties": {
              "actions": {
                "const": [
                  "store"
                ]
              }
            },
            "type": "object"
          },
          "then": {
            "properties": {
              "operation": {
                "properties": {
                  "copy_context": {
                    "$ref": "#/$defs/copy_context"
                  }
                },
                "type": "object"
              }
            },
            "type": "object"
          },
          "else": {
            "properties": {
              "operation": {
                "properties": {
                  "copy_context": {
                    "type": "null"
                  },
                  "kind": {
                    "const": "action"
                  }
                },
                "type": "object"
              }
            },
            "type": "object"
          }
        },
        {
          "if": {
            "properties": {
              "operation": {
                "properties": {
                  "kind": {
                    "const": "storage_session"
                  }
                },
                "type": "object"
              }
            },
            "type": "object"
          },
          "then": {
            "properties": {
              "actions": {
                "const": [
                  "store"
                ]
              },
              "related_events": {
                "minItems": 1,
                "maxItems": 1,
                "type": "array"
              },
              "derived_from": {
                "maxItems": 0,
                "type": "array"
              }
            },
            "type": "object"
          }
        }
      ]
    },
    "operation": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "id": {
          "type": "string",
          "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
        },
        "started_at": {
          "type": "string",
          "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$"
        },
        "ended_at": {
          "anyOf": [
            {
              "type": "string",
              "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$"
            },
            {
              "type": "null"
            }
          ]
        },
        "state": {
          "enum": [
            "in_progress",
            "completed",
            "failed"
          ]
        },
        "kind": {
          "enum": [
            "action",
            "storage_session"
          ]
        },
        "copy_context": {
          "anyOf": [
            {
              "$ref": "#/$defs/copy_context"
            },
            {
              "type": "null"
            }
          ]
        },
        "storage": {
          "anyOf": [
            {
              "$ref": "#/$defs/storage"
            },
            {
              "type": "null"
            }
          ]
        }
      },
      "required": [
        "id",
        "started_at",
        "ended_at",
        "state",
        "kind",
        "copy_context",
        "storage"
      ],
      "allOf": [
        {
          "if": {
            "properties": {
              "kind": {
                "const": "action"
              }
            },
            "type": "object"
          },
          "then": {
            "properties": {
              "storage": {
                "type": "null"
              }
            },
            "type": "object"
          },
          "else": {
            "properties": {
              "copy_context": {
                "$ref": "#/$defs/copy_context"
              },
              "storage": {
                "$ref": "#/$defs/storage"
              },
              "state": {
                "enum": [
                  "in_progress",
                  "completed"
                ]
              }
            },
            "allOf": [
              {
                "if": {
                  "properties": {
                    "storage": {
                      "properties": {
                        "trigger": {
                          "const": "cessation"
                        }
                      },
                      "type": "object"
                    }
                  },
                  "type": "object"
                },
                "then": {
                  "properties": {
                    "state": {
                      "const": "completed"
                    },
                    "ended_at": {
                      "type": "string",
                      "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$"
                    }
                  },
                  "type": "object"
                },
                "else": {
                  "properties": {
                    "state": {
                      "const": "in_progress"
                    },
                    "ended_at": {
                      "type": "null"
                    }
                  },
                  "type": "object"
                }
              }
            ],
            "type": "object"
          }
        }
      ]
    },
    "copy_context": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "copy_id": {
          "type": "string",
          "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
        },
        "acquired_at": {
          "type": "string",
          "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$"
        },
        "retain_until": {
          "anyOf": [
            {
              "type": "string",
              "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$"
            },
            {
              "type": "null"
            }
          ]
        },
        "parent_write": {
          "anyOf": [
            {
              "$ref": "#/$defs/related_event"
            },
            {
              "type": "null"
            }
          ]
        }
      },
      "required": [
        "copy_id",
        "acquired_at",
        "retain_until",
        "parent_write"
      ]
    },
    "storage": {
      "oneOf": [
        {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "trigger": {
              "const": "start"
            },
            "checkpoint_index": {
              "type": "integer",
              "const": 0
            },
            "scheduled_at": {
              "type": "string",
              "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$"
            }
          },
          "required": [
            "trigger",
            "checkpoint_index",
            "scheduled_at"
          ]
        },
        {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "trigger": {
              "const": "checkpoint"
            },
            "checkpoint_index": {
              "type": "integer",
              "minimum": 1,
              "maximum": 3652059
            },
            "scheduled_at": {
              "type": "string",
              "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$"
            }
          },
          "required": [
            "trigger",
            "checkpoint_index",
            "scheduled_at"
          ]
        },
        {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "trigger": {
              "const": "cessation"
            },
            "checkpoint_index": {
              "type": "null"
            },
            "scheduled_at": {
              "type": "string",
              "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$"
            }
          },
          "required": [
            "trigger",
            "checkpoint_index",
            "scheduled_at"
          ]
        }
      ]
    },
    "related_event": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "reporter_id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 4096,
          "pattern": "^https://[^?#]+$"
        },
        "event_id": {
          "type": "string",
          "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
        },
        "payload_digest": {
          "type": "string",
          "pattern": "^sha256:[0-9a-f]{64}$"
        }
      },
      "required": [
        "reporter_id",
        "event_id",
        "payload_digest"
      ]
    },
    "asset_ref": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "asset_id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 4096,
          "pattern": "^https://[^?#]+$"
        },
        "version_id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 4096,
          "pattern": "^https://[^?#]+$"
        },
        "manifest_digest": {
          "type": "string",
          "pattern": "^sha256:[0-9a-f]{64}$"
        },
        "representation_id": {
          "type": "string",
          "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
        }
      },
      "required": [
        "asset_id",
        "version_id",
        "manifest_digest",
        "representation_id"
      ]
    }
  }
}
