{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://postflight.co/aeronautiql/planning/v1/results/planning-snapshot.schema.json",
  "title": "Shared planning immutable snapshot v1",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion",
    "snapshotId",
    "documentId",
    "commitId",
    "generation",
    "publishedAt",
    "planState",
    "publication",
    "engineResults",
    "legs",
    "tripMetrics",
    "issues"
  ],
  "properties": {
    "schemaVersion": {
      "const": "1.0"
    },
    "snapshotId": {
      "$ref": "common.schema.json#/$defs/id"
    },
    "documentId": {
      "$ref": "common.schema.json#/$defs/intentId"
    },
    "commitId": {
      "$ref": "common.schema.json#/$defs/intentId"
    },
    "generation": {
      "type": "integer",
      "minimum": 0
    },
    "publishedAt": {
      "$ref": "common.schema.json#/$defs/utc"
    },
    "planState": {
      "enum": [
        "empty",
        "incomplete",
        "complete"
      ],
      "description": "Derived: empty has no legs; complete has every leg resolved and evaluated with complete evaluated totals; anything else is incomplete."
    },
    "publication": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "atomic",
        "binding"
      ],
      "properties": {
        "atomic": {
          "const": true
        },
        "binding": {
          "const": "snapshotId"
        }
      }
    },
    "engineResults": {
      "type": "array",
      "maxItems": 2000,
      "items": {
        "$ref": "engine-result.schema.json"
      }
    },
    "legs": {
      "type": "array",
      "minItems": 0,
      "maxItems": 1000,
      "items": {
        "$ref": "#/$defs/leg"
      }
    },
    "tripMetrics": {
      "$ref": "#/$defs/tripMetrics"
    },
    "issues": {
      "type": "array",
      "maxItems": 10000,
      "items": {
        "$ref": "common.schema.json#/$defs/issue"
      }
    },
    "extensions": {
      "$ref": "common.schema.json#/$defs/extensions"
    }
  },
  "$defs": {
    "leg": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "legId",
        "ordinal",
        "dependencyKey",
        "resolution",
        "evaluation",
        "vertical",
        "metrics"
      ],
      "properties": {
        "legId": {
          "$ref": "common.schema.json#/$defs/intentId"
        },
        "ordinal": {
          "type": "integer",
          "minimum": 0
        },
        "dependencyKey": {
          "$ref": "common.schema.json#/$defs/dependencyKey"
        },
        "resolution": {
          "$ref": "#/$defs/componentBinding"
        },
        "evaluation": {
          "$ref": "#/$defs/evaluationBinding"
        },
        "vertical": {
          "$ref": "#/$defs/verticalBinding"
        },
        "metrics": {
          "$ref": "#/$defs/legMetrics"
        }
      }
    },
    "componentBinding": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "state"
      ],
      "properties": {
        "state": {
          "enum": [
            "pending",
            "usable",
            "unusable"
          ]
        },
        "resultId": {
          "$ref": "common.schema.json#/$defs/id"
        },
        "routeIdentity": {
          "$ref": "common.schema.json#/$defs/id"
        },
        "mappingId": {
          "$ref": "common.schema.json#/$defs/mappingId"
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "state": {
                "const": "usable"
              }
            }
          },
          "then": {
            "required": [
              "resultId",
              "routeIdentity"
            ]
          }
        }
      ]
    },
    "evaluationBinding": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "state"
      ],
      "properties": {
        "state": {
          "enum": [
            "pending",
            "usable",
            "stale",
            "unavailable"
          ]
        },
        "resultId": {
          "$ref": "common.schema.json#/$defs/id"
        },
        "trajectoryIdentity": {
          "$ref": "common.schema.json#/$defs/id"
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "state": {
                "const": "usable"
              }
            }
          },
          "then": {
            "required": [
              "resultId",
              "trajectoryIdentity"
            ]
          }
        }
      ]
    },
    "verticalBinding": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "state"
      ],
      "properties": {
        "state": {
          "enum": [
            "pending",
            "evaluated",
            "estimated",
            "unavailable"
          ]
        },
        "basis": {
          "$ref": "common.schema.json#/$defs/verticalBasis"
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "state": {
                "enum": [
                  "evaluated",
                  "estimated"
                ]
              }
            }
          },
          "then": {
            "required": [
              "basis"
            ]
          }
        }
      ]
    },
    "legMetrics": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "time",
        "fuel"
      ],
      "properties": {
        "time": {
          "$ref": "#/$defs/timeMetric"
        },
        "fuel": {
          "$ref": "#/$defs/fuelMetric"
        }
      }
    },
    "timeMetric": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "state"
      ],
      "properties": {
        "state": {
          "enum": [
            "pending",
            "evaluated",
            "estimated",
            "unavailable"
          ]
        },
        "seconds": {
          "type": "number",
          "minimum": 0
        },
        "sourceResultId": {
          "$ref": "common.schema.json#/$defs/id"
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "state": {
                "enum": [
                  "evaluated",
                  "estimated"
                ]
              }
            }
          },
          "then": {
            "required": [
              "seconds"
            ]
          }
        },
        {
          "if": {
            "properties": {
              "state": {
                "const": "evaluated"
              }
            }
          },
          "then": {
            "required": [
              "sourceResultId"
            ]
          }
        }
      ]
    },
    "fuelMetric": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "state"
      ],
      "properties": {
        "state": {
          "enum": [
            "pending",
            "evaluated",
            "unavailable"
          ]
        },
        "quantity": {
          "$ref": "common.schema.json#/$defs/fuelQuantity"
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "state": {
                "const": "evaluated"
              }
            }
          },
          "then": {
            "required": [
              "quantity"
            ]
          }
        }
      ]
    },
    "tripMetrics": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "time",
        "fuel"
      ],
      "properties": {
        "time": {
          "$ref": "#/$defs/aggregateTime"
        },
        "fuel": {
          "$ref": "#/$defs/aggregateFuel"
        }
      }
    },
    "aggregateTime": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "state",
        "includedLegIds",
        "missingLegIds"
      ],
      "properties": {
        "state": {
          "enum": [
            "complete",
            "partial",
            "pending",
            "unavailable"
          ]
        },
        "seconds": {
          "type": "number",
          "minimum": 0
        },
        "includedLegIds": {
          "type": "array",
          "uniqueItems": true,
          "items": {
            "$ref": "common.schema.json#/$defs/id"
          }
        },
        "missingLegIds": {
          "type": "array",
          "uniqueItems": true,
          "items": {
            "$ref": "common.schema.json#/$defs/id"
          }
        },
        "provenance": {
          "enum": [
            "evaluated",
            "estimated"
          ]
        }
      },
      "dependentRequired": {
        "seconds": [
          "provenance"
        ],
        "provenance": [
          "seconds"
        ]
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "state": {
                "enum": [
                  "complete",
                  "partial"
                ]
              }
            }
          },
          "then": {
            "required": [
              "seconds"
            ]
          }
        },
        {
          "if": {
            "properties": {
              "state": {
                "enum": [
                  "pending",
                  "unavailable"
                ]
              }
            }
          },
          "then": {
            "not": {
              "required": [
                "seconds"
              ]
            },
            "properties": {
              "includedLegIds": {
                "maxItems": 0
              }
            }
          }
        }
      ]
    },
    "aggregateFuel": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "state",
        "includedLegIds",
        "missingLegIds"
      ],
      "properties": {
        "state": {
          "enum": [
            "complete",
            "partial",
            "pending",
            "unavailable"
          ]
        },
        "quantity": {
          "$ref": "common.schema.json#/$defs/aggregateQuantity"
        },
        "includedLegIds": {
          "type": "array",
          "uniqueItems": true,
          "items": {
            "$ref": "common.schema.json#/$defs/id"
          }
        },
        "missingLegIds": {
          "type": "array",
          "uniqueItems": true,
          "items": {
            "$ref": "common.schema.json#/$defs/id"
          }
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "state": {
                "enum": [
                  "complete",
                  "partial"
                ]
              }
            }
          },
          "then": {
            "required": [
              "quantity"
            ]
          }
        },
        {
          "if": {
            "properties": {
              "state": {
                "enum": [
                  "pending",
                  "unavailable"
                ]
              }
            }
          },
          "then": {
            "not": {
              "required": [
                "quantity"
              ]
            },
            "properties": {
              "includedLegIds": {
                "maxItems": 0
              }
            }
          }
        }
      ]
    }
  }
}
