import { createAction, Property } from '@activepieces/pieces-framework';
import { LeverAuth, leverAuth } from '../..';
import { LEVER_BASE_URL } from '../..';
import {
  AuthenticationType,
  httpClient,
  HttpMethod,
} from '@activepieces/pieces-common';

export const listOpportunityForms = createAction({
  name: 'listOpportunityForms',
  displayName: 'List opportunity forms',
  description:
    'Get all forms for a given opportunity, optionally for a given form template',
  audience: 'both',
  aiMetadata: {
    description:
      "Retrieve the completed forms attached to a Lever opportunity, identified by opportunity ID. Returns all of the opportunity's forms by default, or only forms matching a chosen form template when one is supplied (filtered client-side by base template). Read-only and idempotent.",
    idempotent: true,
  },
  auth: leverAuth,
  props: {
    opportunityId: Property.ShortText({
      displayName: 'Opportunity ID',
      required: true,
    }),
    template: Property.Dropdown({
      auth: leverAuth,
      displayName: 'Form template',
      required: false,
      refreshers: ['auth'],
      options: async ({ auth }) => {
        if (!auth) {
          return {
            disabled: true,
            placeholder: 'Please connect first.',
            options: [],
          };
        }
        const response = await httpClient.sendRequest({
          method: HttpMethod.GET,
          url: `${LEVER_BASE_URL}/form_templates?include=text`,
          authentication: {
            type: AuthenticationType.BASIC,
            username: auth.props.apiKey,
            password: '',
          },
        });
        return {
          options: response.body.data.map(
            (template: { text: string; id: string }) => {
              return { label: template.text, value: template.id };
            }
          ),
        };
      },
    }),
  },
  async run({ auth, propsValue }) {
    const response = await httpClient.sendRequest({
      method: HttpMethod.GET,
      url: `${LEVER_BASE_URL}/opportunities/${propsValue.opportunityId}/forms`,
      authentication: {
        type: AuthenticationType.BASIC,
        username: auth.props.apiKey,
        password: '',
      },
    });
    const forms = response.body.data;
    if (propsValue.template) {
      return forms.filter(
        (form: { baseTemplateId: string }) =>
          form.baseTemplateId === propsValue.template
      );
    }
    return forms;
  },
});
