setup:
  - skip:
      features: headers
  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      indices.create:
        index: store
        body:
          mappings:
            properties:
              cost:
                type: integer
              time:
                type: date
              kind:
                type: keyword

  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      indices.create:
        index: empty-store
        body:
          mappings:
            properties:
              cost:
                type: integer
              time:
                type: date

  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
        Content-Type: application/json
      bulk:
        index: store
        refresh: true
        body: |
          {"index":{}}
          {"cost":200,"time":1587501233000,"kind":"changed"}
          {"index":{}}
          {"cost":200,"time":1587501243000,"kind":"changed"}
          {"index":{}}
          {"cost":200,"time":1587501253000,"kind":"changed"}
          {"index":{}}
          {"cost":250,"time":1587501263000,"kind":"changed"}
          {"index":{}}
          {"cost":250,"time":1587501273000,"kind":"changed"}
          {"index":{}}
          {"cost":380,"time":1587501283000,"kind":"changed"}
          {"index":{}}
          {"cost":450,"time":1587501293000,"kind":"changed"}
          {"index":{}}
          {"cost":550,"time":1587501303000,"kind":"changed"}
          {"index":{}}
          {"cost":600,"time":1587501313000,"kind":"changed"}
          {"index":{}}
          {"cost":600,"time":1587501313000,"kind":"changed"}
          {"index":{}}
          {"cost":600,"time":1587501323000,"kind":"changed"}
          {"index":{}}
          {"cost":600,"time":1587501333000,"kind":"changed"}
          {"index":{}}
          {"cost":600,"time":1587501343000,"kind":"changed"}
          {"index":{}}
          {"cost":600,"time":1587501353000,"kind":"changed"}
          {"index":{}}
          {"cost":600,"time":1587501363000,"kind":"changed"}
          {"index":{}}
          {"cost":600,"time":1587501373000,"kind":"changed"}
          {"index":{}}
          {"cost":600,"time":1587501383000,"kind":"changed"}
          {"index":{}}
          {"cost":600,"time":1587501393000,"kind":"changed"}
          {"index":{}}
          {"cost":600,"time":1587501403000,"kind":"changed"}
          {"index":{}}
          {"cost":600,"time":1587501413000,"kind":"changed"}
          {"index":{}}
          {"cost":600,"time":1587501423000,"kind":"changed"}
          {"index":{}}
          {"cost":600,"time":1587501433000,"kind":"changed"}
          {"index":{}}
          {"cost":600,"time":1587501443000,"kind":"changed"}
          {"index":{}}
          {"cost":600,"time":1587501453000,"kind":"changed"}
          {"index":{}}
          {"cost":600,"time":1587501233000,"kind":"unchanged"}
          {"index":{}}
          {"cost":600,"time":1587501243000,"kind":"unchanged"}
          {"index":{}}
          {"cost":600,"time":1587501253000,"kind":"unchanged"}
          {"index":{}}
          {"cost":600,"time":1587501263000,"kind":"unchanged"}
          {"index":{}}
          {"cost":600,"time":1587501273000,"kind":"unchanged"}
          {"index":{}}
          {"cost":600,"time":1587501283000,"kind":"unchanged"}
          {"index":{}}
          {"cost":600,"time":1587501293000,"kind":"unchanged"}
          {"index":{}}
          {"cost":600,"time":1587501303000,"kind":"unchanged"}
          {"index":{}}
          {"cost":600,"time":1587501313000,"kind":"unchanged"}
          {"index":{}}
          {"cost":600,"time":1587501313000,"kind":"unchanged"}
          {"index":{}}
          {"cost":600,"time":1587501323000,"kind":"unchanged"}
          {"index":{}}
          {"cost":600,"time":1587501333000,"kind":"unchanged"}
          {"index":{}}
          {"cost":600,"time":1587501343000,"kind":"unchanged"}
          {"index":{}}
          {"cost":600,"time":1587501353000,"kind":"unchanged"}
          {"index":{}}
          {"cost":600,"time":1587501363000,"kind":"unchanged"}
          {"index":{}}
          {"cost":600,"time":1587501373000,"kind":"unchanged"}
          {"index":{}}
          {"cost":600,"time":1587501383000,"kind":"unchanged"}
          {"index":{}}
          {"cost":600,"time":1587501393000,"kind":"unchanged"}
          {"index":{}}
          {"cost":600,"time":1587501403000,"kind":"unchanged"}
          {"index":{}}
          {"cost":600,"time":1587501413000,"kind":"unchanged"}
          {"index":{}}
          {"cost":600,"time":1587501423000,"kind":"unchanged"}
          {"index":{}}
          {"cost":600,"time":1587501433000,"kind":"unchanged"}
          {"index":{}}
          {"cost":600,"time":1587501443000,"kind":"unchanged"}
          {"index":{}}
          {"cost":600,"time":1587501453000,"kind":"unchanged"}

---
"Test change_point agg simple":

  - do:
      search:
        index: store
        size: 0
        body: >
          {
            "aggs": {
              "date": {
                "date_histogram": {
                  "field": "time",
                  "fixed_interval": "1s"
                },
                "aggs": {
                  "min": {
                    "min": {
                      "field": "cost"
                    }
                  }
                }
              },
              "change_point": {
                "change_point": {
                  "buckets_path": "date>min"
                }
              }
            }
          }
  - is_true: aggregations.change_point.type.trend_change
  - is_true: aggregations.change_point.type.trend_change.p_value
  - is_true: aggregations.change_point.type.trend_change.r_value

