{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://kaldenframelabs.com/schemas/temporal-boundary-audit-contract-v1.schema.json",
  "title": "KaldenFrame Temporal Boundary Audit Contract v1",
  "type": "object",
  "additionalProperties": false,
  "required": ["contract", "audit_id", "decision", "contexts", "limits"],
  "properties": {
    "contract": { "const": "kaldenframe.temporal-boundary-audit.v1" },
    "audit_id": { "type": "string", "pattern": "^[a-z][a-z0-9_-]{0,63}$" },
    "decision": { "$ref": "#/$defs/decision" },
    "contexts": {
      "type": "array",
      "minItems": 1,
      "items": { "$ref": "#/$defs/context" }
    },
    "limits": { "$ref": "#/$defs/limits" }
  },
  "$defs": {
    "identifier": { "type": "string", "pattern": "^[a-z][a-z0-9_-]{0,63}$" },
    "relativePath": {
      "type": "string",
      "minLength": 1,
      "not": { "pattern": "(^[A-Za-z]:)|(^[/\\\\])|(^|[/\\\\])\\.\\.($|[/\\\\])" }
    },
    "format": { "enum": ["csv", "jsonl"] },
    "column": { "type": "string", "minLength": 1 },
    "columns": {
      "type": "array",
      "items": { "$ref": "#/$defs/column" },
      "uniqueItems": true
    },
    "decision": {
      "type": "object",
      "additionalProperties": false,
      "required": ["dataset_id", "path", "format", "timestamp_column", "entity_columns"],
      "properties": {
        "dataset_id": { "$ref": "#/$defs/identifier" },
        "path": { "$ref": "#/$defs/relativePath" },
        "format": { "$ref": "#/$defs/format" },
        "timestamp_column": { "$ref": "#/$defs/column" },
        "entity_columns": { "$ref": "#/$defs/columns" },
        "expected_interval_seconds": { "type": "integer", "minimum": 1, "maximum": 31536000 },
        "grid_anchor": { "type": "string", "format": "date-time" }
      }
    },
    "context": {
      "type": "object",
      "additionalProperties": false,
      "required": ["context_id", "path", "format", "event_time_column", "available_time_column", "entity_columns", "max_age_seconds"],
      "properties": {
        "context_id": { "$ref": "#/$defs/identifier" },
        "path": { "$ref": "#/$defs/relativePath" },
        "format": { "$ref": "#/$defs/format" },
        "event_time_column": { "$ref": "#/$defs/column" },
        "available_time_column": { "$ref": "#/$defs/column" },
        "period_end_column": { "$ref": "#/$defs/column" },
        "entity_columns": { "$ref": "#/$defs/columns" },
        "expected_interval_seconds": { "type": "integer", "minimum": 1, "maximum": 31536000 },
        "grid_anchor": { "type": "string", "format": "date-time" },
        "max_age_seconds": { "type": "integer", "minimum": 1, "maximum": 31536000 }
      }
    },
    "limits": {
      "type": "object",
      "additionalProperties": false,
      "required": ["max_file_bytes", "max_rows_per_dataset"],
      "properties": {
        "max_file_bytes": { "type": "integer", "minimum": 1, "maximum": 1000000000 },
        "max_rows_per_dataset": { "type": "integer", "minimum": 1, "maximum": 10000000 }
      }
    }
  }
}

