// #region ToolsService [C:2] [TYPE Service] [SEMANTICS tool, service, task, api, runner]
// @BRIEF Service for generic Task API communication used by Tools — run tasks and poll status.
// @LAYER Service
// @RELATION DEPENDS_ON -> [ApiModule.requestApi]

import { requestApi } from '../lib/api';

const API_BASE = '/tasks';

// #region runTask [C:2] [TYPE Function] [SEMANTICS task, run, plugin]
// @BRIEF Start a new task for a given plugin.
// @PRE pluginId and params must be provided.
// @POST Returns a promise resolving to the task instance.
// @SIDE_EFFECT Creates a new task on the backend.
export async function runTask<T = unknown>(pluginId: string, params: Record<string, unknown>): Promise<T> {
	return requestApi<T>(API_BASE, 'POST', { plugin_id: pluginId, params });
}
// #endregion runTask

// #region getTaskStatus [C:2] [TYPE Function] [SEMANTICS task, status, poll]
// @BRIEF Fetch details for a specific task (to poll status or get result).
// @PRE taskId must be provided.
// @POST Returns a promise resolving to task details.
export async function getTaskStatus<T = unknown>(taskId: string): Promise<T> {
	return requestApi<T>(`${API_BASE}/${taskId}`);
}
// #endregion getTaskStatus
// #endregion ToolsService