---
"Test change_point with missing buckets_path":

  - do:
      catch: /Required \[buckets_path\]/
      search:
        index: store
        size: 0
        body: >
          {
            "aggs": {
              "date": {
                "date_histogram": {
                  "field": "time",
                  "fixed_interval": "1s"
                },
                "aggs": {
                  "avg": {
                    "avg": {
                      "field": "cost"
                    }
                  }
                }
              },
              "change_point": {
                "change_point": {
                }
              }
            }
          }

---
"Test change_point with bad buckets_path":

  - do:
      catch: /No aggregation found for path \[foo\]/
      search:
        index: store
        size: 0
        body: >
          {
            "aggs": {
              "date": {
                "date_histogram": {
                  "field": "time",
                  "fixed_interval": "1s"
                },
                "aggs": {
                  "avg": {
                    "avg": {
                      "field": "cost"
                    }
                  }
                }
              },
              "change_point": {
                "change_point": {
                  "buckets_path": "foo"
                }
              }
            }
          }
---
"Test change_point with too few buckets":

  - do:
      search:
        index: empty-store
        size: 0
        body: >
          {
            "aggs": {
              "date": {
                "date_histogram": {
                  "field": "time",
                  "fixed_interval": "1s"
                },
                "aggs": {
                  "min": {
                    "min": {
                      "field": "cost"
                    }
                  }
                }
              },
              "change_point": {
                "change_point": {
                  "buckets_path": "date"
                }
              }
            }
          }
  - is_true: aggregations.change_point.type.indeterminable
---
"Test change_point with terms":
  - do:
      search:
        index: store
        size: 0
        body: >
          {
            "aggs": {
              "terms": {
                 "terms": {
                   "field": "kind"
                 },
                 "aggs": {
                    "date": {
                      "date_histogram": {
                        "field": "time",
                        "fixed_interval": "1s"
                      },
                      "aggs": {
                        "min": {
                          "min": {
                            "field": "cost"
                          }
                        }
                      }
                    },
                    "change_point": {
                      "change_point": {
                        "buckets_path": "date>min"
                      }
                    }
                 }
              }
            }
          }
  - is_true: aggregations.terms.buckets.0.change_point.type.trend_change
  - is_true: aggregations.terms.buckets.0.change_point.type.trend_change.p_value
  - is_true: aggregations.terms.buckets.0.change_point.type.trend_change.r_value
  - is_false: aggregations.terms.buckets.1.change_point.type.trend_change
  - is_false: aggregations.terms.buckets.1.change_point.type.trend_change.p_value
  - is_false: aggregations.terms.buckets.1.change_point.type.trend_change.r_value

  - do:
      search:
        index: store
        size: 0
        body: >
          {
            "aggs": {
              "terms": {
                 "terms": {
                   "field": "kind"
                 },
                 "aggs": {
                    "date": {
                      "date_histogram": {
                        "field": "time",
                        "fixed_interval": "1s"
                      },
                      "aggs": {
                        "min": {
                          "min": {
                            "field": "cost"
                          }
                        }
                      }
                    },
                    "change_point": {
                      "change_point": {
                        "buckets_path": "date>min"
                      }
                    },
                    "select": {
                      "bucket_selector": {
                        "buckets_path": {"p_value": "change_point.p_value"},
                        "script": "params.p_value < 0.5"
                      }
                    }
                 }
              }
            }
          }
  - length: {aggregations.terms.buckets: 1}
  - is_true: aggregations.terms.buckets.0.change_point.type.trend_change
  - is_true: aggregations.terms.buckets.0.change_point.type.trend_change.p_value
  - is_true: aggregations.terms.buckets.0.change_point.type.trend_change.r_value

  - do:
      search:
        index: store
        size: 0
        body: >
          {
            "aggs": {
              "terms": {
                 "terms": {
                   "field": "kind"
                 },
                 "aggs": {
                    "date": {
                      "date_histogram": {
                        "field": "time",
                        "fixed_interval": "1s"
                      },
                      "aggs": {
                        "min": {
                          "min": {
                            "field": "cost"
                          }
                        }
                      }
                    },
                    "change_point": {
                      "change_point": {
                        "buckets_path": "date>min"
                      }
                    },
                    "sort": {
                      "bucket_sort": {
                        "sort": [{"change_point.p_value": {"order": "desc"}}]
                      }
                    }
                 }
              }
            }
          }
  - length: {aggregations.terms.buckets: 2}
  - is_false: aggregations.terms.buckets.0.change_point.type.trend_change
  - is_false: aggregations.terms.buckets.0.change_point.type.trend_change.p_value
  - is_false: aggregations.terms.buckets.0.change_point.type.trend_change.r_value
  - is_true: aggregations.terms.buckets.1.change_point.type.trend_change
  - is_true: aggregations.terms.buckets.1.change_point.type.trend_change.p_value
  - is_true: aggregations.terms.buckets.1.change_point.type.trend_change.r_value
