{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://postflight.co/aeronautiql/planning/v1_1/results/engine-result.schema.json",
  "title": "Shared planning engine result v1.1",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion",
    "resultId",
    "jobId",
    "documentId",
    "commitId",
    "legId",
    "submittedAt",
    "completedAt",
    "origin",
    "dependencyKey",
    "resolution",
    "evaluation",
    "displayInputs",
    "executionClock"
  ],
  "properties": {
    "schemaVersion": {
      "const": "1.1"
    },
    "resultId": {
      "$ref": "https://postflight.co/aeronautiql/planning/v1/results/engine-result.schema.json#/properties/resultId"
    },
    "jobId": {
      "$ref": "https://postflight.co/aeronautiql/planning/v1/results/engine-result.schema.json#/properties/jobId"
    },
    "documentId": {
      "$ref": "https://postflight.co/aeronautiql/planning/v1/results/engine-result.schema.json#/properties/documentId"
    },
    "commitId": {
      "$ref": "https://postflight.co/aeronautiql/planning/v1/results/engine-result.schema.json#/properties/commitId"
    },
    "legId": {
      "$ref": "https://postflight.co/aeronautiql/planning/v1/results/engine-result.schema.json#/properties/legId"
    },
    "submittedAt": {
      "$ref": "https://postflight.co/aeronautiql/planning/v1/results/engine-result.schema.json#/properties/submittedAt"
    },
    "completedAt": {
      "$ref": "https://postflight.co/aeronautiql/planning/v1/results/engine-result.schema.json#/properties/completedAt"
    },
    "origin": {
      "$ref": "https://postflight.co/aeronautiql/planning/v1/results/engine-result.schema.json#/properties/origin"
    },
    "dependencyKey": {
      "allOf": [
        {
          "$ref": "https://postflight.co/aeronautiql/planning/v1/results/engine-result.schema.json#/properties/dependencyKey"
        },
        {
          "properties": {
            "policyRevision": {
              "const": "planning-v1.1"
            },
            "additionalDataRevisions": {
              "required": [
                "legInputs",
                "displayInputs",
                "executionClock",
                "nativeDepartureUtc",
                "nativeAircraftId",
                "performanceMode"
              ],
              "properties": {
                "legInputs": {
                  "$ref": "https://postflight.co/aeronautiql/planning/v1/results/common.schema.json#/$defs/mappingId"
                },
                "displayInputs": {
                  "$ref": "https://postflight.co/aeronautiql/planning/v1/results/common.schema.json#/$defs/mappingId"
                },
                "executionClock": {
                  "$ref": "https://postflight.co/aeronautiql/planning/v1/results/common.schema.json#/$defs/mappingId"
                },
                "nativeDepartureUtc": {
                  "type": "string",
                  "format": "date-time"
                },
                "nativeAircraftId": {
                  "type": "string",
                  "minLength": 1,
                  "maxLength": 256
                },
                "performanceMode": {
                  "enum": [
                    "manualProfile",
                    "tables"
                  ]
                },
                "tableWitness": {
                  "$ref": "https://postflight.co/aeronautiql/planning/v1/results/common.schema.json#/$defs/mappingId"
                },
                "tableAlgorithm": {
                  "type": "string",
                  "minLength": 1,
                  "maxLength": 256
                },
                "rangeEstimate": {
                  "const": "table-range-estimate-v1"
                }
              },
              "allOf": [
                {
                  "if": {
                    "properties": {
                      "performanceMode": {
                        "const": "tables"
                      }
                    }
                  },
                  "then": {
                    "required": [
                      "tableWitness",
                      "tableAlgorithm"
                    ]
                  }
                }
              ]
            }
          },
          "required": [
            "additionalDataRevisions"
          ]
        }
      ]
    },
    "resolution": {
      "$ref": "#/$defs/resolution"
    },
    "evaluation": {
      "$ref": "#/$defs/evaluation"
    },
    "extensions": {
      "$ref": "https://postflight.co/aeronautiql/planning/v1/results/engine-result.schema.json#/properties/extensions"
    },
    "cacheReuse": {
      "$ref": "cache-reuse.schema.json"
    },
    "displayInputs": {
      "$ref": "display-inputs.schema.json"
    },
    "executionClock": {
      "$ref": "execution-clock.schema.json"
    }
  },
  "$defs": {
    "resolution": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "state",
        "presentationEligibility",
        "mappingState",
        "issues"
      ],
      "properties": {
        "state": {
          "$ref": "https://postflight.co/aeronautiql/planning/v1/results/engine-result.schema.json#/$defs/resolution/properties/state"
        },
        "presentationEligibility": {
          "$ref": "https://postflight.co/aeronautiql/planning/v1/results/engine-result.schema.json#/$defs/resolution/properties/presentationEligibility"
        },
        "mappingState": {
          "$ref": "https://postflight.co/aeronautiql/planning/v1/results/engine-result.schema.json#/$defs/resolution/properties/mappingState"
        },
        "routeIdentity": {
          "$ref": "https://postflight.co/aeronautiql/planning/v1/results/engine-result.schema.json#/$defs/resolution/properties/routeIdentity"
        },
        "resolutionMappingId": {
          "$ref": "https://postflight.co/aeronautiql/planning/v1/results/engine-result.schema.json#/$defs/resolution/properties/mappingId"
        },
        "payload": {
          "$ref": "https://postflight.co/aeronautiql/planning/v1/results/engine-result.schema.json#/$defs/resolution/properties/payload"
        },
        "elementMappings": {
          "$ref": "https://postflight.co/aeronautiql/planning/v1/results/engine-result.schema.json#/$defs/resolution/properties/elementMappings"
        },
        "issues": {
          "$ref": "https://postflight.co/aeronautiql/planning/v1/results/engine-result.schema.json#/$defs/resolution/properties/issues"
        },
        "terminalOwnership": {
          "$ref": "terminal-ownership.schema.json"
        },
        "terminalInputs": {
          "$ref": "terminal-ownership.schema.json#/$defs/inputs"
        },
        "terminalGeometryInputsDigest": {
          "$ref": "https://postflight.co/aeronautiql/planning/v1/results/common.schema.json#/$defs/mappingId"
        }
      },
      "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": [
              "resolutionMappingId",
              "elementMappings"
            ],
            "properties": {
              "state": {
                "const": "usable"
              },
              "elementMappings": {
                "minItems": 1
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "mappingState": {
                "const": "complete"
              }
            },
            "required": [
              "mappingState"
            ]
          },
          "then": {
            "required": [
              "terminalOwnership",
              "terminalInputs",
              "terminalGeometryInputsDigest"
            ]
          }
        },
        {
          "if": {
            "properties": {
              "state": {
                "const": "usable"
              }
            },
            "required": [
              "state"
            ]
          },
          "then": {
            "required": [
              "terminalInputs",
              "terminalGeometryInputsDigest"
            ]
          }
        }
      ]
    },
    "evaluation": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "state",
        "presentationEligibility",
        "issues"
      ],
      "properties": {
        "state": {
          "enum": [
            "pending",
            "usable",
            "stale",
            "unavailable"
          ]
        },
        "presentationEligibility": {
          "enum": [
            "none",
            "metrics"
          ]
        },
        "trajectoryIdentity": {
          "$ref": "https://postflight.co/aeronautiql/planning/v1/results/common.schema.json#/$defs/id"
        },
        "payload": {
          "$ref": "https://postflight.co/aeronautiql/planning/v1/results/trajectory-result-v1.schema.json"
        },
        "issues": {
          "type": "array",
          "maxItems": 10000,
          "items": {
            "$ref": "https://postflight.co/aeronautiql/planning/v1/results/common.schema.json#/$defs/issue"
          }
        },
        "tableReplay": {
          "$ref": "table-replay.schema.json"
        },
        "terminalPressure": {
          "$ref": "terminal-pressure.schema.json"
        },
        "altitudeSelection": {
          "$ref": "altitude-selection.schema.json"
        },
        "rangeEstimate": {
          "$ref": "range-estimate.schema.json"
        }
      },
      "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"
              }
            }
          }
        }
      ]
    }
  }
}
