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

interface Item {
  name: string;
  id: string;
  number: string;
}

export const sendMms = createAction({
  name: 'sendMms',
  displayName: 'Send MMS',
  auth: krispcallAuth,
  description: 'Send mms in krispcall.',
  audience: 'both',
  aiMetadata: {
    description:
      'Send an MMS message carrying one or more media URLs from a connected KrispCall account number to a destination phone number, with optional text content. Use when an agent needs to send media (images/files) rather than a plain SMS; the from number must be provisioned on the account and at least one media URL is required. Not idempotent — each call dispatches a new message.',
    idempotent: false,
  },
  props: {
    from_number: Property.Dropdown({
      auth: krispcallAuth,
      displayName: 'from number',
      description: 'Select an number',
      required: true,
      refreshers: ['auth'],
      refreshOnSearch: false,
      options: async ({ auth }) => {
        if (!auth) {
          return {
            disabled: true,
            options: [],
            placeholder: 'Please connect your account first',
          };
        }
          try {
          const authVaue = auth.props;
          const res = await httpClient.sendRequest<Item[]>({
            method: HttpMethod.GET,
            url: 'https://app.krispcall.com/api/v3/platform/activepiece/get-numbers',
            headers: {
              'X-API-KEY': authVaue.apiKey,
            },
          });
          const mappedOptions = res?.body?.map((item) => {
            return {
              label: item.name,
              value: item.number,
            };
          });

          return {
            disabled: false,
            options: mappedOptions,
          };
        } catch (error) {
          // Handle error
          console.error(error);
          return { disabled: true, options: [] }; // Return empty options array or handle error accordingly
        }
      },
    }),
    to_number: Property.ShortText({
      displayName: 'To Number',
      description: 'Enter the number to which you want to send sms.',
      required: true,
    }),
    content: Property.ShortText({
      displayName: 'content',
      description: 'Enter your message here.',
      required: false,
    }),
    medias: Property.Array({
      displayName: 'Medias url',
      description: 'Enter medias urls',
      required: true,
    }),
  },
  async run({ auth, propsValue }) {
    const res = await httpClient.sendRequest<string[]>({
      method: HttpMethod.POST,
      url: 'https://app.krispcall.com/api/v3/platform/activepiece/send-mms',
      headers: {
        'X-API-KEY': auth.props.apiKey,
      },

      body: {
        from_number: propsValue.from_number,
        to_number: propsValue.to_number,
        content: propsValue.content,
        medias: propsValue.medias,
      },
    });
    return res.body;
  },
});
