{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://postflight.co/aeronautiql/planning/v1/results/engine-result.schema.json",
  "title": "Shared planning engine result v1",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion",
    "resultId",
    "jobId",
    "documentId",
    "commitId",
    "legId",
    "submittedAt",
    "completedAt",
    "origin",
    "dependencyKey",
    "resolution",
    "evaluation"
  ],
  "properties": {
    "schemaVersion": {
      "const": "1.0"
    },
    "resultId": {
      "$ref": "common.schema.json#/$defs/id"
    },
    "jobId": {
      "$ref": "common.schema.json#/$defs/id"
    },
    "documentId": {
      "$ref": "common.schema.json#/$defs/intentId"
    },
    "commitId": {
      "$ref": "common.schema.json#/$defs/intentId"
    },
    "legId": {
      "$ref": "common.schema.json#/$defs/intentId"
    },
    "submittedAt": {
      "$ref": "common.schema.json#/$defs/utc"
    },
    "completedAt": {
      "$ref": "common.schema.json#/$defs/utc"
    },
    "origin": {
      "$ref": "common.schema.json#/$defs/resultOrigin"
    },
    "dependencyKey": {
      "$ref": "common.schema.json#/$defs/dependencyKey"
    },
    "resolution": {
      "$ref": "#/$defs/resolution"
    },
    "evaluation": {
      "$ref": "#/$defs/evaluation"
    },
    "extensions": {
      "$ref": "common.schema.json#/$defs/extensions"
    }
  },
  "$defs": {
    "resolution": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "state",
        "presentationEligibility",
        "mappingState",
        "issues"
      ],
      "properties": {
        "state": {
          "enum": [
            "pending",
            "usable",
            "unusable"
          ]
        },
        "presentationEligibility": {
          "enum": [
            "none",
            "readOnly",
            "editable"
          ]
        },
        "mappingState": {
          "enum": [
            "complete",
            "unavailableLegacy",
            "notApplicable"
          ]
        },
        "routeIdentity": {
          "$ref": "common.schema.json#/$defs/id"
        },
        "mappingId": {
          "$ref": "common.schema.json#/$defs/mappingId"
        },
        "payload": {
          "$ref": "resolved-route-v1.schema.json"
        },
        "elementMappings": {
          "type": "array",
          "maxItems": 20000,
          "items": {
            "$ref": "#/$defs/elementMapping"
          }
        },
        "issues": {
          "type": "array",
          "maxItems": 10000,
          "items": {
            "$ref": "common.schema.json#/$defs/issue"
          }
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "state": {
                "const": "usable"
              }
            }
          },
          "then": {
            "required": [
              "routeIdentity",
              "payload"
            ]
          }
        },
        {
          "if": {
            "properties": {
              "state": {
                "const": "pending"
              }
            }
          },
          "then": {
            "properties": {
              "presentationEligibility": {
                "const": "none"
              },
              "mappingState": {
                "const": "notApplicable"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "state": {
                "const": "unusable"
              }
            }
          },
          "then": {
            "properties": {
              "presentationEligibility": {
                "const": "none"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "presentationEligibility": {
                "const": "editable"
              }
            }
          },
          "then": {
            "required": [
              "elementMappings"
            ],
            "properties": {
              "state": {
                "const": "usable"
              },
              "mappingState": {
                "const": "complete"
              },
              "elementMappings": {
                "minItems": 1
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "mappingState": {
                "const": "unavailableLegacy"
              }
            }
          },
          "then": {
            "properties": {
              "presentationEligibility": {
                "const": "readOnly"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "mappingState": {
                "const": "complete"
              }
            }
          },
          "then": {
            "required": [
              "mappingId",
              "elementMappings"
            ],
            "properties": {
              "state": {
                "const": "usable"
              },
              "elementMappings": {
                "minItems": 1
              }
            }
          }
        }
      ]
    },
    "elementMapping": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "elementId",
        "elementKind",
        "firstPointIndex",
        "lastPointIndex",
        "edgeIds"
      ],
      "properties": {
        "elementId": {
          "$ref": "common.schema.json#/$defs/intentId"
        },
        "elementKind": {
          "$ref": "common.schema.json#/$defs/elementKind"
        },
        "firstPointIndex": {
          "type": "integer",
          "minimum": 0
        },
        "lastPointIndex": {
          "type": "integer",
          "minimum": 0
        },
        "edgeIds": {
          "type": "array",
          "uniqueItems": true,
          "items": {
            "$ref": "common.schema.json#/$defs/id"
          }
        }
      },
      "description": "Listed in intent element order; spans are inclusive native point indices and never decrease."
    },
    "evaluation": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "state",
        "presentationEligibility",
        "issues"
      ],
      "properties": {
        "state": {
          "enum": [
            "pending",
            "usable",
            "stale",
            "unavailable"
          ]
        },
        "presentationEligibility": {
          "enum": [
            "none",
            "metrics"
          ]
        },
        "trajectoryIdentity": {
          "$ref": "common.schema.json#/$defs/id"
        },
        "payload": {
          "$ref": "trajectory-result-v1.schema.json"
        },
        "issues": {
          "type": "array",
          "maxItems": 10000,
          "items": {
            "$ref": "common.schema.json#/$defs/issue"
          }
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "state": {
                "const": "usable"
              }
            }
          },
          "then": {
            "required": [
              "trajectoryIdentity",
              "payload"
            ],
            "properties": {
              "presentationEligibility": {
                "const": "metrics"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "state": {
                "enum": [
                  "pending",
                  "stale",
                  "unavailable"
                ]
              }
            }
          },
          "then": {
            "properties": {
              "presentationEligibility": {
                "const": "none"
              }
            }
          }
        }
      ]
    }
  }
}
