{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://flockline.com/learn/evidence/data/evidence-graph.schema.json",
  "title": "Flockline Evidence Graph",
  "type": "object",
  "required": ["metadata", "nodes", "edges"],
  "properties": {
    "metadata": {
      "type": "object",
      "required": [
        "graphId",
        "title",
        "version",
        "status",
        "canonicalUrl",
        "publishedAt",
        "lastUpdatedAt",
        "description",
        "license"
      ],
      "properties": {
        "graphId": { "type": "string", "minLength": 1 },
        "title": { "type": "string", "minLength": 1 },
        "version": { "type": "string", "pattern": "^\\d+\\.\\d+\\.\\d+$" },
        "status": { "enum": ["foundation", "operational"] },
        "canonicalUrl": { "type": "string", "format": "uri" },
        "publishedAt": { "type": "string", "format": "date-time" },
        "lastUpdatedAt": { "type": "string", "format": "date-time" },
        "description": { "type": "string", "minLength": 1 },
        "license": { "type": "string", "minLength": 1 }
      },
      "additionalProperties": false
    },
    "nodes": {
      "type": "array",
      "items": { "$ref": "#/$defs/node" }
    },
    "edges": {
      "type": "array",
      "items": { "$ref": "#/$defs/edge" }
    }
  },
  "additionalProperties": false,
  "$defs": {
    "topic": {
      "enum": [
        "disease",
        "nutrition",
        "behaviour",
        "welfare",
        "biosecurity",
        "reproduction",
        "housing"
      ]
    },
    "publicationState": {
      "enum": ["draft", "published", "retired", "disabled"]
    },
    "quality": {
      "enum": ["not_assessed", "very_low", "low", "moderate", "high"]
    },
    "baseNodeProperties": {
      "type": "object",
      "properties": {
        "id": { "type": "string", "pattern": "^[a-z][a-z0-9.-]+$" },
        "nodeType": {
          "enum": [
            "article",
            "topic",
            "study",
            "reviewer",
            "evidence",
            "methodology",
            "benchmark",
            "evaluation",
            "correction",
            "version",
            "community_pattern",
            "product_insight"
          ]
        },
        "title": { "type": "string", "minLength": 1 },
        "slug": { "type": "string", "pattern": "^[a-z0-9]+(?:-[a-z0-9]+)*$" },
        "version": { "type": "string", "pattern": "^\\d+\\.\\d+\\.\\d+$" },
        "status": { "$ref": "#/$defs/publicationState" },
        "publishedAt": {
          "oneOf": [
            { "type": "string", "format": "date-time" },
            { "type": "null" }
          ]
        },
        "updatedAt": { "type": "string", "format": "date-time" }
      },
      "required": [
        "id",
        "nodeType",
        "title",
        "slug",
        "version",
        "status",
        "publishedAt",
        "updatedAt"
      ]
    },
    "node": {
      "allOf": [
        { "$ref": "#/$defs/baseNodeProperties" },
        {
          "if": {
            "properties": { "nodeType": { "const": "article" } },
            "required": ["nodeType"]
          },
          "then": {
            "required": [
              "contentUrl",
              "topics",
              "topicNodeIds",
              "sourceNodeIds",
              "evidenceNodeIds",
              "reviewerNodeIds",
              "lastReviewedAt",
              "reviewStatus"
            ],
            "properties": {
              "contentUrl": { "type": "string", "format": "uri" },
              "topics": {
                "type": "array",
                "items": { "$ref": "#/$defs/topic" },
                "uniqueItems": true
              },
              "topicNodeIds": {
                "type": "array",
                "items": { "type": "string" },
                "uniqueItems": true
              },
              "sourceNodeIds": {
                "type": "array",
                "items": { "type": "string" },
                "uniqueItems": true
              },
              "evidenceNodeIds": {
                "type": "array",
                "items": { "type": "string" },
                "uniqueItems": true
              },
              "reviewerNodeIds": {
                "type": "array",
                "items": { "type": "string" },
                "uniqueItems": true
              },
              "lastReviewedAt": {
                "oneOf": [
                  { "type": "string", "format": "date-time" },
                  { "type": "null" }
                ]
              },
              "reviewStatus": {
                "enum": ["unreviewed", "in_review", "reviewed", "review_due"]
              }
            }
          }
        },
        {
          "if": {
            "properties": { "nodeType": { "const": "topic" } },
            "required": ["nodeType"]
          },
          "then": {
            "required": [
              "category",
              "description",
              "knownSummary",
              "unknownSummary",
              "evidenceStrength",
              "flocklineConfidence"
            ],
            "properties": {
              "category": { "$ref": "#/$defs/topic" },
              "description": { "type": "string" },
              "knownSummary": { "type": "string" },
              "unknownSummary": { "type": "string" },
              "evidenceStrength": { "$ref": "#/$defs/quality" },
              "flocklineConfidence": {
                "enum": ["not_assessed", "low", "moderate", "high"]
              }
            }
          }
        },
        {
          "if": {
            "properties": { "nodeType": { "const": "study" } },
            "required": ["nodeType"]
          },
          "then": {
            "required": [
              "sourceClass",
              "topics",
              "authors",
              "publication",
              "year",
              "studyType",
              "species",
              "sampleSize",
              "evidenceQuality",
              "evidenceQualityNote",
              "summary",
              "limitations",
              "doi",
              "pubmedUrl",
              "externalUrl",
              "fullTextUrl"
            ],
            "properties": {
              "sourceClass": {
                "enum": [
                  "peer_reviewed",
                  "veterinary_textbook",
                  "extension_service",
                  "government_guidance",
                  "veterinary_review"
                ]
              },
              "topics": {
                "type": "array",
                "minItems": 1,
                "uniqueItems": true,
                "items": { "$ref": "#/$defs/topic" }
              },
              "authors": {
                "type": "array",
                "minItems": 1,
                "items": { "type": "string", "minLength": 1 }
              },
              "publication": { "type": "string", "minLength": 1 },
              "year": {
                "oneOf": [
                  { "type": "integer", "minimum": 1800, "maximum": 2100 },
                  { "type": "null" }
                ]
              },
              "studyType": { "type": "string", "minLength": 1 },
              "species": {
                "type": "array",
                "minItems": 1,
                "items": { "type": "string", "minLength": 1 }
              },
              "sampleSize": {
                "type": "object",
                "required": ["value", "unit", "note"],
                "properties": {
                  "value": {
                    "oneOf": [
                      { "type": "integer", "minimum": 0 },
                      { "type": "null" }
                    ]
                  },
                  "unit": { "type": "string", "minLength": 1 },
                  "note": {
                    "oneOf": [
                      { "type": "string" },
                      { "type": "null" }
                    ]
                  }
                },
                "additionalProperties": false
              },
              "evidenceQuality": { "$ref": "#/$defs/quality" },
              "evidenceQualityNote": { "type": "string", "minLength": 1 },
              "summary": { "type": "string", "minLength": 1 },
              "limitations": { "type": "string", "minLength": 1 },
              "doi": {
                "oneOf": [{ "type": "string" }, { "type": "null" }]
              },
              "pubmedUrl": {
                "oneOf": [
                  { "type": "string", "format": "uri" },
                  { "type": "null" }
                ]
              },
              "externalUrl": { "type": "string", "format": "uri" },
              "fullTextUrl": {
                "oneOf": [
                  { "type": "string", "format": "uri" },
                  { "type": "null" }
                ]
              }
            }
          }
        },
        {
          "if": {
            "properties": { "nodeType": { "const": "reviewer" } },
            "required": ["nodeType"]
          },
          "then": {
            "required": [
              "displayName",
              "qualifications",
              "expertise",
              "verifiedAt",
              "pagesReviewed",
              "lastReviewDate",
              "profileUrl"
            ]
          }
        },
        {
          "if": {
            "properties": { "nodeType": { "const": "methodology" } },
            "required": ["nodeType"]
          },
          "then": {
            "required": [
              "summary",
              "principles",
              "reviewCadence",
              "retirementRule"
            ]
          }
        },
        {
          "if": {
            "properties": { "nodeType": { "const": "benchmark" } },
            "required": ["nodeType"]
          },
          "then": {
            "required": [
              "targetScenarioCount",
              "publishedScenarioCount",
              "dimensions",
              "scoringProtocol",
              "evaluatorPolicy",
              "resultStatus"
            ]
          }
        },
        {
          "if": {
            "properties": { "nodeType": { "const": "evaluation" } },
            "required": ["nodeType"]
          },
          "then": {
            "required": [
              "benchmarkId",
              "configurationId",
              "runDate",
              "scenarioCount",
              "metrics",
              "knownLimitations"
            ]
          }
        },
        {
          "if": {
            "properties": { "nodeType": { "const": "correction" } },
            "required": ["nodeType"]
          },
          "then": {
            "required": [
              "targetNodeId",
              "correctionType",
              "previousText",
              "correctedText",
              "reason",
              "correctedAt"
            ]
          }
        },
        {
          "if": {
            "properties": { "nodeType": { "const": "version" } },
            "required": ["nodeType"]
          },
          "then": {
            "required": [
              "targetNodeId",
              "changeType",
              "summary",
              "changedAt"
            ]
          }
        },
        {
          "if": {
            "properties": { "nodeType": { "const": "community_pattern" } },
            "required": ["nodeType"]
          },
          "then": {
            "required": [
              "topics",
              "observation",
              "minimumIndependentReports",
              "observedReportCount",
              "collectionWindow",
              "medicalEvidence",
              "privacyReviewed"
            ],
            "properties": {
              "medicalEvidence": { "const": false }
            }
          }
        },
        {
          "if": {
            "properties": { "nodeType": { "const": "product_insight" } },
            "required": ["nodeType"]
          },
          "then": {
            "required": [
              "topics",
              "metric",
              "cohortSize",
              "minimumCohortThreshold",
              "privacyApproved",
              "suppressionApplied"
            ]
          }
        }
      ]
    },
    "edge": {
      "type": "object",
      "required": [
        "id",
        "type",
        "from",
        "to",
        "rationale",
        "status",
        "createdAt"
      ],
      "properties": {
        "id": { "type": "string", "pattern": "^edge\\.[a-z0-9.-]+$" },
        "type": {
          "enum": [
            "supports",
            "contradicts",
            "qualifies",
            "reviews",
            "cites",
            "related_to",
            "supersedes",
            "evaluates",
            "derived_from"
          ]
        },
        "from": { "type": "string", "minLength": 1 },
        "to": { "type": "string", "minLength": 1 },
        "rationale": { "type": "string", "minLength": 1 },
        "status": { "$ref": "#/$defs/publicationState" },
        "createdAt": { "type": "string", "format": "date-time" }
      },
      "additionalProperties": false
    }
  }
}
