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

export const upsertContactAction = createAction({
  name: 'upsert_contact',
  displayName: 'Upsert Contact',
  description: 'Create or update a contact using email or phone number',
  audience: 'both',
  aiMetadata: {
    description: 'Creates or updates an Insighto.ai contact, matching on the supplied email or phone number. Use when you need a contact to exist before referencing it elsewhere without checking first. At least one of email or phone number is required. Idempotent: the contact is keyed on email/phone, so repeating with the same input converges to the same record rather than creating duplicates.',
    idempotent: true,
  },
  auth: insightoAuth,
  props: {
    first_name: Property.ShortText({
      displayName: 'First Name',
      required: false,
    }),
    last_name: Property.ShortText({
      displayName: 'Last Name',
      required: false,
    }),
    email: Property.ShortText({
      displayName: 'Email',
      description: 'Email address of the contact',
      required: false,
    }),
    phone_number: Property.ShortText({
      displayName: 'Phone Number',
      description: 'Phone number including country code (e.g., 16501111234)',
      required: false,
    }),
  },
  async run(context) {
    try {
      const first_name = context.propsValue['first_name'];
      const last_name = context.propsValue['last_name'];
      const email = context.propsValue['email'];
      const phone_number = context.propsValue['phone_number'];

      if (!email && !phone_number) {
        throw new Error('Either email or phone number must be provided');
      }

      const apiKey = context.auth.secret_text;
      const url = `https://api.insighto.ai/api/v1/contact/upsert`;

      const queryParams: Record<string, string> = {
        api_key: apiKey,
      };

      if (first_name) queryParams['first_name'] = first_name;
      if (last_name) queryParams['last_name'] = last_name;
      if (email) queryParams['email'] = email;
      if (phone_number) queryParams['phone_number'] = phone_number;

      const response = await httpClient.sendRequest({
        method: HttpMethod.POST,
        url,
        queryParams,
        headers: {
          'Content-Type': 'application/json',
        },
      });

      if (!response.body) {
        throw new Error('No response received from Insighto.ai API');
      }

      return response.body;
    } catch (error) {
      if (error instanceof Error) {
        throw new Error(`Failed to upsert contact: ${error.message}`);
      }
      throw new Error('Failed to upsert contact');
    }
  },
});
