{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://arsenal.thiagoac.com.br/lab/geo-exp-v1.schema.json",
  "title": "Arsenal GEO-EXP v1",
  "description": "Contrato público para pré-registro, execução, análise e replicação de experimentos GEO/AIO.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema_version", "experiment_id", "title", "owner", "status", "hypothesis",
    "design", "baseline", "platforms", "prompt_set", "metrics", "safeguards",
    "schedule", "preregistration", "lifecycle", "result", "evidence", "replication"
  ],
  "properties": {
    "schema_version": {"const": "1.0.0"},
    "experiment_id": {"type": "string", "pattern": "^GEO-EXP-[0-9]{3,}$"},
    "title": {"type": "string", "minLength": 8},
    "owner": {"type": "string", "minLength": 2},
    "status": {
      "enum": ["DRAFT", "PREREGISTERED", "RUNNING", "ANALYZING", "COMPLETED", "INVALIDATED", "REPLICATED"]
    },
    "hypothesis": {
      "type": "object",
      "additionalProperties": false,
      "required": ["statement", "expected_mechanism", "falsification_rule"],
      "properties": {
        "statement": {"type": "string", "minLength": 20},
        "expected_mechanism": {"type": "string", "minLength": 10},
        "falsification_rule": {"type": "string", "minLength": 10}
      }
    },
    "design": {
      "type": "object",
      "additionalProperties": false,
      "required": ["experiment_type", "experimental_unit", "sample", "treatment", "control"],
      "properties": {
        "experiment_type": {"enum": ["DETERMINISTIC", "INSTRUMENT_VALIDATION", "OBSERVATIONAL", "QUASI_EXPERIMENTAL", "CAUSAL"]},
        "experimental_unit": {"type": "string", "minLength": 3},
        "sample": {
          "type": "object",
          "additionalProperties": false,
          "required": ["planned_n", "power_rule", "inclusion", "exclusion"],
          "properties": {
            "planned_n": {"type": ["integer", "null"], "minimum": 1},
            "power_rule": {"type": "string", "minLength": 3},
            "inclusion": {"type": "array", "items": {"type": "string"}},
            "exclusion": {"type": "array", "items": {"type": "string"}}
          }
        },
        "treatment": {
          "type": "object",
          "additionalProperties": false,
          "required": ["description", "surfaces"],
          "properties": {
            "description": {"type": "string", "minLength": 5},
            "surfaces": {"type": "array", "items": {"type": "string"}}
          }
        },
        "control": {
          "type": "object",
          "additionalProperties": false,
          "required": ["type", "description", "surfaces"],
          "properties": {
            "type": {"enum": ["NONE_DETERMINISTIC", "PRE_POST", "PARALLEL", "HOLDOUT", "SYNTHETIC"]},
            "description": {"type": "string", "minLength": 3},
            "surfaces": {"type": "array", "items": {"type": "string"}}
          }
        }
      }
    },
    "baseline": {
      "type": "object",
      "additionalProperties": false,
      "required": ["state", "summary", "period", "snapshot_refs"],
      "properties": {
        "state": {"enum": ["MEASURED", "NOT_MEASURED", "NOT_APPLICABLE", "MISSING"]},
        "summary": {"type": "string", "minLength": 5},
        "period": {"$ref": "#/$defs/period"},
        "snapshot_refs": {"type": "array", "items": {"type": "string"}}
      }
    },
    "platforms": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["provider", "surface", "model", "version"],
        "properties": {
          "provider": {"type": "string"},
          "surface": {"type": "string"},
          "model": {"type": ["string", "null"]},
          "version": {"type": ["string", "null"]}
        }
      }
    },
    "prompt_set": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "version", "sha256", "public"],
      "properties": {
        "id": {"type": "string"},
        "version": {"type": "string"},
        "sha256": {"type": ["string", "null"], "pattern": "^[a-f0-9]{64}$"},
        "public": {"type": "boolean"}
      }
    },
    "metrics": {
      "type": "object",
      "additionalProperties": false,
      "required": ["dictionary_version", "primary", "secondary"],
      "properties": {
        "dictionary_version": {"type": "string"},
        "primary": {"$ref": "#/$defs/metricSelection"},
        "secondary": {"type": "array", "items": {"$ref": "#/$defs/metricSelection"}}
      }
    },
    "safeguards": {
      "type": "object",
      "additionalProperties": false,
      "required": ["risks", "confounders", "stopping_rule"],
      "properties": {
        "risks": {"type": "array", "items": {"type": "string"}},
        "confounders": {"type": "array", "items": {"type": "string"}},
        "stopping_rule": {"type": "string", "minLength": 5}
      }
    },
    "schedule": {
      "type": "object",
      "additionalProperties": false,
      "required": ["planned_start", "planned_end"],
      "properties": {
        "planned_start": {"type": ["string", "null"], "format": "date"},
        "planned_end": {"type": ["string", "null"], "format": "date"}
      }
    },
    "preregistration": {
      "type": "object",
      "additionalProperties": false,
      "required": ["locked_at", "locked_fields_sha256", "source_commit"],
      "properties": {
        "locked_at": {"type": ["string", "null"], "format": "date-time"},
        "locked_fields_sha256": {"type": ["string", "null"], "pattern": "^[a-f0-9]{64}$"},
        "source_commit": {"type": ["string", "null"]}
      }
    },
    "lifecycle": {
      "type": "object",
      "additionalProperties": false,
      "required": ["intervention_started_at", "analysis_started_at", "completed_at", "invalidated_at"],
      "properties": {
        "intervention_started_at": {"type": ["string", "null"], "format": "date-time"},
        "analysis_started_at": {"type": ["string", "null"], "format": "date-time"},
        "completed_at": {"type": ["string", "null"], "format": "date-time"},
        "invalidated_at": {"type": ["string", "null"], "format": "date-time"}
      }
    },
    "result": {
      "oneOf": [
        {"type": "null"},
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["primary_metric", "secondary_metrics", "decision", "uncertainty", "limitations"],
          "properties": {
            "primary_metric": {"$ref": "#/$defs/measurement"},
            "secondary_metrics": {"type": "array", "items": {"$ref": "#/$defs/measurement"}},
            "decision": {"type": "string", "minLength": 3},
            "uncertainty": {"type": "string", "minLength": 3},
            "limitations": {"type": "array", "minItems": 1, "items": {"type": "string"}}
          }
        }
      ]
    },
    "evidence": {"type": "array", "items": {"$ref": "#/$defs/evidence"}},
    "replication": {
      "type": "object",
      "additionalProperties": false,
      "required": ["decision", "rationale", "target_experiment_id"],
      "properties": {
        "decision": {"enum": ["NOT_DECIDED", "REPLICATE", "DO_NOT_REPLICATE", "REPLICATED"]},
        "rationale": {"type": "string"},
        "target_experiment_id": {"type": ["string", "null"], "pattern": "^GEO-EXP-[0-9]{3,}$"}
      }
    }
  },
  "$defs": {
    "period": {
      "type": "object",
      "additionalProperties": false,
      "required": ["start", "end"],
      "properties": {
        "start": {"type": ["string", "null"], "format": "date-time"},
        "end": {"type": ["string", "null"], "format": "date-time"}
      }
    },
    "decisionRule": {
      "type": "object",
      "additionalProperties": false,
      "required": ["type", "operator", "threshold", "power_plan"],
      "properties": {
        "type": {"enum": ["EXACT", "THRESHOLD", "POWER_CALCULATION"]},
        "operator": {"enum": ["EQ", "GTE", "LTE", "NOT_APPLICABLE"]},
        "threshold": {"type": ["number", "null"]},
        "power_plan": {"type": ["string", "null"]}
      }
    },
    "metricSelection": {
      "type": "object",
      "additionalProperties": false,
      "required": ["metric_id", "metric_version", "decision_rule"],
      "properties": {
        "metric_id": {"type": "string"},
        "metric_version": {"type": "string"},
        "decision_rule": {"$ref": "#/$defs/decisionRule"}
      }
    },
    "measurement": {
      "type": "object",
      "additionalProperties": false,
      "required": ["metric_id", "metric_version", "state", "value", "numerator", "denominator", "period", "interval"],
      "properties": {
        "metric_id": {"type": "string"},
        "metric_version": {"type": "string"},
        "state": {"enum": ["MEASURED", "ZERO", "NOT_MEASURED", "NOT_ATTRIBUTABLE", "MISSING"]},
        "value": {"type": ["number", "null"]},
        "numerator": {"type": ["number", "null"]},
        "denominator": {"type": ["number", "null"]},
        "period": {"$ref": "#/$defs/period"},
        "interval": {"type": ["string", "null"]}
      }
    },
    "evidence": {
      "type": "object",
      "additionalProperties": false,
      "required": ["evidence_id", "url", "sha256", "origin", "captured_at", "privacy_class"],
      "properties": {
        "evidence_id": {"type": "string"},
        "url": {"type": "string"},
        "sha256": {"type": "string", "pattern": "^[a-f0-9]{64}$"},
        "origin": {"type": "string", "minLength": 3},
        "captured_at": {"type": "string", "format": "date-time"},
        "privacy_class": {"enum": ["PUBLIC", "AGGREGATE", "PSEUDONYMIZED"]}
      }
    }
  },
  "allOf": [
    {
      "if": {"properties": {"status": {"const": "DRAFT"}}},
      "then": {
        "properties": {
          "result": {"type": "null"},
          "lifecycle": {"properties": {"intervention_started_at": {"type": "null"}}}
        }
      }
    },
    {
      "if": {"properties": {"status": {"enum": ["COMPLETED", "REPLICATED"]}}},
      "then": {"properties": {"result": {"type": "object"}}}
    }
  ]
}
