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

export const getJobStatus = createAction({
  auth: promptmateAuth,
  name: 'get_job_status',
  displayName: 'Get Job Status and Result',
  description: 'Retrieve the status and results of a PromptMate app job',
  audience: 'both',
  aiMetadata: { description: 'Looks up the current status and any available results for a single PromptMate app job, given its appId and jobId (the jobId comes from a prior run-app call). Use it to poll a submitted job until it completes and to fetch its output; optionally restrict to the default result fields only. Read-only and safe to repeat.', idempotent: true },
  props: {
    appId: Property.Dropdown({
      displayName: 'App',
      description: 'Select the app for which to get job status',
      required: true,
      refreshers: [],
      auth: promptmateAuth,
      options: async ({ auth }) => {
        if (!auth) {
          return {
            options: [],
            disabled: true,
          };
        }
        return await getAppDropdownOptions(auth.secret_text);
      },
    }),
    jobId: Property.ShortText({
      displayName: 'Job ID',
      description: 'The ID of the job to check',
      required: true,
    }),
    onlyDefaultResultFields: Property.Checkbox({
      displayName: 'Only Default Result Fields',
      description: 'Return only the default result fields',
      required: false,
      defaultValue: true,
    }),
  },
  async run({ auth, propsValue }) {
    const { appId, jobId, onlyDefaultResultFields } = propsValue;

    const queryParams: Record<string, string> = {
      appId,
      jobId,
    };

    if (onlyDefaultResultFields !== undefined) {
      queryParams['onlyDefaultResultFields'] = onlyDefaultResultFields.toString();
    }

    const response = await httpClient.sendRequest({
      method: HttpMethod.GET,
      url: 'https://api.promptmate.io/v1/app-jobs',
      headers: {
        'x-api-key': auth.secret_text,
      },
      queryParams,
    });

    return response.body;
  },
});
