{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Fault Code Event",
  "type": "object",
  "properties": {
    "connectionId": {
      "type": "string",
      "description": "The ID of the connection that this record belongs to."
    },
    "id": {
      "title": "FaultCodeEventId",
      "type": "string",
      "format": "ulid"
    },
    "provider": {
      "type": "string",
      "description": "Every provider has a unique code to identify it across Terminal's system. You can find each provider's code under [provider details](/providers)."
    },
    "vehicle": {
      "description": "The vehicle associated with the fault.",
      "title": "VehicleId",
      "type": "string",
      "format": "ulid",
      "pattern": "^vcl_[0-9A-HJKMNP-TV-Z]{26}$"
    },
    "protocol": {
      "type": "string",
      "enum": ["j1939", "obdii"],
      "description": "The diagnostic protocol used"
    },
    "status": {
      "type": "string",
      "enum": ["open", "pending", "closed"],
      "description": "The status of the fault code at the time of the event. Some providers do not report closed statuses."
    },
    "code": {
      "type": "string",
      "description": "The fault code identifier. For OBD-II systems: alphanumeric DTC codes (P0087, U0046, C0123, B1234). For J1939 systems: SPN-FMI format (SPN3031-FMI9) or derived codes from SPN/FMI combinations."
    },
    "description": {
      "type": "string",
      "description": "Human-readable description of the fault"
    },
    "observedAt": {
      "description": "The time the fault code was observed",
      "title": "ISODateTime",
      "type": "string",
      "format": "date-time"
    },
    "milStatus": {
      "type": "string",
      "enum": ["on", "off"],
      "description": "The Malfunction Indicator Lamp (MIL) status of the vehicle at the time the fault code was observed"
    },
    "obdii": {
      "type": "object",
      "description": "OBD-II Diagnostic Trouble Code information for light/medium duty vehicles. Contains standardized alphanumeric codes like P0087, U0046, C0123, B1234.",
      "properties": {
        "code": {
          "type": "string",
          "description": "Alphanumeric DTC code (e.g., P0087, U0046)"
        },
        "description": {
          "type": "string",
          "description": "Human-readable description of the DTC"
        }
      },
      "required": ["code"]
    },
    "j1939": {
      "type": "object",
      "description": "J1939 fault code information for heavy-duty vehicles. FMI (Failure Mode Identifier) indicates the specific nature of the fault (0-31 numeric codes).",
      "properties": {
        "fmi": {
          "type": "integer",
          "description": "Failure Mode Identifier (FMI) - numeric code (0-31) indicating the specific nature of the fault"
        },
        "fmiDescription": {
          "type": "string",
          "description": "Human-readable description of the FMI failure mode"
        },
        "spn": {
          "type": "integer",
          "description": "Suspect Parameter Number (SPN) - identifies the specific parameter/system affected"
        },
        "spnDescription": {
          "type": "string",
          "description": "Human-readable description of the SPN parameter/system"
        },
        "occurrenceCount": {
          "type": "integer",
          "description": "The occurrence count reported by the provider at the time the fault code was observed"
        },
        "sourceAddress": {
          "type": "integer",
          "description": "The source address of the fault code"
        }
      },
      "required": ["spn"]
    },
    "metadata": {
      "title": "EntityMetadata",
      "type": "object",
      "description": "Internal metadata about the record.",
      "required": ["addedAt", "modifiedAt"],
      "properties": {
        "addedAt": {
          "title": "AddedAt",
          "type": "string",
          "format": "date-time",
          "description": "The date and time the record was ingested into Terminal. Note: this is not the date and time the record was created in the provider's system."
        },
        "modifiedAt": {
          "title": "ModifiedAt",
          "type": "string",
          "format": "date-time",
          "description": "The date and time the record was last updated in Terminal. Note: this is not the date and time the record was updated in the provider's system."
        }
      }
    },
    "raw": {
      "title": "RawDataList",
      "type": "array",
      "items": {
        "title": "RawData",
        "type": "object",
        "properties": {
          "provider": {
            "type": "string"
          },
          "schema": {
            "type": "string"
          },
          "extractedAt": {
            "type": "string"
          },
          "data": {
            "type": "object"
          }
        },
        "required": ["provider", "schema", "extractedAt", "data"]
      }
    }
  },
  "discriminator": {
    "propertyName": "protocol"
  },
  "oneOf": [
    {
      "title": "FaultCodeEventObdii",
      "properties": {
        "protocol": {
          "const": "obdii"
        }
      },
      "required": ["protocol", "obdii"]
    },
    {
      "title": "FaultCodeEventJ1939",
      "properties": {
        "protocol": {
          "const": "j1939"
        }
      },
      "required": ["protocol", "j1939"]
    }
  ],
  "required": [
    "connectionId",
    "id",
    "provider",
    "vehicle",
    "code",
    "protocol",
    "observedAt",
    "status",
    "metadata"
  ]
}
