{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://stac.linz.govt.nz/v0.0.15/historical-imagery/schema.json",
  "title": "Historical Imagery Extension",
  "description": "STAC Historical Imagery Extension for STAC Items and STAC Collections.",
  "allOf": [
    {
      "$ref": "#/definitions/stac_extensions"
    },
    {
      "$ref": "#/definitions/fields"
    },
    {
      "if": {
        "type": "object",
        "properties": {
          "type": {
            "const": "Feature"
          }
        }
      },
      "then": {
        "$comment": "This is the schema for STAC Items.",
        "allOf": [
          {
            "type": "object",
            "required": ["type", "properties", "assets"],
            "properties": {
              "properties": {
                "type": "object",
                "$comment": "Require fields here for Item Properties.",
                "required": ["platform", "mission"]
              },
              "assets": {
                "type": "object",
                "$comment": "Require fields here for Item Asset Properties.",
                "additionalProperties": {
                  "type": "object",
                  "required": ["eo:bands"]
                }
              }
            }
          }
        ]
      },
      "else": {
        "if": {
          "type": "object",
          "properties": {
            "type": {
              "const": "Collection"
            }
          }
        },
        "then": {
          "$comment": "This is the schema for STAC Collections. Require fields here for Collections (top-level).",
          "allOf": [
            {
              "type": "object",
              "required": ["type", "title", "providers", "summaries"]
            }
          ]
        },
        "else": false
      }
    }
  ],
  "definitions": {
    "stac_extensions": {
      "type": "object",
      "required": ["stac_extensions"],
      "properties": {
        "stac_extensions": {
          "type": "array",
          "allOf": [
            {
              "contains": {
                "const": "https://stac.linz.govt.nz/v0.0.15/historical-imagery/schema.json"
              }
            },
            {
              "contains": {
                "const": "https://stac.linz.govt.nz/v0.0.15/linz/schema.json"
              }
            },
            {
              "contains": {
                "const": "https://stac-extensions.github.io/eo/v1.0.0/schema.json"
              }
            },
            {
              "contains": {
                "const": "https://stac-extensions.github.io/projection/v1.0.0/schema.json"
              }
            },
            {
              "contains": {
                "const": "https://stac-extensions.github.io/file/v2.0.0/schema.json"
              }
            },
            {
              "contains": {
                "const": "https://stac.linz.govt.nz/v0.0.15/aerial-photo/schema.json"
              }
            },
            {
              "contains": {
                "const": "https://stac.linz.govt.nz/v0.0.15/camera/schema.json"
              }
            },
            {
              "contains": {
                "const": "https://stac.linz.govt.nz/v0.0.15/film/schema.json"
              }
            },
            {
              "contains": {
                "const": "https://stac.linz.govt.nz/v0.0.15/scanning/schema.json"
              }
            }
          ]
        }
      }
    },
    "fields": {
      "type": "object",
      "$comment": "Remove licensor and producer when validation against linz STAC extension is included.",
      "properties": {
        "providers": {
          "allOf": [
            {
              "type": "array",
              "contains": {
                "type": "object",
                "properties": {
                  "roles": {
                    "type": "array",
                    "contains": {
                      "const": "licensor"
                    }
                  }
                }
              }
            },
            {
              "type": "array",
              "contains": {
                "type": "object",
                "properties": {
                  "roles": {
                    "type": "array",
                    "contains": {
                      "const": "producer"
                    }
                  }
                }
              }
            },
            {
              "type": "array",
              "contains": {
                "type": "object",
                "properties": {
                  "roles": {
                    "type": "array",
                    "contains": {
                      "const": "processor"
                    }
                  }
                }
              }
            },
            {
              "type": "array",
              "contains": {
                "type": "object",
                "properties": {
                  "roles": {
                    "type": "array",
                    "contains": {
                      "const": "host"
                    }
                  }
                }
              }
            }
          ]
        },
        "summaries": {
          "type": "object",
          "required": ["platform", "mission", "proj:epsg"],
          "properties": {
            "platform": {
              "type": "array",
              "items": {
                "type": "string"
              },
              "minItems": 1
            },
            "mission": {
              "type": "array",
              "items": {
                "type": "string"
              },
              "minItems": 1
            },
            "instruments": {
              "type": "array",
              "items": {
                "type": "string"
              },
              "minItems": 1
            },
            "proj:epsg": {
              "type": "array",
              "items": {
                "type": ["integer", "null"]
              }
            }
          }
        }
      }
    }
  }
}
