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

export const addContact = createAction({
  name: 'addContact',
  displayName: 'Add Contact',
  auth: krispcallAuth,
  description: 'Add contact in Krispcall',
  audience: 'both',
  aiMetadata: {
    description:
      'Create a contact in the connected KrispCall account, keyed by phone number. Use when an agent needs to register a new contact (name, company, email, address are optional; only the contact number is required). Not idempotent — calling it again with the same number creates or re-submits another contact entry.',
    idempotent: false,
  },
  props: {
    name: Property.ShortText({
      displayName: 'Name',
      description: 'Enter your name',
      required: false,
    }),
    number: Property.ShortText({
      displayName: 'Contact number',
      description: 'Enter contact number',
      required: true,
    }),
    address: Property.ShortText({
      displayName: 'Address',
      description: 'Enter your address',
      required: false,
    }),
    company: Property.ShortText({
      displayName: 'Company',
      description: 'Enter your company',
      required: false,
    }),
    email: Property.ShortText({
      displayName: 'Email',
      description: 'Enter your email',
      required: false,
    }),
  },
  async run({ auth, propsValue }) {
    const res = await httpClient.sendRequest<string[]>({
      method: HttpMethod.POST,
      url: 'https://app.krispcall.com/api/v3/platform/activepiece/add-contact',
      headers: {
        'X-API-KEY': auth.props.apiKey,
      },
      body: {
        name: propsValue.name,
        number: propsValue.number,
        company: propsValue.company,
        email: propsValue.email,
        address: propsValue.address,
      },
    });
    return res.body;
  },
});
