setup:
  - skip:
      features:
        - headers
        - allowed_warnings
  - do:
      allowed_warnings:
        - "index [.ml-inference-000003] matches multiple legacy templates [.ml-inference-000003, global], composable templates will only match a single template"
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      ml.put_trained_model:
        model_id: a-unused-regression-model1
        body: >
          {
            "description": "empty model for tests",
            "tags": ["regression", "tag1"],
            "input": {"field_names": ["field1", "field2"]},
            "inference_config": {"regression": {}},
            "definition": {
               "preprocessors": [],
               "trained_model": {
                  "tree": {
                     "feature_names": ["field1", "field2"],
                     "tree_structure": [
                        {"node_index": 0, "leaf_value": 1}
                     ],
                     "target_type": "regression"
                  }
               }
            }
          }

  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      ml.put_trained_model:
        model_id: a-unused-regression-model
        body: >
          {
            "description": "empty model for tests",
            "tags": ["regression", "tag1"],
            "input": {"field_names": ["field1", "field2"]},
            "inference_config": {"regression": {}},
            "definition": {
               "preprocessors": [],
               "trained_model": {
                  "tree": {
                     "feature_names": ["field1", "field2"],
                     "tree_structure": [
                        {"node_index": 0, "leaf_value": 1}
                     ],
                     "target_type": "regression"
                  }
               }
            }
          }

  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      ml.put_trained_model:
        model_id: another-regression-model
        body: >
          {
            "description": "empty model for tests",
            "tags": ["regression", "tag1"],
            "input": {"field_names": ["field1", "field2"]},
            "inference_config": {"regression": {}},
            "definition": {
               "preprocessors": [],
               "trained_model": {
                  "tree": {
                     "feature_names": ["field1", "field2"],
                     "tree_structure": [
                        {"node_index": 0, "leaf_value": 1}
                     ],
                     "target_type": "regression"
                  }
               }
            }
          }
  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      ml.put_trained_model_alias:
        model_alias: "my-regression"
        model_id: "a-unused-regression-model1"
---
"Test get stats given missing trained model":

  - do:
      catch: missing
      ml.get_trained_models_stats:
        model_id: "missing-trained-model"
---
"Test get stats given expression without matches and allow_no_match is false":

  - do:
      catch: missing
      ml.get_trained_models_stats:
        model_id: "missing-trained-model*"
        allow_no_match: false

---
"Test get stats given expression without matches and allow_no_match is true":

  - do:
      ml.get_trained_models_stats:
        model_id: "missing-trained-model*"
        allow_no_match: true
  - match: { count: 0 }
  - match: { trained_model_stats: [] }
---
"Test get stats given trained models":
  - do:
      ml.get_trained_models_stats:
        model_id: "a-unused-regression-model"

  - match: { count: 1 }

  - do:
      ml.get_trained_models_stats:
        model_id: "_all"
  - match: { count: 4 }
  - match: { trained_model_stats.0.model_id: a-unused-regression-model }
  - match: { trained_model_stats.0.pipeline_count: 0 }
  - is_false: trained_model_stats.0.ingest
  - match: { trained_model_stats.1.model_id: a-unused-regression-model1 }
  - match: { trained_model_stats.1.pipeline_count: 0 }
  - is_false: trained_model_stats.1.ingest
  - match: { trained_model_stats.2.pipeline_count: 0 }
  - is_false: trained_model_stats.2.ingest

  - do:
      ml.get_trained_models_stats:
        model_id: "*"
  - match: { count: 4 }
  - match: { trained_model_stats.0.model_id: a-unused-regression-model }
  - match: { trained_model_stats.0.pipeline_count: 0 }
  - is_false: trained_model_stats.0.ingest
  - match: { trained_model_stats.1.model_id: a-unused-regression-model1 }
  - match: { trained_model_stats.1.pipeline_count: 0 }
  - is_false: trained_model_stats.1.ingest
  - match: { trained_model_stats.2.pipeline_count: 0 }
  - is_false: trained_model_stats.2.ingest

  - do:
      ml.get_trained_models_stats:
        model_id: "a-unused-regression-model*"
  - match: { count: 2 }
  - match: { trained_model_stats.0.model_id: a-unused-regression-model }
  - match: { trained_model_stats.0.pipeline_count: 0 }
  - is_false: trained_model_stats.0.ingest
  - match: { trained_model_stats.1.model_id: a-unused-regression-model1 }
  - match: { trained_model_stats.1.pipeline_count: 0 }
  - is_false: trained_model_stats.1.ingest

  - do:
      ml.get_trained_models_stats:
        model_id: "a-unused-regression-model*"
        size: 1
  - match: { count: 2 }
  - match: { trained_model_stats.0.model_id: a-unused-regression-model }
  - match: { trained_model_stats.0.pipeline_count: 0 }
  - is_false: trained_model_stats.0.ingest

  - do:
      ml.get_trained_models_stats:
        model_id: "a-unused-regression-model*"
        from: 1
        size: 1
  - match: { count: 2 }
  - match: { trained_model_stats.0.model_id: a-unused-regression-model1 }
  - match: { trained_model_stats.0.pipeline_count: 0 }
  - is_false: trained_model_stats.0.ingest

  - do:
      ml.get_trained_models_stats:
        model_id: "another-regression-model"

  - match: { count: 1 }
  - match: { trained_model_stats.0.model_id: another-regression-model }
  - match: { trained_model_stats.0.pipeline_count: 0 }


# test with model alias
  - do:
      ml.get_trained_models_stats:
        model_id: "my-regression"

  - match: { count: 1 }
  - match: { trained_model_stats.0.model_id: a-unused-regression-model1 }

  - do:
      ml.get_trained_models_stats:
        model_id: "my-regression,another-regression-model"

  - match: { count: 2 }
  - match: { trained_model_stats.0.model_id: a-unused-regression-model1 }
  - match: { trained_model_stats.1.model_id: another-regression-model }
