setup:
  - requires:
      test_runner_features: [ "allowed_warnings" ]
  - skip:
      features: ["allowed_warnings", "headers"]
  - do:
      indices.create:
        index: foo
        body:
          mappings:
            properties:
              the_field:
                type: date
              value_field:
                type: integer

  - do:
      indices.create:
        index: dummy-rollup-index
        body:
          settings:
            number_of_shards: 1
            number_of_replicas: 0
          mappings:
            _meta:
              _rollup:
                my-id: { }

  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      rollup.put_job:
        id: foo
        body:  >
          {
            "index_pattern": "foo",
            "rollup_index": "foo_rollup",
            "cron": "*/30 * * * * ? 2017",
            "page_size" :10,
            "groups" : {
              "date_histogram": {
                "field": "the_field",
                "calendar_interval": "1h"
              }
            },
            "metrics": [
              {
                "field": "value_field",
                "metrics": ["min", "max", "sum"]
              }
            ]
          }
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."

---
"Test basic delete_job":
  - requires:
      test_runner_features: ["allowed_warnings"]
  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      rollup.get_jobs:
        id: foo

  - match:
      jobs:
        - config:
            id: "foo"
            index_pattern: "foo"
            rollup_index: "foo_rollup"
            cron: "*/30 * * * * ? 2017"
            page_size: 10
            groups :
              date_histogram:
                calendar_interval: "1h"
                field: "the_field"
                time_zone: "UTC"
            metrics:
              - field: "value_field"
                metrics:
                 - "min"
                 - "max"
                 - "sum"
            timeout: "20s"
          stats:
            pages_processed: 0
            documents_processed: 0
            rollups_indexed: 0
            trigger_count: 0
            search_failures: 0
            index_failures: 0
            index_time_in_ms: 0
            index_total: 0
            search_time_in_ms: 0
            search_total: 0
            processing_time_in_ms: 0
            processing_total: 0
          status:
            job_state: "stopped"

  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      rollup.delete_job:
        id: foo
  - is_true: acknowledged

  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      rollup.get_jobs:
        id: foo
  - match:
      jobs: []

---
"Test delete job twice":

  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      rollup.get_jobs:
        id: foo

  - match:
      jobs:
        - config:
            id: "foo"
            index_pattern: "foo"
            rollup_index: "foo_rollup"
            cron: "*/30 * * * * ? 2017"
            page_size: 10
            groups :
              date_histogram:
                calendar_interval: "1h"
                field: "the_field"
                time_zone: "UTC"
            metrics:
              - field: "value_field"
                metrics:
                 - "min"
                 - "max"
                 - "sum"
            timeout: "20s"
          stats:
            pages_processed: 0
            documents_processed: 0
            rollups_indexed: 0
            trigger_count: 0
            search_failures: 0
            index_failures: 0
            index_time_in_ms: 0
            index_total: 0
            search_time_in_ms: 0
            search_total: 0
            processing_time_in_ms: 0
            processing_total: 0
          status:
            job_state: "stopped"

  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      rollup.delete_job:
        id: foo
  - is_true: acknowledged

  - do:
      allowed_warnings:
        - The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information.
      rollup.get_jobs:
        id: foo
  - match:
      jobs: []

---
"Test delete running job":

  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      rollup.get_jobs:
        id: foo

  - match:
      jobs:
        - config:
            id: "foo"
            index_pattern: "foo"
            rollup_index: "foo_rollup"
            cron: "*/30 * * * * ? 2017"
            page_size: 10
            groups :
              date_histogram:
                calendar_interval: "1h"
                field: "the_field"
                time_zone: "UTC"
            metrics:
              - field: "value_field"
                metrics:
                 - "min"
                 - "max"
                 - "sum"
            timeout: "20s"
          stats:
            pages_processed: 0
            documents_processed: 0
            rollups_indexed: 0
            trigger_count: 0
            search_failures: 0
            index_failures: 0
            index_time_in_ms: 0
            index_total: 0
            search_time_in_ms: 0
            search_total: 0
            processing_time_in_ms: 0
            processing_total: 0
          status:
            job_state: "stopped"

  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      rollup.start_job:
        id: foo
  - is_true: started

  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      catch: request
      rollup.delete_job:
        id: foo
  - is_false: acknowledged
  - match: { task_failures.0.reason.type: "illegal_state_exception" }
  - match: { task_failures.0.reason.reason: "Could not delete job [foo] because indexer state is [STARTED].  Job must be [STOPPED] before deletion." }

---
"Test delete non-existent job":

  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      catch: /the task with id \[does_not_exist\] doesn't exist/
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      rollup.delete_job:
        id: does_not_exist
