{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://kaldenframelabs.com/schemas/clock-interpretation-probe-contract-v1.schema.json",
  "title": "KaldenFrame Clock Interpretation Probe Contract v1",
  "type": "object",
  "additionalProperties": false,
  "required": ["contract", "probe_id", "reference", "candidate", "matching", "limits"],
  "properties": {
    "contract": { "const": "kaldenframe.clock-interpretation-probe.v1" },
    "probe_id": { "$ref": "#/$defs/identifier" },
    "reference": { "$ref": "#/$defs/reference" },
    "candidate": { "$ref": "#/$defs/candidate" },
    "matching": { "$ref": "#/$defs/matching" },
    "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
    },
    "reference": {
      "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" }
      }
    },
    "candidate": {
      "type": "object",
      "additionalProperties": false,
      "required": ["dataset_id", "path", "format", "timestamp_column", "entity_columns", "interpretations"],
      "properties": {
        "dataset_id": { "$ref": "#/$defs/identifier" },
        "path": { "$ref": "#/$defs/relativePath" },
        "format": { "$ref": "#/$defs/format" },
        "timestamp_column": { "$ref": "#/$defs/column" },
        "entity_columns": { "$ref": "#/$defs/columns" },
        "interpretations": {
          "type": "array",
          "minItems": 2,
          "maxItems": 64,
          "items": { "$ref": "#/$defs/interpretation" }
        }
      }
    },
    "interpretation": {
      "oneOf": [
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["interpretation_id", "mode"],
          "properties": {
            "interpretation_id": { "$ref": "#/$defs/identifier" },
            "mode": { "const": "EXPLICIT_RFC3339" }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["interpretation_id", "mode", "offset_minutes"],
          "properties": {
            "interpretation_id": { "$ref": "#/$defs/identifier" },
            "mode": { "const": "NAIVE_FIXED_OFFSET" },
            "offset_minutes": { "type": "integer", "minimum": -840, "maximum": 840 }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["interpretation_id", "mode", "zone"],
          "properties": {
            "interpretation_id": { "$ref": "#/$defs/identifier" },
            "mode": { "const": "NAIVE_IANA_ZONE" },
            "zone": { "type": "string", "minLength": 1, "maxLength": 128 }
          }
        }
      ]
    },
    "matching": {
      "type": "object",
      "additionalProperties": false,
      "required": ["direction", "tolerance_seconds", "minimum_coverage_basis_points", "minimum_gap_basis_points"],
      "properties": {
        "direction": { "const": "BACKWARD" },
        "tolerance_seconds": { "type": "integer", "minimum": 0, "maximum": 31536000 },
        "minimum_coverage_basis_points": { "type": "integer", "minimum": 0, "maximum": 10000 },
        "minimum_gap_basis_points": { "type": "integer", "minimum": 1, "maximum": 10000 }
      }
    },
    "limits": {
      "type": "object",
      "additionalProperties": false,
      "required": ["max_file_bytes", "max_rows_per_dataset", "max_interpretations"],
      "properties": {
        "max_file_bytes": { "type": "integer", "minimum": 1, "maximum": 1000000000 },
        "max_rows_per_dataset": { "type": "integer", "minimum": 1, "maximum": 10000000 },
        "max_interpretations": { "type": "integer", "minimum": 1, "maximum": 64 }
      }
    }
  }
}
