setup:
  - do:
      indices.create:
        index: bar
        body:
          mappings:
            properties:
              baz:
                type: keyword
              a:
                type: keyword
              b:
                type: keyword

---
"Test enrich crud apis":
  - do:
      enrich.put_policy:
        name: policy-crud
        body:
          match:
            indices: ["bar*"]
            match_field: baz
            enrich_fields: ["a", "b"]
  - is_true: acknowledged

  - do:
      enrich.execute_policy:
        name: policy-crud
  - match: { status.phase: "COMPLETE" }

  - do:
      enrich.get_policy:
        name: policy-crud
  - length: { policies: 1 }
  - match: { policies.0.config.match.name: policy-crud }
  - match: { policies.0.config.match.indices: ["bar*"] }
  - match: { policies.0.config.match.match_field: baz }
  - match: { policies.0.config.match.enrich_fields: ["a", "b"] }

  - do:
      enrich.get_policy: {}
  - length: { policies: 1 }
  - match: { policies.0.config.match.name: policy-crud }
  - match: { policies.0.config.match.indices: ["bar*"] }
  - match: { policies.0.config.match.match_field: baz }
  - match: { policies.0.config.match.enrich_fields: ["a", "b"] }

  - do:
      enrich.stats: {}
  - length: { executing_policies: 0}
  - length: { coordinator_stats: 1}
  - match: { coordinator_stats.0.queue_size: 0}
  - match: { coordinator_stats.0.remote_requests_current: 0}
  - gte: { coordinator_stats.0.remote_requests_total: 0}
  - gte: { coordinator_stats.0.executed_searches_total: 0}

  - do:
      enrich.delete_policy:
        name: policy-crud
  - is_true: acknowledged

---
"Test using the deprecated elasticsearch_version field results in a warning":
  - requires:
      cluster_features: ["gte_v8.12.0"]
      reason: "elasticsearch_version field deprecated in 8.12.0, to be removed in 9.0"
      test_runner_features: warnings

  - do:
      warnings:
        - "the [elasticsearch_version] field of an enrich policy has no effect and will be removed in a future version of Elasticsearch"
      enrich.put_policy:
        name: policy-crud-warning
        body:
          match:
            indices: ["bar*"]
            match_field: baz
            enrich_fields: ["a", "b"]
            elasticsearch_version: "any string here is acceptable"
  - is_true: acknowledged

  - do:
      enrich.delete_policy:
        name: policy-crud-warning
  - is_true: acknowledged
