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

export const sendSms = createAction({
  auth:autocallsAuth,
  name: 'sendSms',
  displayName: 'Send SMS to Customer',
  description: "Send an SMS to a customer using a phone number from our platform.",
  audience: 'both',
  aiMetadata: { description: 'Sends a single SMS text message to a customer phone number from a selected SMS-capable Autocalls phone number. Use to deliver an outbound text rather than place a call. Requires a sender phone number id, recipient number, and message body (max 300 characters); each call sends a new message, so it is not idempotent.', idempotent: false },
  props: {
    from: Property.Dropdown({
      auth: autocallsAuth,
      displayName: 'From phone number',
      description: 'Select an SMS capable phone number to send the SMS from',
      required: true,
      refreshers: ['auth'],
      refreshOnSearch: false,
      options: async ({ auth }) => {
        const res = await httpClient.sendRequest({
          method: HttpMethod.GET,
          url: baseApiUrl + 'api/user/phone-numbers',
          headers: {
            Authorization: "Bearer " + auth?.secret_text,
            'Content-Type': 'application/json',
            'Accept': 'application/json'
          },
        });

        if (res.status !== 200) {
          return {
            disabled: true,
            placeholder: 'Error fetching phone numbers',
            options: [],
          };
        } else if (res.body.length === 0) {
          return {
            disabled: true,
            placeholder: 'No phone numbers found. Purchase an SMS capable phone number first.',
            options: [],
          };
        }

        return {
          options: res.body.map((phoneNumber: any) => ({
            value: phoneNumber.id,
            label: phoneNumber.phone_number,
          })),
        };
      }
    }),
    to: Property.ShortText({
      displayName: 'Customer phone number',
      description: 'Enter the phone number of the customer',
      required: true,
    }),

    body: Property.ShortText({
      displayName: 'Text message',
      description: 'Enter the text message to send to the customer (max 300 characters)',
      required: true,
    }),
  },
  async run(context) {
    const res = await httpClient.sendRequest<string[]>({
      method: HttpMethod.POST,
      url: baseApiUrl + 'api/user/sms',
      body: {
        from: context.propsValue['from'],
        to: context.propsValue['to'],
        body: context.propsValue['body'],
      },
      headers: {
        Authorization: "Bearer " + context.auth.secret_text,
        'Content-Type': 'application/json',
        'Accept': 'application/json'
      },
    });
    return res.body;
  },
});
