import { createAction, Property } from '@activepieces/pieces-framework';
import { HttpMethod } from '@activepieces/pieces-common';
import { podioAuth } from '../auth';
import { podioApiCall, getAccessToken, dynamicTaskProperty } from '../common';

export const findTaskAction = createAction({
  auth: podioAuth,
  name: 'find_task',
  displayName: 'Find Task',
  description: 'Retrieve a task by ID for further updates.',
  audience: 'both',
  aiMetadata: { description: 'Fetches a single Podio task by its task id. Use to read a task\'s current details before updating it or to verify it exists; requires the task id. Idempotent — a read that does not modify data.', idempotent: true },
  props: {
    taskId: dynamicTaskProperty,
  },
  async run(context) {
    const accessToken = getAccessToken(context.auth);
    const { taskId } = context.propsValue;

    if (!taskId) {
      throw new Error('Task selection is required. Please select a task from the dropdown.');
    }

    const response = await podioApiCall<any>({
      method: HttpMethod.GET,
      accessToken,
      resourceUri: `/task/${taskId}`,
    });

    return response;
  },
}); 