{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://www.firstmilelabs.com/schemas/webhooks/data.corrected.v1.json",
  "title": "FirstMileLabs Outbound Webhook — data.corrected (v1)",
  "description": "Emitted when a customer corrects extracted data during review, or when an analyst manually overrides an errored document (approve / escalate). The payload shape varies by source — see the oneOf branches.",
  "type": "object",
  "additionalProperties": true,
  "required": [
    "event",
    "eventId",
    "eventVersion",
    "timestamp",
    "orgId",
    "caseId",
    "source"
  ],
  "properties": {
    "event": { "type": "string", "const": "data.corrected" },
    "eventId": { "type": "string", "format": "uuid" },
    "eventVersion": { "type": "integer", "const": 1 },
    "timestamp": { "type": "string", "format": "date-time" },
    "orgId": { "type": "string" },
    "crmRecordId": { "type": ["string", "null"] },
    "caseId": { "type": "string" },
    "source": { "type": "string", "enum": ["customer_review", "analyst_override"] },
    "amendmentCount": { "type": "integer", "minimum": 0, "description": "customer_review only — number of fields the customer changed." },
    "amendments": {
      "type": "array",
      "description": "customer_review only — array of per-field amendments.",
      "items": {
        "type": "object",
        "additionalProperties": true,
        "required": ["section", "field"],
        "properties": {
          "section": { "type": "string" },
          "field": { "type": "string" },
          "originalValue": {},
          "correctedValue": {}
        }
      }
    },
    "documentId": { "type": "integer", "description": "analyst_override only — the document that was overridden." },
    "field": { "type": "string", "description": "analyst_override only — always \"document_status\"." },
    "originalValue": { "type": ["string", "null"], "description": "analyst_override only — typically \"error\"." },
    "correctedValue": { "type": "string", "enum": ["approved", "escalated"], "description": "analyst_override only." },
    "note": { "type": "string", "description": "analyst_override only — manual approval note." },
    "reason": { "type": "string", "description": "analyst_override only — escalation reason." },
    "by": { "type": "string", "description": "analyst_override only — analyst email." }
  },
  "oneOf": [
    {
      "title": "customer_review",
      "properties": {
        "source": { "const": "customer_review" }
      },
      "required": ["source", "amendmentCount", "amendments"]
    },
    {
      "title": "analyst_override",
      "properties": {
        "source": { "const": "analyst_override" }
      },
      "required": ["source", "documentId", "field", "originalValue", "correctedValue", "by"]
    }
  ]
}
