{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://postflight.co/aeronautiql/planning/v1/results/draw-projection.schema.json",
  "title": "Shared planning draw projection v1",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion",
    "projectionId",
    "snapshotId",
    "documentId",
    "commitId",
    "createdAt",
    "context",
    "legStates",
    "verticalBases",
    "edges",
    "layers"
  ],
  "properties": {
    "schemaVersion": {
      "const": "1.0"
    },
    "projectionId": {
      "$ref": "common.schema.json#/$defs/id"
    },
    "snapshotId": {
      "$ref": "common.schema.json#/$defs/id"
    },
    "documentId": {
      "$ref": "common.schema.json#/$defs/intentId"
    },
    "commitId": {
      "$ref": "common.schema.json#/$defs/intentId"
    },
    "createdAt": {
      "$ref": "common.schema.json#/$defs/utc"
    },
    "context": {
      "$ref": "#/$defs/context"
    },
    "legStates": {
      "type": "array",
      "minItems": 0,
      "items": {
        "$ref": "#/$defs/legState"
      }
    },
    "verticalBases": {
      "type": "array",
      "items": {
        "$ref": "common.schema.json#/$defs/verticalBasis"
      }
    },
    "edges": {
      "type": "array",
      "maxItems": 20000,
      "items": {
        "$ref": "#/$defs/edge"
      }
    },
    "layers": {
      "type": "array",
      "minItems": 0,
      "maxItems": 1000,
      "items": {
        "$ref": "#/$defs/layer"
      }
    },
    "extensions": {
      "$ref": "common.schema.json#/$defs/extensions"
    }
  },
  "$defs": {
    "context": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "slot",
        "slotRevision",
        "mode",
        "navigationRevision"
      ],
      "properties": {
        "slot": {
          "enum": [
            "active",
            "standby",
            "preview"
          ]
        },
        "slotRevision": {
          "$ref": "common.schema.json#/$defs/id"
        },
        "mode": {
          "enum": [
            "fullPlan",
            "directTo"
          ]
        },
        "navigationRevision": {
          "$ref": "common.schema.json#/$defs/id"
        },
        "directTo": {
          "$ref": "#/$defs/directTo"
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "mode": {
                "const": "directTo"
              }
            }
          },
          "then": {
            "required": [
              "directTo"
            ]
          }
        }
      ]
    },
    "directTo": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "targetOccurrenceId",
        "pathEdgeIds",
        "profileSourceResultId"
      ],
      "properties": {
        "targetOccurrenceId": {
          "$ref": "common.schema.json#/$defs/id"
        },
        "pathEdgeIds": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": {
            "$ref": "common.schema.json#/$defs/id"
          }
        },
        "profileSourceResultId": {
          "$ref": "common.schema.json#/$defs/id"
        }
      }
    },
    "legState": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "legId",
        "resolutionState"
      ],
      "properties": {
        "legId": {
          "$ref": "common.schema.json#/$defs/intentId"
        },
        "resolutionState": {
          "enum": [
            "pending",
            "usable",
            "unusable"
          ]
        },
        "routeIdentity": {
          "$ref": "common.schema.json#/$defs/id"
        },
        "mappingId": {
          "$ref": "common.schema.json#/$defs/mappingId"
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "resolutionState": {
                "const": "usable"
              }
            }
          },
          "then": {
            "required": [
              "routeIdentity"
            ]
          }
        }
      ]
    },
    "edge": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "edgeId",
        "legId",
        "fromOccurrenceId",
        "toOccurrenceId",
        "ownerElementIds",
        "editable",
        "routeIdentity"
      ],
      "properties": {
        "edgeId": {
          "$ref": "common.schema.json#/$defs/id"
        },
        "legId": {
          "$ref": "common.schema.json#/$defs/intentId"
        },
        "fromOccurrenceId": {
          "$ref": "common.schema.json#/$defs/id"
        },
        "toOccurrenceId": {
          "$ref": "common.schema.json#/$defs/id"
        },
        "ownerElementIds": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": {
            "$ref": "common.schema.json#/$defs/id"
          }
        },
        "editable": {
          "type": "boolean"
        },
        "routeIdentity": {
          "$ref": "common.schema.json#/$defs/id"
        },
        "fromPointIndex": {
          "type": "integer",
          "minimum": 0
        },
        "toPointIndex": {
          "type": "integer",
          "minimum": 1
        },
        "admission": {
          "$ref": "common.schema.json#/$defs/admission"
        }
      },
      "dependentRequired": {
        "fromPointIndex": [
          "toPointIndex"
        ],
        "toPointIndex": [
          "fromPointIndex"
        ]
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "editable": {
                "const": true
              }
            }
          },
          "then": {
            "required": [
              "fromPointIndex",
              "toPointIndex",
              "admission"
            ],
            "properties": {
              "admission": {
                "properties": {
                  "state": {
                    "const": "editable"
                  }
                },
                "required": [
                  "insert"
                ],
                "not": {
                  "anyOf": [
                    {
                      "required": [
                        "delete"
                      ]
                    },
                    {
                      "required": [
                        "replace"
                      ]
                    }
                  ]
                }
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "editable": {
                "const": false
              }
            }
          },
          "then": {
            "properties": {
              "admission": {
                "properties": {
                  "state": {
                    "const": "readOnly"
                  }
                }
              }
            }
          }
        }
      ]
    },
    "layer": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "layerId",
        "role",
        "sourceSnapshotId",
        "features"
      ],
      "properties": {
        "layerId": {
          "$ref": "common.schema.json#/$defs/id"
        },
        "role": {
          "enum": [
            "route",
            "fixes",
            "vertical",
            "issues",
            "directTo",
            "selection"
          ]
        },
        "sourceSnapshotId": {
          "$ref": "common.schema.json#/$defs/id"
        },
        "features": {
          "type": "array",
          "maxItems": 50000,
          "items": {
            "oneOf": [
              {
                "$ref": "#/$defs/lineFeature"
              },
              {
                "$ref": "#/$defs/fixFeature"
              },
              {
                "$ref": "#/$defs/breakFeature"
              }
            ]
          }
        }
      }
    },
    "lineFeature": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "kind",
        "featureId",
        "legId",
        "edgeIds",
        "coordinates"
      ],
      "properties": {
        "kind": {
          "const": "line"
        },
        "featureId": {
          "$ref": "common.schema.json#/$defs/id"
        },
        "legId": {
          "$ref": "common.schema.json#/$defs/intentId"
        },
        "edgeIds": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "common.schema.json#/$defs/id"
          }
        },
        "coordinates": {
          "type": "array",
          "minItems": 2,
          "maxItems": 20000,
          "items": {
            "$ref": "common.schema.json#/$defs/coordinate"
          }
        },
        "verticalBasisId": {
          "$ref": "common.schema.json#/$defs/id"
        },
        "altitudes": {
          "type": "array",
          "minItems": 2,
          "maxItems": 20000,
          "items": {
            "type": "number"
          }
        }
      },
      "dependentRequired": {
        "altitudes": [
          "verticalBasisId"
        ],
        "verticalBasisId": [
          "altitudes"
        ]
      }
    },
    "fixFeature": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "kind",
        "featureId",
        "legId",
        "occurrenceId",
        "ownerElementIds",
        "position"
      ],
      "properties": {
        "kind": {
          "const": "fix"
        },
        "featureId": {
          "$ref": "common.schema.json#/$defs/id"
        },
        "legId": {
          "$ref": "common.schema.json#/$defs/intentId"
        },
        "occurrenceId": {
          "$ref": "common.schema.json#/$defs/id"
        },
        "ownerElementIds": {
          "type": "array",
          "minItems": 0,
          "uniqueItems": true,
          "items": {
            "$ref": "common.schema.json#/$defs/id"
          }
        },
        "position": {
          "$ref": "common.schema.json#/$defs/coordinate"
        },
        "verticalBasisId": {
          "$ref": "common.schema.json#/$defs/id"
        },
        "altitude": {
          "type": "number"
        },
        "pointIndex": {
          "type": "integer",
          "minimum": 0
        },
        "admission": {
          "$ref": "common.schema.json#/$defs/admission"
        }
      },
      "dependentRequired": {
        "altitude": [
          "verticalBasisId"
        ],
        "verticalBasisId": [
          "altitude"
        ]
      },
      "allOf": [
        {
          "if": {
            "required": [
              "admission"
            ]
          },
          "then": {
            "required": [
              "pointIndex"
            ],
            "properties": {
              "admission": {
                "not": {
                  "required": [
                    "insert"
                  ]
                }
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "admission": {
                "properties": {
                  "state": {
                    "const": "editable"
                  }
                },
                "required": [
                  "state"
                ]
              }
            },
            "required": [
              "admission"
            ]
          },
          "then": {
            "properties": {
              "admission": {
                "required": [
                  "delete",
                  "replace"
                ]
              }
            }
          }
        }
      ]
    },
    "breakFeature": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "kind",
        "featureId",
        "legId",
        "reason"
      ],
      "properties": {
        "kind": {
          "const": "break"
        },
        "featureId": {
          "$ref": "common.schema.json#/$defs/id"
        },
        "legId": {
          "$ref": "common.schema.json#/$defs/intentId"
        },
        "reason": {
          "enum": [
            "pending",
            "unusable",
            "discontinuity",
            "notInNavigationContext"
          ]
        }
      }
    }
  }
}
