// #region TaskService [C:3] [TYPE Service] [SEMANTICS task, service, api, crud, status]
// @BRIEF Service for Task Management API interactions: listing, fetching details, resuming, resolving, and clearing tasks.
// @LAYER Service
// @RELATION DEPENDS_ON -> [ApiModule.requestApi]

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

const API_BASE = '/tasks';

// #region getTasks [C:2] [TYPE Function] [SEMANTICS task, list, pagination]
// @BRIEF Fetch a list of tasks with pagination and optional status filter.
// @PRE limit and offset are numbers.
// @POST Returns a promise resolving to a list of tasks.
export async function getTasks<T = unknown>(limit: number = 10, offset: number = 0, status: string | null = null): Promise<T> {
	const params = new URLSearchParams({
		limit: limit.toString(),
		offset: offset.toString(),
	});
	if (status) {
		params.append('status', status);
	}
	return requestApi<T>(`${API_BASE}?${params.toString()}`);
}
// #endregion getTasks

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

// #region getTaskLogs [C:2] [TYPE Function] [SEMANTICS task, logs]
// @BRIEF Fetch logs for a specific task.
// @PRE taskId must be provided.
// @POST Returns a promise resolving to a list of log entries.
export async function getTaskLogs<T = unknown>(taskId: string): Promise<T> {
	const task = await getTask<{ logs?: T }>(taskId);
	return (task?.logs || []) as T;
}
// #endregion getTaskLogs

// #region resumeTask [C:2] [TYPE Function] [SEMANTICS task, resume, password]
// @BRIEF Resume a task that is awaiting input (e.g., passwords).
// @PRE taskId and passwords must be provided.
// @POST Returns a promise resolving to the updated task object.
// @SIDE_EFFECT Sends credentials to the backend to resume blocked task.
export async function resumeTask<T = unknown>(taskId: string, passwords: Record<string, unknown>): Promise<T> {
	return requestApi<T>(`${API_BASE}/${taskId}/resume`, 'POST', { passwords });
}
// #endregion resumeTask

// #region resolveTask [C:2] [TYPE Function] [SEMANTICS task, resolve, mapping]
// @BRIEF Resolve a task that is awaiting mapping.
// @PRE taskId and resolutionParams must be provided.
// @POST Returns a promise resolving to the updated task object.
export async function resolveTask<T = unknown>(taskId: string, resolutionParams: Record<string, unknown>): Promise<T> {
	return requestApi<T>(`${API_BASE}/${taskId}/resolve`, 'POST', { resolution_params: resolutionParams });
}
// #endregion resolveTask

// #region clearTasks [C:2] [TYPE Function] [SEMANTICS task, clear, delete]
// @BRIEF Clear tasks based on status.
// @PRE status is a string or null.
// @POST Returns a promise that resolves when tasks are cleared.
export async function clearTasks<T = unknown>(status: string | null = null): Promise<T> {
	const params = new URLSearchParams();
	if (status) {
		params.append('status', status);
	}
	return requestApi<T>(`${API_BASE}?${params.toString()}`, 'DELETE');
}
// #endregion clearTasks
// #endregion TaskService
