setup:
  - do:
      indices.create:
        index: test-index1
        body:
          settings:
            index:
              number_of_shards: 1
              number_of_replicas: 0

---
teardown:
  - do:
      search_application.delete:
        name: test-search-application-1
        ignore: 404

  - do:
      search_application.delete:
        name: test-search-application-2
        ignore: 404

  - do:
      indices.delete:
        index: test-index1
        ignore: 404

---
"xpack usage includes Enterprise Search":
  - requires:
      test_runner_features: [ allowed_warnings ]

  - do:
      xpack.usage: { }

  - match: {
    enterprise_search: {
      enabled: true,
      available: true,
      search_applications: { count: 0 },
      analytics_collections: { count: 0 },
      query_rulesets: { total_count: 0, total_rule_count: 0, min_rule_count: 0, max_rule_count: 0 }
    }
  }

  - do:
      search_application.put:
        name: test-search-application-1
        body:
          indices: [ "test-index1" ]
          template:
            script:
              source:
                query:
                  query_string:
                    query: "{{query_string}}"

  - do:
      xpack.usage: { }

  - match: {
    enterprise_search: {
      enabled: true,
      available: true,
      search_applications: { count: 1 },
      analytics_collections: { count: 0 },
      query_rulesets: { total_count: 0, total_rule_count: 0, min_rule_count: 0, max_rule_count: 0 }
    }
  }

  - do:
      search_application.put:
        name: test-search-application-2
        body:
          indices: [ "test-index1" ]
          template:
            script:
              source:
                query:
                  query_string:
                    query: "{{query_string}}"

  - do:
      allowed_warnings:
        - "Behavioral Analytics is deprecated and will be removed in a future release."
      search_application.put_behavioral_analytics:
        name: test-analytics-collection

  - do:
      xpack.usage: { }

  - match: {
    enterprise_search: {
      enabled: true,
      available: true,
      search_applications: { count: 2 },
      analytics_collections: { count: 1 },
      query_rulesets: { total_count: 0, total_rule_count: 0, min_rule_count: 0, max_rule_count: 0 }
    }
  }

  - do:
      search_application.delete:
        name: test-search-application-2

  - do:
      xpack.usage: { }

  - match: {
    enterprise_search: {
      enabled: true,
      available: true,
      search_applications: { count: 1 },
      analytics_collections: { count: 1 },
      query_rulesets: { total_count: 0, total_rule_count: 0, min_rule_count: 0, max_rule_count: 0 }
    }
  }

  - do:
      allowed_warnings:
        - "Behavioral Analytics is deprecated and will be removed in a future release."
      search_application.delete_behavioral_analytics:
        name: test-analytics-collection

  - do:
      xpack.usage: { }

  - match: {
    enterprise_search: {
      enabled: true,
      available: true,
      search_applications: { count: 1 },
      analytics_collections: { count: 0 },
      query_rulesets: { total_count: 0, total_rule_count: 0, min_rule_count: 0, max_rule_count: 0 }
    }
  }

  - do:
      query_rules.put_ruleset:
        ruleset_id: test-query-ruleset
        body:
          rules:
            - rule_id: query-rule-id1
              type: pinned
              criteria:
                - type: exact
                  metadata: query_string
                  values: [ puggles ]
              actions:
                ids:
                  - 'id1'
                  - 'id2'
            - rule_id: query-rule-id2
              type: exclude
              criteria:
                - type: exact
                  metadata: query_string
                  values: [ pugs ]
              actions:
                ids:
                  - 'id3'
                  - 'id4'

  - do:
      query_rules.put_ruleset:
        ruleset_id: test-query-ruleset2
        body:
          rules:
            - rule_id: query-rule-id1
              type: pinned
              criteria:
                - type: exact
                  metadata: query_string
                  values: [ beagles ]
              actions:
                ids:
                  - 'id1'
                  - 'id2'
            - rule_id: query-rule-id2
              type: pinned
              criteria:
                - type: exact
                  metadata: query_string
                  values: [ pugs ]
              actions:
                ids:
                  - 'id3'
                  - 'id4'
            - rule_id: query-rule-id3
              type: pinned
              criteria:
                - type: exact
                  metadata: query_string
                  values: [ puggles ]
              actions:
                ids:
                  - 'id4'
                  - 'id5'

  - do:
      xpack.usage: { }

  - match: { "enterprise_search.enabled": true }
  - match: { "enterprise_search.available": true }
  - match: { "enterprise_search.search_applications.count": 1 }
  - match: { "enterprise_search.analytics_collections.count": 0 }
  - match: { "enterprise_search.query_rulesets.total_count": 2 }
  - match: { "enterprise_search.query_rulesets.total_rule_count": 5 }
  - match: { "enterprise_search.query_rulesets.min_rule_count": 2 }
  - match: { "enterprise_search.query_rulesets.max_rule_count": 3 }
  - match: { "enterprise_search.query_rulesets.rule_criteria_total_counts.exact": 5 }

  - do:
      query_rules.delete_ruleset:
        ruleset_id: test-query-ruleset2

  - do:
      xpack.usage: { }

  - match: { "enterprise_search.enabled": true }
  - match: { "enterprise_search.available": true }
  - match: { "enterprise_search.search_applications.count": 1 }
  - match: { "enterprise_search.analytics_collections.count": 0 }
  - match: { "enterprise_search.query_rulesets.total_count": 1 }
  - match: { "enterprise_search.query_rulesets.total_rule_count": 2 }
  - match: { "enterprise_search.query_rulesets.min_rule_count": 2 }
  - match: { "enterprise_search.query_rulesets.max_rule_count": 2 }
  - match: { "enterprise_search.query_rulesets.rule_criteria_total_counts.exact": 2 }
