import { biginAuth } from '../auth';
import { createAction, Property } from '@activepieces/pieces-framework';
import { biginApiService } from '../common/request';

export const searchContactRecord = createAction({
  auth: biginAuth,
  name: 'searchContactRecord',
  displayName: 'Search Contact Record',
  description: 'Searches contacts by criteria, email, phone, or word',
  audience: 'both',
  aiMetadata: { description: 'Searches contact (person) records in Bigin CRM and returns matches. The search mode selects what the term is matched against: Criteria (name/email/mobile with equals or starts-with), a dedicated Email lookup, a dedicated Phone lookup, or a free-text Word search. Use to find a person by name, email, or phone before referencing or updating them. Idempotent: read-only, repeating the search returns the same matches.', idempotent: true },
  props: {
    mode: Property.StaticDropdown({
      displayName: 'Search Mode',
      description: 'Choose how to search Contacts',
      required: true,
      defaultValue: 'criteria',
      options: {
        options: [
          { label: 'Criteria (name/email/mobile)', value: 'criteria' },
          { label: 'Email', value: 'email' },
          { label: 'Phone', value: 'phone' },
          { label: 'Word', value: 'word' },
        ],
      },
    }),
    searchTerm: Property.ShortText({
      displayName: 'Search Term',
      description: 'Text, email, phone, or word based on the selected mode',
      required: true,
    }),
  },
  async run({ auth, propsValue }) {
    const { searchTerm, mode } = propsValue;

    const { access_token, data } = auth;
    const api_domain = data['api_domain'];

    let queryKey = 'criteria';
    let queryValue = '';
    if (mode === 'email') {
      queryKey = 'email';
      queryValue = searchTerm;
    } else if (mode === 'phone') {
      queryKey = 'phone';
      queryValue = searchTerm;
    } else if (mode === 'word') {
      queryKey = 'word';
      queryValue = searchTerm;
    } else {
      queryValue = ['First_Name', 'Last_Name', 'Email', 'Mobile']
        .flatMap((key) => [
          `${key}:equals:${searchTerm}`,
          `${key}:starts_with:${searchTerm}`,
        ])
        .map((condition) => `(${condition})`)
        .join('OR');
    }

    try {
      const response = await biginApiService.searchRecords(
        access_token,
        api_domain,
        'Contacts',
        {
          key: queryKey,
          value: queryValue,
        }
      );

      return {
        message: 'Contact record search completed successfully',
        data: response.data,
      };
    } catch (error: any) {
      throw new Error(`Error searching contact record: ${error.message}`);
    }
  },
});
