{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://postflight.co/aeronautiql/planning/v1_1/results/altitude-selection.schema.json",
  "title": "Native automatic altitude selection proof",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "policyRevision",
    "search",
    "evaluated",
    "outcome",
    "initialTerminalPressure",
    "work"
  ],
  "properties": {
    "policyRevision": {
      "const": "automatic-altitude-v1"
    },
    "search": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "schema_version",
        "floor_altitude",
        "ceiling_altitude",
        "altitude_step_ft",
        "course_deg",
        "hemispheric",
        "minimum_cruise_seconds",
        "preference_tolerance_seconds",
        "atmosphere"
      ],
      "properties": {
        "schema_version": {
          "const": 1
        },
        "floor_altitude": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "value_ft",
            "reference"
          ],
          "properties": {
            "value_ft": {
              "type": "number"
            },
            "reference": {
              "const": "pressure"
            }
          }
        },
        "ceiling_altitude": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "value_ft",
            "reference"
          ],
          "properties": {
            "value_ft": {
              "type": "number"
            },
            "reference": {
              "const": "pressure"
            }
          }
        },
        "altitude_step_ft": {
          "type": "number",
          "exclusiveMinimum": 0
        },
        "course_deg": {
          "type": "number"
        },
        "hemispheric": {
          "enum": [
            "ifr",
            "vfr",
            "none"
          ]
        },
        "minimum_cruise_seconds": {
          "type": "number",
          "minimum": 0
        },
        "preference_tolerance_seconds": {
          "type": "number",
          "minimum": 0
        },
        "atmosphere": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "qnh_hpa",
            "temperature_model",
            "source",
            "revision"
          ],
          "properties": {
            "qnh_hpa": {
              "type": "number",
              "minimum": 800,
              "maximum": 1100
            },
            "temperature_model": {
              "const": "isa"
            },
            "source": {
              "type": "string",
              "minLength": 1
            },
            "revision": {
              "type": "string",
              "minLength": 1
            }
          }
        }
      }
    },
    "evaluated": {
      "type": "array",
      "minItems": 1,
      "maxItems": 10000,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "pressure_altitude_ft",
          "feasible"
        ],
        "properties": {
          "pressure_altitude_ft": {
            "type": "number"
          },
          "feasible": {
            "type": "boolean"
          },
          "cruise_seconds": {
            "type": "number",
            "minimum": 0
          },
          "rejection": {
            "type": "string",
            "minLength": 1
          },
          "rejection_category": {
            "enum": [
              "aircraft_limit",
              "distance",
              "wind_progress",
              "usable_fuel",
              "source_condition"
            ]
          },
          "limited_calculation_weight_lb": {
            "type": "number",
            "exclusiveMinimum": 0
          }
        }
      }
    },
    "outcome": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "schema_version",
        "status",
        "selection_complete",
        "selection",
        "evaluated_count",
        "candidate_count"
      ],
      "properties": {
        "schema_version": {
          "const": 1
        },
        "status": {
          "enum": [
            "selected",
            "no_feasible_candidate"
          ]
        },
        "selection_complete": {
          "const": true
        },
        "selection": {
          "oneOf": [
            {
              "type": "object",
              "additionalProperties": false,
              "required": [
                "schema_version",
                "altitude",
                "pressure_altitude_ft",
                "cruise_seconds",
                "preference_met",
                "hemispheric_relaxed"
              ],
              "properties": {
                "schema_version": {
                  "const": 1
                },
                "altitude": {
                  "type": "object",
                  "additionalProperties": false,
                  "required": [
                    "value_ft",
                    "reference"
                  ],
                  "properties": {
                    "value_ft": {
                      "type": "number"
                    },
                    "reference": {
                      "const": "pressure"
                    }
                  }
                },
                "pressure_altitude_ft": {
                  "type": "number"
                },
                "cruise_seconds": {
                  "type": "number",
                  "minimum": 0
                },
                "preference_met": {
                  "type": "boolean"
                },
                "hemispheric_relaxed": {
                  "type": "boolean"
                }
              }
            },
            {
              "type": "null"
            }
          ]
        },
        "evaluated_count": {
          "type": "integer",
          "minimum": 0
        },
        "candidate_count": {
          "type": "integer",
          "minimum": 0
        }
      }
    },
    "initialTerminalPressure": {
      "oneOf": [
        {
          "type": "null"
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "departure",
            "arrival"
          ],
          "properties": {
            "departure": {
              "$ref": "terminal-pressure.schema.json#/$defs/endpoint"
            },
            "arrival": {
              "$ref": "terminal-pressure.schema.json#/$defs/endpoint"
            }
          }
        }
      ]
    },
    "work": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "forecastQueries",
        "steps",
        "queryLimit"
      ],
      "properties": {
        "forecastQueries": {
          "type": "integer",
          "minimum": 0,
          "maximum": 100000
        },
        "steps": {
          "type": "integer",
          "minimum": 0
        },
        "queryLimit": {
          "const": 100000
        }
      }
    },
    "limitBoundaries": {
      "type": "array",
      "minItems": 1,
      "maxItems": 10000,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["pressureAltitudeFt", "boundary"],
        "properties": {
          "pressureAltitudeFt": { "type": "number" },
          "boundary": {
            "enum": [
              "reserve_requirement_exceeded",
              "usable_fuel_exhausted",
              "minimum_cruise_weight"
            ]
          }
        }
      }
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "outcome": {
            "properties": {
              "status": { "const": "no_feasible_candidate" }
            }
          }
        }
      },
      "then": {
        "required": ["limitBoundaries"],
        "properties": {
          "outcome": {
            "properties": {
              "selection": { "type": "null" }
            }
          }
        }
      },
      "else": {
        "properties": {
          "outcome": {
            "properties": {
              "selection": { "type": "object" }
            }
          }
        }
      }
    }
  ]
}
