import { createAction, Property } from '@activepieces/pieces-framework';
import { httpClient, HttpMethod } from '@activepieces/pieces-common';
import { manusAuth } from '../common/auth';

async function getTasksDropdown(auth: string): Promise<{ label: string; value: string }[]> {
  try {
    const response = await httpClient.sendRequest({
      method: HttpMethod.GET,
      url: 'https://api.manus.ai/v1/tasks',
      headers: {
        'accept': 'application/json',
        'API_KEY': auth,
      },
      queryParams: {
        limit: '50',
        order: 'desc',
        orderBy: 'created_at',
      },
    });

    const tasks = response.body?.data || [];
    return tasks.map((task: any) => ({
      label: `${task.instructions?.substring(0, 50) || 'Untitled Task'} (${task.status})`,
      value: task.id,
    }));
  } catch (error) {
    return [];
  }
}

export const getTask = createAction({
  name: 'get_task',
  auth: manusAuth,
  displayName: 'Get Task',
  description: 'Retrieve detailed information about a specific task by its ID',
  audience: 'both',
  aiMetadata: { description: 'Fetches the full details and current status of a single Manus task by its ID. Use to check progress or read back the result of a task you created or know the ID of. Read-only and idempotent.', idempotent: true },
  props: {
    taskId: Property.Dropdown({
      auth: manusAuth,
      displayName: 'Task',
      description: 'Select the task to retrieve details for',
      required: true,
      refreshers: ['auth'],
      options: async ({ auth }) => {
        if (!auth) return { disabled: true, options: [], placeholder: 'Connect account first' };
        return { disabled: false, options: await getTasksDropdown(auth.secret_text) };
      },
    }),
  },
  async run(context) {
    const response = await httpClient.sendRequest({
      method: HttpMethod.GET,
      url: `https://api.manus.ai/v1/tasks/${context.propsValue['taskId']}`,
      headers: {
        'accept': 'application/json',
        'API_KEY': context.auth.secret_text,
      },
    });

    return response.body;
  },
});
