<?php

declare(strict_types=1);

/**
 * Copyright OpenSearch Contributors
 * SPDX-License-Identifier: Apache-2.0
 *
 * OpenSearch PHP client
 *
 * @link      https://github.com/opensearch-project/opensearch-php/
 * @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
 * @license   http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
 * @license   https://www.gnu.org/licenses/lgpl-2.1.html GNU Lesser General Public License, Version 2.1
 *
 * Licensed to Elasticsearch B.V under one or more agreements.
 * Elasticsearch B.V licenses this file to you under the Apache 2.0 License or
 * the GNU Lesser General Public License, Version 2.1, at your option.
 * See the LICENSE file in the project root for more information.
 */

namespace OpenSearch\Namespaces;

use OpenSearch\Endpoints\Tasks\Cancel;
use OpenSearch\Endpoints\Tasks\Get;
use OpenSearch\Endpoints\Tasks\ListTasks;

/**
 * Class TasksNamespace
 *
 * NOTE: This file is autogenerated using util/GenerateEndpoints.php
 */
class TasksNamespace extends AbstractNamespace
{
    /**
     * Cancels a task, if it can be cancelled through an API.
     *
     * @param array{task_id?: string, actions?: mixed, nodes?: mixed, parent_task_id?: string, wait_for_completion?: bool, pretty?: bool, human?: bool, error_trace?: bool, source?: string, filter_path?: mixed} $params
     * - task_id: The task ID.
     * - actions: A comma-separated list of actions that should be returned. Keep empty to return all.
     * - nodes: A comma-separated list of node IDs or names used to limit the returned information. Use `_local` to return information from the node you're connecting to, specify the node name to get information from a specific node, or keep the parameter empty to get information from all nodes.
     * - parent_task_id: Returns tasks with a specified parent task ID (`node_id:task_number`). Keep empty or set to -1 to return all.
     * - wait_for_completion: Waits for the matching task to complete. When `true`, the request is blocked until the task has completed. (Default: false)
     * - pretty: Whether to pretty-format the returned JSON response. (Default: false)
     * - human: Whether to return human-readable values for statistics. (Default: false)
     * - error_trace: Whether to include the stack trace of returned errors. (Default: false)
     * - source: The URL-encoded request definition. Useful for libraries that do not accept a request body for non-POST requests.
     * - filter_path: A comma-separated list of filters used to filter the response. Use wildcards to match any field or part of a field's name. To exclude fields, use `-`.
     * @return array
     */
    public function cancel(array $params = [])
    {
        $task_id = $this->extractArgument($params, 'task_id');

        $endpoint = $this->endpointFactory->getEndpoint(Cancel::class);
        $endpoint->setParams($params);
        $endpoint->setTaskId($task_id);

        return $this->performRequest($endpoint);
    }

    /**
     * Returns information about a task.
     *
     * @param array{task_id?: string, timeout?: string, wait_for_completion?: bool, pretty?: bool, human?: bool, error_trace?: bool, source?: string, filter_path?: mixed} $params
     * - task_id: The task ID.
     * - timeout: The amount of time to wait for a response.
     * - wait_for_completion: Waits for the matching task to complete. When `true`, the request is blocked until the task has completed. (Default: false)
     * - pretty: Whether to pretty-format the returned JSON response. (Default: false)
     * - human: Whether to return human-readable values for statistics. (Default: false)
     * - error_trace: Whether to include the stack trace of returned errors. (Default: false)
     * - source: The URL-encoded request definition. Useful for libraries that do not accept a request body for non-POST requests.
     * - filter_path: A comma-separated list of filters used to filter the response. Use wildcards to match any field or part of a field's name. To exclude fields, use `-`.
     * @return array
     */
    public function get(array $params = [])
    {
        $task_id = $this->extractArgument($params, 'task_id');

        $endpoint = $this->endpointFactory->getEndpoint(Get::class);
        $endpoint->setParams($params);
        $endpoint->setTaskId($task_id);

        return $this->performRequest($endpoint);
    }

    /**
     * Returns a list of tasks.
     *
     * @param array{actions?: mixed, detailed?: bool, group_by?: mixed, nodes?: mixed, parent_task_id?: string, timeout?: string, wait_for_completion?: bool, pretty?: bool, human?: bool, error_trace?: bool, source?: string, filter_path?: mixed} $params
     * - actions: A comma-separated list of actions that should be returned. Keep empty to return all.
     * - detailed: When `true`, the response includes detailed information about shard recoveries. (Default: false)
     * - group_by: Groups tasks by parent/child relationships or nodes. (Options: nodes, none, parents)
     * - nodes: A comma-separated list of node IDs or names used to limit the returned information. Use `_local` to return information from the node you're connecting to, specify the node name to get information from a specific node, or keep the parameter empty to get information from all nodes.
     * - parent_task_id: Returns tasks with a specified parent task ID (`node_id:task_number`). Keep empty or set to -1 to return all.
     * - timeout: The amount of time to wait for a response.
     * - wait_for_completion: Waits for the matching task to complete. When `true`, the request is blocked until the task has completed. (Default: false)
     * - pretty: Whether to pretty-format the returned JSON response. (Default: false)
     * - human: Whether to return human-readable values for statistics. (Default: false)
     * - error_trace: Whether to include the stack trace of returned errors. (Default: false)
     * - source: The URL-encoded request definition. Useful for libraries that do not accept a request body for non-POST requests.
     * - filter_path: A comma-separated list of filters used to filter the response. Use wildcards to match any field or part of a field's name. To exclude fields, use `-`.
     * @return array
     */
    public function list(array $params = [])
    {

        $endpoint = $this->endpointFactory->getEndpoint(ListTasks::class);
        $endpoint->setParams($params);

        return $this->performRequest($endpoint);
    }

    /**
     * Proxy function to list() to prevent BC break since 7.4.0
     */
    public function tasksList(array $params = [])
    {
        return $this->list($params);
    }
}
