/**
 * Service for interacting with the Task Management API.
 */

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

const API_BASE = '/tasks';

// [DEF:getTasks:Function]
/* @PURPOSE: 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.
*/
/**
 * Fetch a list of tasks with pagination and optional status filter.
 * @param {number} limit - Maximum number of tasks to return.
 * @param {number} offset - Number of tasks to skip.
 * @param {string|null} status - Filter by task status (optional).
 * @returns {Promise<Array>} List of tasks.
 */
export async function getTasks(limit = 10, offset = 0, status = null) {
    const params = new URLSearchParams({
        limit: limit.toString(),
        offset: offset.toString()
    });
    if (status) {
        params.append('status', status);
    }

    return requestApi(`${API_BASE}?${params.toString()}`);
}
// [/DEF:getTasks:Function]

// [DEF:getTask:Function]
/* @PURPOSE: Fetch details for a specific task.
   @PRE: taskId must be provided.
   @POST: Returns a promise resolving to task details.
*/
/**
 * Fetch details for a specific task.
 * @param {string} taskId - The ID of the task.
 * @returns {Promise<Object>} Task details.
 */
export async function getTask(taskId) {
    return requestApi(`${API_BASE}/${taskId}`);
}
// [/DEF:getTask:Function]

// [DEF:getTaskLogs:Function]
/* @PURPOSE: Fetch logs for a specific task.
   @PRE: taskId must be provided.
   @POST: Returns a promise resolving to a list of log entries.
*/
/**
 * Fetch logs for a specific task.
 * @param {string} taskId - The ID of the task.
 * @returns {Promise<Array>} List of log entries.
 */
export async function getTaskLogs(taskId) {
    // Currently, logs are included in the task object, but we might have a separate endpoint later.
    // For now, we fetch the task and return its logs.
    // Or if we implement T017 (GET /api/tasks/{task_id}/logs), we would use that.
    // The current backend implementation in tasks.py does NOT have a separate /logs endpoint yet.
    // T017 is in Phase 3.
    // So for now, we'll fetch the task.
    const task = await getTask(taskId);
    return task.logs || [];
}
// [/DEF:getTaskLogs:Function]

// [DEF:resumeTask:Function]
/* @PURPOSE: 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.
*/
/**
 * Resume a task that is awaiting input (e.g., passwords).
 * @param {string} taskId - The ID of the task.
 * @param {Object} passwords - Map of database names to passwords.
 * @returns {Promise<Object>} Updated task object.
 */
export async function resumeTask(taskId, passwords) {
    return requestApi(`${API_BASE}/${taskId}/resume`, 'POST', { passwords });
}
// [/DEF:resumeTask:Function]

// [DEF:resolveTask:Function]
/* @PURPOSE: Resolve a task that is awaiting mapping.
   @PRE: taskId and resolutionParams must be provided.
   @POST: Returns a promise resolving to the updated task object.
*/
/**
 * Resolve a task that is awaiting mapping.
 * @param {string} taskId - The ID of the task.
 * @param {Object} resolutionParams - Resolution parameters.
 * @returns {Promise<Object>} Updated task object.
 */
export async function resolveTask(taskId, resolutionParams) {
    return requestApi(`${API_BASE}/${taskId}/resolve`, 'POST', { resolution_params: resolutionParams });
}
// [/DEF:resolveTask:Function]

// [DEF:clearTasks:Function]
/* @PURPOSE: Clear tasks based on status.
   @PRE: status is a string or null.
   @POST: Returns a promise that resolves when tasks are cleared.
*/
/**
 * Clear tasks based on status.
 * @param {string|null} status - Filter by task status (optional).
 */
export async function clearTasks(status = null) {
    const params = new URLSearchParams();
    if (status) {
        params.append('status', status);
    }

    return requestApi(`${API_BASE}?${params.toString()}`, 'DELETE');
}
// [/DEF:clearTasks:Function]