import { createAction } from '@activepieces/pieces-framework';
import {
  httpClient,
  HttpMethod,
  HttpRequest,
} from '@activepieces/pieces-common';
import { mauticCommon } from '../common';
import { mauticAuth } from '../auth';

export const createCompany = createAction({
  auth: mauticAuth,
  description: 'Creates a new company in Mautic CRM', // Must be a unique across the piece, this shouldn't be changed.
  audience: 'both',
  aiMetadata: {
    description:
      'Create a new company in Mautic from the provided field values (company name, address, custom fields, etc.). Use when adding an organization to Mautic; to modify an existing company use Update Company, and check Search Company first to avoid duplicates. Not idempotent: each call inserts a new company record.',
    idempotent: false,
  },
  displayName: 'Create Company',
  name: 'create_mautic_company',
  props: {
    fields: mauticCommon.companyFields,
  },
  run: async function (context) {
    const { base_url, username, password } = context.auth.props;
    const request: HttpRequest = {
      method: HttpMethod.POST,
      url:
        (base_url.endsWith('/') ? base_url : base_url + '/') +
        'api/companies/new',
      body: JSON.stringify(context.propsValue.fields),
      headers: {
        Authorization:
          'Basic ' + Buffer.from(`${username}:${password}`).toString('base64'),
        'Content-Type': 'application/json',
      },
    };
    return await httpClient.sendRequest(request);
  },
});
