import { createAction, Property } from '@activepieces/pieces-framework';
import { enrichlayerAuth } from '../auth';
import { enrichlayerApiCall } from '../common/client';
import { ENDPOINTS } from '../common/constants';

export const getRoleLookup = createAction({
  name: 'get_role_lookup',
  auth: enrichlayerAuth,
  displayName: 'Look Up Role at Company',
  description:
    'Find the person who most closely matches a specified role at a company (3 credits)',
  audience: 'both',
  aiMetadata: {
    description:
      'Find the single person who best matches a given role (e.g. "ceo", "vp of engineering") at a named company. Read-only and safe to retry. Use when you need the one person holding a role; to get many people by job-title keyword use Search Company Employees. Profile enrichment adds a credit.',
    idempotent: true,
  },
  props: {
    company_name: Property.ShortText({
      displayName: 'Company Name',
      description: 'Name of the company (e.g., enrichlayer)',
      required: true,
    }),
    role: Property.ShortText({
      displayName: 'Role',
      description: 'Role to look up (e.g., ceo, cto, vp of engineering)',
      required: true,
    }),
    enrich_profile: Property.StaticDropdown({
      displayName: 'Enrich Profile',
      description:
        'Enrich the result with cached profile data (+1 credit)',
      required: false,
      options: {
        options: [
          { label: 'Skip (default)', value: 'skip' },
          { label: 'Enrich (+1 credit)', value: 'enrich' },
        ],
      },
    }),
  },
  async run(context) {
    return await enrichlayerApiCall(
      context.auth.secret_text as string,
      ENDPOINTS.ROLE_LOOKUP,
      {
        company_name: context.propsValue.company_name,
        role: context.propsValue.role,
        enrich_profile: context.propsValue.enrich_profile,
      },
    );
  },
});
