import { getErrorMessage } from '@sim/utils/errors'
import { ApolloIcon } from '@/components/icons'
import type { BlockConfig } from '@/blocks/types'
import { AuthMode, IntegrationType } from '@/blocks/types'
import type { ApolloResponse } from '@/tools/apollo/types'

export const ApolloBlock: BlockConfig<ApolloResponse> = {
  type: 'apollo',
  name: 'Apollo',
  description: 'Search, enrich, and manage contacts with Apollo.io',
  authMode: AuthMode.ApiKey,
  longDescription:
    'Integrates Apollo.io into the workflow. Search for people and companies, enrich contact data, manage your CRM contacts and accounts, add contacts to sequences, and create tasks.',
  docsLink: 'https://docs.sim.ai/tools/apollo',
  category: 'tools',
  integrationType: IntegrationType.Sales,
  tags: ['enrichment', 'sales-engagement'],
  bgColor: '#EBF212',
  icon: ApolloIcon,
  subBlocks: [
    {
      id: 'operation',
      title: 'Operation',
      type: 'dropdown',
      options: [
        { label: 'Search People', id: 'people_search' },
        { label: 'Enrich Person', id: 'people_enrich' },
        { label: 'Bulk Enrich People', id: 'people_bulk_enrich' },
        { label: 'Search Organizations', id: 'organization_search' },
        { label: 'Enrich Organization', id: 'organization_enrich' },
        { label: 'Bulk Enrich Organizations', id: 'organization_bulk_enrich' },
        { label: 'Create Contact', id: 'contact_create' },
        { label: 'Update Contact', id: 'contact_update' },
        { label: 'Search Contacts', id: 'contact_search' },
        { label: 'Bulk Create Contacts', id: 'contact_bulk_create' },
        { label: 'Bulk Update Contacts', id: 'contact_bulk_update' },
        { label: 'Create Account', id: 'account_create' },
        { label: 'Update Account', id: 'account_update' },
        { label: 'Search Accounts', id: 'account_search' },
        { label: 'Bulk Create Accounts', id: 'account_bulk_create' },
        { label: 'Bulk Update Accounts', id: 'account_bulk_update' },
        { label: 'Create Opportunity', id: 'opportunity_create' },
        { label: 'Search Opportunities', id: 'opportunity_search' },
        { label: 'Get Opportunity', id: 'opportunity_get' },
        { label: 'Update Opportunity', id: 'opportunity_update' },
        { label: 'Search Sequences', id: 'sequence_search' },
        { label: 'Add to Sequence', id: 'sequence_add' },
        { label: 'Create Task', id: 'task_create' },
        { label: 'Search Tasks', id: 'task_search' },
        { label: 'Get Email Accounts', id: 'email_accounts' },
      ],
      value: () => 'people_search',
    },
    {
      id: 'apiKey',
      title: 'Apollo API Key',
      type: 'short-input',
      placeholder: 'Enter your Apollo API key',
      password: true,
      required: true,
    },

    // People Search Fields
    {
      id: 'person_titles',
      title: 'Job Titles',
      type: 'code',
      placeholder: '["CEO", "VP of Sales"]',
      condition: { field: 'operation', value: 'people_search' },
      mode: 'advanced',
    },
    {
      id: 'person_locations',
      title: 'Locations',
      type: 'code',
      placeholder: '["San Francisco, CA", "New York, NY"]',
      condition: { field: 'operation', value: 'people_search' },
      mode: 'advanced',
    },
    {
      id: 'organization_names',
      title: 'Company Names',
      type: 'code',
      placeholder: '["Company A", "Company B"]',
      condition: { field: 'operation', value: 'people_search' },
      mode: 'advanced',
    },
    {
      id: 'person_seniorities',
      title: 'Seniority Levels',
      type: 'code',
      placeholder: '["senior", "manager", "director"]',
      condition: { field: 'operation', value: 'people_search' },
      mode: 'advanced',
    },
    {
      id: 'include_similar_titles',
      title: 'Include Similar Titles',
      type: 'switch',
      condition: { field: 'operation', value: 'people_search' },
      mode: 'advanced',
    },
    {
      id: 'contact_email_status',
      title: 'Contact Email Status',
      type: 'code',
      placeholder: '["verified", "unverified", "likely to engage"]',
      condition: { field: 'operation', value: 'people_search' },
      mode: 'advanced',
    },
    {
      id: 'contact_stage_ids',
      title: 'Contact Stage IDs',
      type: 'code',
      placeholder: '["stage_id_1", "stage_id_2"]',
      condition: { field: 'operation', value: 'contact_search' },
      mode: 'advanced',
    },
    {
      id: 'contact_label_ids',
      title: 'Contact Label IDs',
      type: 'code',
      placeholder: '["label_id_1", "label_id_2"]',
      condition: { field: 'operation', value: 'contact_search' },
      mode: 'advanced',
    },

    // People Enrich Fields
    {
      id: 'first_name',
      title: 'First Name',
      type: 'short-input',
      placeholder: 'First name',
      condition: {
        field: 'operation',
        value: ['people_enrich', 'contact_create', 'contact_update'],
      },
      required: { field: 'operation', value: 'contact_create' },
    },
    {
      id: 'last_name',
      title: 'Last Name',
      type: 'short-input',
      placeholder: 'Last name',
      condition: {
        field: 'operation',
        value: ['people_enrich', 'contact_create', 'contact_update'],
      },
      required: { field: 'operation', value: 'contact_create' },
    },
    {
      id: 'email',
      title: 'Email',
      type: 'short-input',
      placeholder: 'email@example.com',
      condition: {
        field: 'operation',
        value: ['people_enrich', 'contact_create', 'contact_update'],
      },
    },
    {
      id: 'organization_name',
      title: 'Company Name',
      type: 'short-input',
      placeholder: 'Company name',
      condition: {
        field: 'operation',
        value: ['people_enrich', 'contact_create', 'contact_update'],
      },
    },
    {
      id: 'domain',
      title: 'Domain',
      type: 'short-input',
      placeholder: 'example.com',
      condition: {
        field: 'operation',
        value: ['people_enrich', 'organization_enrich', 'account_create', 'account_update'],
      },
      required: {
        field: 'operation',
        value: 'organization_enrich',
      },
    },
    {
      id: 'reveal_personal_emails',
      title: 'Reveal Personal Emails',
      type: 'switch',
      condition: {
        field: 'operation',
        value: ['people_enrich', 'people_bulk_enrich'],
      },
      mode: 'advanced',
    },
    {
      id: 'reveal_phone_number',
      title: 'Reveal Phone Numbers',
      type: 'switch',
      condition: {
        field: 'operation',
        value: ['people_enrich', 'people_bulk_enrich'],
      },
      mode: 'advanced',
    },
    {
      id: 'webhook_url',
      title: 'Phone Reveal Webhook URL',
      type: 'short-input',
      placeholder: 'https://your-app.com/apollo-phone-webhook',
      condition: {
        field: 'operation',
        value: ['people_enrich', 'people_bulk_enrich'],
      },
      mode: 'advanced',
    },

    // Bulk Enrich Fields
    {
      id: 'people',
      title: 'People (JSON Array)',
      type: 'code',
      placeholder: '[{"first_name": "John", "last_name": "Doe", "email": "john@example.com"}]',
      condition: { field: 'operation', value: 'people_bulk_enrich' },
      required: true,
    },
    {
      id: 'organizations',
      title: 'Organizations (JSON Array)',
      type: 'code',
      placeholder:
        '[{"name": "Company A", "domain": "companya.com"}, {"name": "Company B", "domain": "companyb.com"}]',
      condition: { field: 'operation', value: 'organization_bulk_enrich' },
      required: true,
    },

    // Organization Search Fields
    {
      id: 'organization_locations',
      title: 'Organization Locations',
      type: 'code',
      placeholder: '["San Francisco, CA"]',
      condition: { field: 'operation', value: ['organization_search', 'people_search'] },
      mode: 'advanced',
    },
    {
      id: 'organization_not_locations',
      title: 'Excluded Organization Locations',
      type: 'code',
      placeholder: '["Ireland", "Minnesota"]',
      condition: { field: 'operation', value: 'organization_search' },
      mode: 'advanced',
    },
    {
      id: 'organization_ids',
      title: 'Organization IDs',
      type: 'code',
      placeholder: '["org_id_1", "org_id_2"]',
      condition: { field: 'operation', value: ['organization_search', 'people_search'] },
      mode: 'advanced',
    },
    {
      id: 'q_organization_domains_list',
      title: 'Organization Domains',
      type: 'code',
      placeholder: '["apollo.io", "stripe.com"]',
      condition: { field: 'operation', value: ['organization_search', 'people_search'] },
      mode: 'advanced',
    },
    {
      id: 'organization_num_employees_ranges',
      title: 'Employee Count Ranges',
      type: 'code',
      placeholder: '["1,10", "11,50", "51,200"]',
      condition: { field: 'operation', value: ['organization_search', 'people_search'] },
      mode: 'advanced',
    },
    {
      id: 'q_organization_keyword_tags',
      title: 'Keyword Tags',
      type: 'code',
      placeholder: '["saas", "b2b", "enterprise"]',
      condition: { field: 'operation', value: 'organization_search' },
      mode: 'advanced',
    },
    {
      id: 'q_organization_name',
      title: 'Organization Name',
      type: 'short-input',
      placeholder: 'Company name to search',
      condition: { field: 'operation', value: ['organization_search', 'account_search'] },
    },

    // Contact Fields
    {
      id: 'contact_id',
      title: 'Contact ID',
      type: 'short-input',
      placeholder: 'Apollo contact ID',
      condition: { field: 'operation', value: 'contact_update' },
      required: true,
    },
    {
      id: 'title',
      title: 'Job Title',
      type: 'short-input',
      placeholder: 'Job title',
      condition: { field: 'operation', value: ['contact_create', 'contact_update'] },
      mode: 'advanced',
    },
    {
      id: 'account_id',
      title: 'Account ID',
      type: 'short-input',
      placeholder: 'Apollo account ID',
      condition: {
        field: 'operation',
        value: ['contact_create', 'contact_update', 'account_update', 'opportunity_create'],
      },
      required: {
        field: 'operation',
        value: 'account_update',
      },
    },
    {
      id: 'owner_id',
      title: 'Owner ID',
      type: 'short-input',
      placeholder: 'Apollo user ID',
      condition: {
        field: 'operation',
        value: [
          'contact_create',
          'contact_update',
          'account_create',
          'account_update',
          'opportunity_create',
          'opportunity_update',
        ],
      },
      mode: 'advanced',
    },

    {
      id: 'website_url',
      title: 'Corporate Website URL',
      type: 'short-input',
      placeholder: 'https://www.apollo.io/',
      condition: { field: 'operation', value: ['contact_create', 'contact_update'] },
      mode: 'advanced',
    },
    {
      id: 'label_names',
      title: 'Label Names (JSON Array)',
      type: 'code',
      placeholder: '["Prospects", "VIP"]',
      condition: { field: 'operation', value: ['contact_create', 'contact_update'] },
      mode: 'advanced',
    },
    {
      id: 'contact_stage_id',
      title: 'Contact Stage ID',
      type: 'short-input',
      placeholder: 'Apollo contact stage ID',
      condition: { field: 'operation', value: ['contact_create', 'contact_update'] },
      mode: 'advanced',
    },
    {
      id: 'present_raw_address',
      title: 'Personal Location',
      type: 'short-input',
      placeholder: 'Atlanta, United States',
      condition: { field: 'operation', value: ['contact_create', 'contact_update'] },
      mode: 'advanced',
    },
    {
      id: 'direct_phone',
      title: 'Direct Phone',
      type: 'short-input',
      placeholder: '+1 555 123 4567',
      condition: { field: 'operation', value: ['contact_create', 'contact_update'] },
      mode: 'advanced',
    },
    {
      id: 'corporate_phone',
      title: 'Corporate Phone',
      type: 'short-input',
      placeholder: '+1 555 123 4567',
      condition: { field: 'operation', value: ['contact_create', 'contact_update'] },
      mode: 'advanced',
    },
    {
      id: 'mobile_phone',
      title: 'Mobile Phone',
      type: 'short-input',
      placeholder: '+1 555 123 4567',
      condition: { field: 'operation', value: ['contact_create', 'contact_update'] },
      mode: 'advanced',
    },
    {
      id: 'home_phone',
      title: 'Home Phone',
      type: 'short-input',
      placeholder: '+1 555 123 4567',
      condition: { field: 'operation', value: ['contact_create', 'contact_update'] },
      mode: 'advanced',
    },
    {
      id: 'other_phone',
      title: 'Other Phone',
      type: 'short-input',
      placeholder: '+1 555 123 4567',
      condition: { field: 'operation', value: ['contact_create', 'contact_update'] },
      mode: 'advanced',
    },
    {
      id: 'typed_custom_fields',
      title: 'Custom Fields (JSON Object)',
      type: 'code',
      placeholder: '{"custom_field_id": "value"}',
      condition: {
        field: 'operation',
        value: [
          'contact_create',
          'contact_update',
          'account_create',
          'account_update',
          'opportunity_create',
          'opportunity_update',
        ],
      },
      mode: 'advanced',
    },
    {
      id: 'contact_run_dedupe',
      title: 'Run Deduplication',
      type: 'switch',
      condition: { field: 'operation', value: 'contact_create' },
      mode: 'advanced',
    },

    // Contact Bulk Operations
    {
      id: 'contacts',
      title: 'Contacts (JSON Array)',
      type: 'code',
      placeholder:
        '[{"first_name": "John", "last_name": "Doe", "email": "john@example.com", "title": "CEO"}]',
      condition: { field: 'operation', value: 'contact_bulk_create' },
      required: true,
    },
    {
      id: 'contacts',
      title: 'Contact IDs (JSON Array)',
      type: 'code',
      placeholder: '["contact_id_1", "contact_id_2"]',
      condition: { field: 'operation', value: 'contact_bulk_update' },
    },
    {
      id: 'contact_attributes',
      title: 'Contact Attributes (JSON Array of Objects)',
      type: 'code',
      placeholder:
        '[{"id": "contact_id_1", "first_name": "John", "title": "VP Sales", "owner_id": "user_id"}]',
      condition: { field: 'operation', value: 'contact_bulk_update' },
    },
    {
      id: 'async',
      title: 'Force Asynchronous Processing',
      type: 'switch',
      condition: {
        field: 'operation',
        value: ['contact_bulk_update', 'account_bulk_update'],
      },
      mode: 'advanced',
    },
    {
      id: 'run_dedupe',
      title: 'Run Deduplication',
      type: 'switch',
      condition: {
        field: 'operation',
        value: ['contact_bulk_create', 'account_bulk_create'],
      },
      mode: 'advanced',
    },
    {
      id: 'append_label_names',
      title: 'Append Label Names (JSON Array)',
      type: 'code',
      placeholder: '["Hot Lead", "Q4 Outreach"]',
      condition: {
        field: 'operation',
        value: ['contact_bulk_create', 'account_bulk_create'],
      },
      mode: 'advanced',
    },

    // Account Fields
    {
      id: 'account_name',
      title: 'Account Name',
      type: 'short-input',
      placeholder: 'Company name',
      condition: { field: 'operation', value: ['account_create', 'account_update'] },
      required: { field: 'operation', value: 'account_create' },
    },
    {
      id: 'phone',
      title: 'Phone Number',
      type: 'short-input',
      placeholder: 'Company phone',
      condition: { field: 'operation', value: ['account_create', 'account_update'] },
      mode: 'advanced',
    },
    {
      id: 'account_stage_id',
      title: 'Account Stage ID',
      type: 'short-input',
      placeholder: 'Apollo account stage ID',
      condition: { field: 'operation', value: ['account_create', 'account_update'] },
      mode: 'advanced',
    },
    {
      id: 'raw_address',
      title: 'Account Address',
      type: 'short-input',
      placeholder: '123 Main St, San Francisco, CA',
      condition: { field: 'operation', value: ['account_create', 'account_update'] },
      mode: 'advanced',
    },

    // Account Search Fields
    {
      id: 'q_keywords',
      title: 'Keywords',
      type: 'short-input',
      placeholder: 'Search keywords',
      condition: {
        field: 'operation',
        value: ['people_search', 'contact_search'],
      },
    },
    {
      id: 'account_stage_ids',
      title: 'Account Stage IDs',
      type: 'code',
      placeholder: '["stage_id_1", "stage_id_2"]',
      condition: { field: 'operation', value: 'account_search' },
      mode: 'advanced',
    },
    {
      id: 'account_label_ids',
      title: 'Account Label IDs',
      type: 'code',
      placeholder: '["label_id_1", "label_id_2"]',
      condition: { field: 'operation', value: 'account_search' },
      mode: 'advanced',
    },

    // Account Bulk Operations
    {
      id: 'accounts',
      title: 'Accounts (JSON Array)',
      type: 'code',
      placeholder: '[{"name": "Company A", "domain": "companya.com", "phone": "+1234567890"}]',
      condition: { field: 'operation', value: 'account_bulk_create' },
      required: true,
    },
    {
      id: 'accounts',
      title: 'Account IDs (JSON Array)',
      type: 'code',
      placeholder: '["account_id_1", "account_id_2"]',
      condition: { field: 'operation', value: 'account_bulk_update' },
    },
    {
      id: 'account_bulk_update_name',
      title: 'Uniform Name (used with Account IDs)',
      type: 'short-input',
      placeholder: 'Updated Account Name',
      condition: { field: 'operation', value: 'account_bulk_update' },
      mode: 'advanced',
    },
    {
      id: 'account_bulk_update_owner_id',
      title: 'Uniform Owner ID (used with Account IDs)',
      type: 'short-input',
      placeholder: 'Apollo user ID',
      condition: { field: 'operation', value: 'account_bulk_update' },
      mode: 'advanced',
    },
    {
      id: 'account_attributes',
      title: 'Account Attributes (JSON Array of Objects)',
      type: 'code',
      placeholder:
        '[{"id": "account_id_1", "name": "Acme", "owner_id": "user_id", "account_stage_id": "stage_id"}]',
      condition: { field: 'operation', value: 'account_bulk_update' },
    },

    // Opportunity Fields
    {
      id: 'opportunity_name',
      title: 'Opportunity Name',
      type: 'short-input',
      placeholder: 'Opportunity name',
      condition: { field: 'operation', value: ['opportunity_create', 'opportunity_update'] },
      required: { field: 'operation', value: 'opportunity_create' },
    },
    {
      id: 'amount',
      title: 'Amount',
      type: 'short-input',
      placeholder: 'Plain number, no commas (e.g., 50000)',
      condition: { field: 'operation', value: ['opportunity_create', 'opportunity_update'] },
      mode: 'advanced',
    },
    {
      id: 'opportunity_stage_id',
      title: 'Opportunity Stage ID',
      type: 'short-input',
      placeholder: 'Apollo opportunity_stage_id',
      condition: { field: 'operation', value: ['opportunity_create', 'opportunity_update'] },
      mode: 'advanced',
    },
    {
      id: 'closed_date',
      title: 'Close Date',
      type: 'short-input',
      placeholder: 'YYYY-MM-DD (e.g., 2024-12-31)',
      condition: { field: 'operation', value: ['opportunity_create', 'opportunity_update'] },
      mode: 'advanced',
      wandConfig: {
        enabled: true,
        prompt: `Generate a date in YYYY-MM-DD format based on the user's description.
Examples:
- "end of this quarter" -> Calculate the last day of the current quarter in YYYY-MM-DD format
- "next month" -> Calculate 30 days from now in YYYY-MM-DD format
- "in 2 weeks" -> Calculate 14 days from now in YYYY-MM-DD format
- "end of year" -> December 31st of the current year in YYYY-MM-DD format

Return ONLY the date string in YYYY-MM-DD format - no explanations, no quotes, no extra text.`,
        placeholder: 'Describe the date (e.g., "end of quarter", "in 2 weeks")...',
        generationType: 'timestamp',
      },
    },

    // Opportunity Get
    {
      id: 'opportunity_id',
      title: 'Opportunity ID',
      type: 'short-input',
      placeholder: 'Apollo opportunity ID',
      condition: { field: 'operation', value: ['opportunity_get', 'opportunity_update'] },
      required: true,
    },

    // Opportunity / Account / Task Search shared sort
    {
      id: 'sort_by_field',
      title: 'Sort By Field',
      type: 'short-input',
      placeholder: 'Sort field name',
      condition: {
        field: 'operation',
        value: ['opportunity_search', 'account_search', 'task_search', 'contact_search'],
      },
      mode: 'advanced',
    },
    {
      id: 'sort_ascending',
      title: 'Sort Ascending',
      type: 'switch',
      condition: { field: 'operation', value: ['account_search', 'contact_search'] },
      mode: 'advanced',
    },

    // Sequence Search Fields
    {
      id: 'q_name',
      title: 'Sequence Name',
      type: 'short-input',
      placeholder: 'Search by sequence name',
      condition: { field: 'operation', value: 'sequence_search' },
    },

    // Sequence Add Fields
    {
      id: 'sequence_id',
      title: 'Sequence ID',
      type: 'short-input',
      placeholder: 'Apollo sequence ID',
      condition: { field: 'operation', value: 'sequence_add' },
      required: true,
    },
    {
      id: 'contact_ids',
      title: 'Contact IDs (JSON Array)',
      type: 'code',
      placeholder: '["contact_id_1", "contact_id_2"]',
      condition: { field: 'operation', value: ['sequence_add', 'task_create'] },
      required: { field: 'operation', value: 'task_create' },
    },
    {
      id: 'sequence_add_label_names',
      title: 'Label Names (JSON Array)',
      type: 'code',
      placeholder: '["Hot Lead", "Q4 Outreach"]',
      condition: { field: 'operation', value: 'sequence_add' },
      mode: 'advanced',
    },
    {
      id: 'send_email_from_email_account_id',
      title: 'Send Email From (Email Account ID)',
      type: 'short-input',
      placeholder: 'Apollo email account ID',
      condition: { field: 'operation', value: 'sequence_add' },
      required: true,
    },
    {
      id: 'send_email_from_email_address',
      title: 'Send Email From (Email Address)',
      type: 'short-input',
      placeholder: 'sender@example.com',
      condition: { field: 'operation', value: 'sequence_add' },
      mode: 'advanced',
    },
    {
      id: 'sequence_same_company_in_same_campaign',
      title: 'Allow Same Company in Same Campaign',
      type: 'switch',
      condition: { field: 'operation', value: 'sequence_add' },
      mode: 'advanced',
    },
    {
      id: 'contacts_without_ownership_permission',
      title: 'Add Contacts Without Ownership Permission',
      type: 'switch',
      condition: { field: 'operation', value: 'sequence_add' },
      mode: 'advanced',
    },
    {
      id: 'add_if_in_queue',
      title: 'Add If In Queue',
      type: 'switch',
      condition: { field: 'operation', value: 'sequence_add' },
      mode: 'advanced',
    },
    {
      id: 'contact_verification_skipped',
      title: 'Skip Contact Verification',
      type: 'switch',
      condition: { field: 'operation', value: 'sequence_add' },
      mode: 'advanced',
    },
    {
      id: 'sequence_user_id',
      title: 'Acting User ID',
      type: 'short-input',
      placeholder: 'Apollo user ID',
      condition: { field: 'operation', value: 'sequence_add' },
      mode: 'advanced',
    },
    {
      id: 'sequence_status',
      title: 'Initial Status',
      type: 'dropdown',
      options: [
        { label: 'Active', id: 'active' },
        { label: 'Paused', id: 'paused' },
      ],
      condition: { field: 'operation', value: 'sequence_add' },
      mode: 'advanced',
    },
    {
      id: 'auto_unpause_at',
      title: 'Auto Unpause At',
      type: 'short-input',
      placeholder: 'ISO 8601 (e.g., 2024-12-31T23:59:59Z)',
      condition: { field: 'operation', value: 'sequence_add' },
      mode: 'advanced',
    },

    // Task Create Fields
    {
      id: 'user_id',
      title: 'Assigned User ID',
      type: 'short-input',
      placeholder: 'Apollo user ID',
      condition: { field: 'operation', value: 'task_create' },
      required: true,
    },
    {
      id: 'priority',
      title: 'Priority',
      type: 'dropdown',
      options: [
        { label: 'High', id: 'high' },
        { label: 'Medium', id: 'medium' },
        { label: 'Low', id: 'low' },
      ],
      value: () => 'medium',
      condition: { field: 'operation', value: 'task_create' },
    },
    {
      id: 'type',
      title: 'Task Type',
      type: 'dropdown',
      options: [
        { label: 'Call', id: 'call' },
        { label: 'Outreach Manual Email', id: 'outreach_manual_email' },
        { label: 'LinkedIn — Connect', id: 'linkedin_step_connect' },
        { label: 'LinkedIn — Message', id: 'linkedin_step_message' },
        { label: 'LinkedIn — View Profile', id: 'linkedin_step_view_profile' },
        { label: 'LinkedIn — Interact with Post', id: 'linkedin_step_interact_post' },
        { label: 'Action Item', id: 'action_item' },
      ],
      value: () => 'action_item',
      condition: { field: 'operation', value: 'task_create' },
      required: true,
    },
    {
      id: 'status',
      title: 'Status',
      type: 'dropdown',
      options: [
        { label: 'Scheduled', id: 'scheduled' },
        { label: 'Completed', id: 'completed' },
        { label: 'Skipped', id: 'skipped' },
      ],
      value: () => 'scheduled',
      condition: { field: 'operation', value: 'task_create' },
      required: true,
    },
    {
      id: 'due_at',
      title: 'Due Date',
      type: 'short-input',
      placeholder: 'ISO 8601 (e.g., 2024-12-31T23:59:59Z)',
      condition: { field: 'operation', value: 'task_create' },
      required: true,
      wandConfig: {
        enabled: true,
        prompt: `Generate an ISO 8601 timestamp based on the user's description.
The timestamp should be in the format: YYYY-MM-DDTHH:MM:SSZ (UTC timezone).
Examples:
- "tomorrow at 5pm" -> Calculate tomorrow's date at 17:00:00Z
- "end of day" -> Today's date at 23:59:59Z
- "next week" -> 7 days from now at 17:00:00Z
- "in 3 days" -> 3 days from now at 17:00:00Z

Return ONLY the timestamp string in ISO 8601 format - no explanations, no quotes, no extra text.`,
        placeholder: 'Describe the due date (e.g., "tomorrow at 5pm", "end of week")...',
        generationType: 'timestamp',
      },
    },
    {
      id: 'task_notes',
      title: 'Task Notes',
      type: 'long-input',
      placeholder: 'Notes for the task',
      condition: { field: 'operation', value: 'task_create' },
      mode: 'advanced',
    },

    // Task Search Fields
    {
      id: 'open_factor_names',
      title: 'Open Factor Names',
      type: 'code',
      placeholder: '["task_types"]',
      condition: { field: 'operation', value: 'task_search' },
      mode: 'advanced',
    },

    // Pagination
    {
      id: 'page',
      title: 'Page Number',
      type: 'short-input',
      placeholder: '1',
      condition: {
        field: 'operation',
        value: [
          'people_search',
          'organization_search',
          'contact_search',
          'account_search',
          'opportunity_search',
          'sequence_search',
          'task_search',
        ],
      },
      mode: 'advanced',
    },
    {
      id: 'per_page',
      title: 'Results Per Page',
      type: 'short-input',
      placeholder: '25 (max: 100)',
      condition: {
        field: 'operation',
        value: [
          'people_search',
          'organization_search',
          'contact_search',
          'account_search',
          'opportunity_search',
          'sequence_search',
          'task_search',
        ],
      },
      mode: 'advanced',
    },
  ],
  tools: {
    access: [
      'apollo_people_search',
      'apollo_people_enrich',
      'apollo_people_bulk_enrich',
      'apollo_organization_search',
      'apollo_organization_enrich',
      'apollo_organization_bulk_enrich',
      'apollo_contact_create',
      'apollo_contact_update',
      'apollo_contact_search',
      'apollo_contact_bulk_create',
      'apollo_contact_bulk_update',
      'apollo_account_create',
      'apollo_account_update',
      'apollo_account_search',
      'apollo_account_bulk_create',
      'apollo_account_bulk_update',
      'apollo_opportunity_create',
      'apollo_opportunity_search',
      'apollo_opportunity_get',
      'apollo_opportunity_update',
      'apollo_sequence_search',
      'apollo_sequence_add_contacts',
      'apollo_task_create',
      'apollo_task_search',
      'apollo_email_accounts',
    ],
    config: {
      tool: (params) => {
        switch (params.operation) {
          case 'people_search':
            return 'apollo_people_search'
          case 'people_enrich':
            return 'apollo_people_enrich'
          case 'people_bulk_enrich':
            return 'apollo_people_bulk_enrich'
          case 'organization_search':
            return 'apollo_organization_search'
          case 'organization_enrich':
            return 'apollo_organization_enrich'
          case 'organization_bulk_enrich':
            return 'apollo_organization_bulk_enrich'
          case 'contact_create':
            return 'apollo_contact_create'
          case 'contact_update':
            return 'apollo_contact_update'
          case 'contact_search':
            return 'apollo_contact_search'
          case 'contact_bulk_create':
            return 'apollo_contact_bulk_create'
          case 'contact_bulk_update':
            return 'apollo_contact_bulk_update'
          case 'account_create':
            return 'apollo_account_create'
          case 'account_update':
            return 'apollo_account_update'
          case 'account_search':
            return 'apollo_account_search'
          case 'account_bulk_create':
            return 'apollo_account_bulk_create'
          case 'account_bulk_update':
            return 'apollo_account_bulk_update'
          case 'opportunity_create':
            return 'apollo_opportunity_create'
          case 'opportunity_search':
            return 'apollo_opportunity_search'
          case 'opportunity_get':
            return 'apollo_opportunity_get'
          case 'opportunity_update':
            return 'apollo_opportunity_update'
          case 'sequence_search':
            return 'apollo_sequence_search'
          case 'sequence_add':
            return 'apollo_sequence_add_contacts'
          case 'task_create':
            return 'apollo_task_create'
          case 'task_search':
            return 'apollo_task_search'
          case 'email_accounts':
            return 'apollo_email_accounts'
          default:
            throw new Error(`Invalid Apollo operation: ${params.operation}`)
        }
      },
      params: (params) => {
        const { apiKey, ...rest } = params
        const parsedParams: Record<string, unknown> = { apiKey, ...rest }

        const parseJsonField = (field: string) => {
          const value = (rest as Record<string, unknown>)[field]
          if (typeof value === 'string' && value.trim() !== '') {
            parsedParams[field] = JSON.parse(value)
          }
        }

        try {
          for (const field of [
            'person_titles',
            'person_locations',
            'person_seniorities',
            'organization_names',
            'organization_locations',
            'organization_not_locations',
            'organization_ids',
            'q_organization_domains_list',
            'contact_email_status',
            'organization_num_employees_ranges',
            'q_organization_keyword_tags',
            'contact_stage_ids',
            'contact_label_ids',
            'account_stage_ids',
            'account_label_ids',
            'people',
            'organizations',
            'contacts',
            'accounts',
            'contact_ids',
            'contact_attributes',
            'account_attributes',
            'label_names',
            'sequence_add_label_names',
            'append_label_names',
            'typed_custom_fields',
            'open_factor_names',
          ]) {
            parseJsonField(field)
          }
        } catch (error) {
          const message = getErrorMessage(error)
          throw new Error(`Invalid JSON input: ${message}`)
        }

        const splitBulkUpdateInput = (
          raw: unknown
        ): { ids?: string[]; attributes?: Array<Record<string, unknown>> } => {
          if (!Array.isArray(raw)) return {}
          const ids: string[] = []
          const attributes: Array<Record<string, unknown>> = []
          for (const item of raw) {
            if (typeof item === 'string') {
              ids.push(item)
              continue
            }
            if (item && typeof item === 'object' && 'id' in item) {
              const obj = item as Record<string, unknown>
              const id = obj.id
              if (typeof id !== 'string') continue
              const otherKeys = Object.keys(obj).filter((k) => k !== 'id')
              if (otherKeys.length === 0) {
                ids.push(id)
              } else {
                attributes.push(obj)
              }
            }
          }
          return {
            ids: ids.length > 0 ? ids : undefined,
            attributes: attributes.length > 0 ? attributes : undefined,
          }
        }

        if (params.operation === 'contact_bulk_update') {
          const { ids, attributes } = splitBulkUpdateInput(parsedParams.contacts)
          if (attributes) {
            if (parsedParams.contact_attributes === undefined) {
              parsedParams.contact_attributes = attributes
            }
          } else if (ids && parsedParams.contact_ids === undefined) {
            parsedParams.contact_ids = ids
          }
          parsedParams.contacts = undefined
        }

        if (params.operation === 'account_bulk_update') {
          const { ids, attributes } = splitBulkUpdateInput(parsedParams.accounts)
          if (attributes) {
            if (parsedParams.account_attributes === undefined) {
              parsedParams.account_attributes = attributes
            }
          } else if (ids && parsedParams.account_ids === undefined) {
            parsedParams.account_ids = ids
          }
          parsedParams.accounts = undefined
          if (rest.account_bulk_update_name) {
            parsedParams.name = rest.account_bulk_update_name
          }
          if (rest.account_bulk_update_owner_id) {
            parsedParams.owner_id = rest.account_bulk_update_owner_id
          }
          parsedParams.account_bulk_update_name = undefined
          parsedParams.account_bulk_update_owner_id = undefined
        }

        if (params.operation === 'contact_create') {
          if (rest.contact_run_dedupe !== undefined) {
            parsedParams.run_dedupe = rest.contact_run_dedupe
          }
          parsedParams.contact_run_dedupe = undefined
        }

        if (params.operation === 'account_create' || params.operation === 'account_update') {
          if (rest.account_name) parsedParams.name = rest.account_name
          parsedParams.account_name = undefined
        }

        if (params.operation === 'account_update') {
          parsedParams.account_id = rest.account_id
        }

        if (params.operation === 'sequence_add') {
          if (parsedParams.sequence_add_label_names !== undefined) {
            parsedParams.label_names = parsedParams.sequence_add_label_names
          }
          parsedParams.sequence_add_label_names = undefined
          if (rest.sequence_user_id !== undefined && rest.sequence_user_id !== '') {
            parsedParams.user_id = rest.sequence_user_id
          }
          parsedParams.sequence_user_id = undefined
          if (rest.sequence_status !== undefined && rest.sequence_status !== '') {
            parsedParams.status = rest.sequence_status
          }
          parsedParams.sequence_status = undefined
        }

        if (params.operation === 'task_create') {
          if (rest.task_notes !== undefined) {
            parsedParams.note = rest.task_notes
          }
          parsedParams.task_notes = undefined
        }

        if (
          params.operation === 'opportunity_create' ||
          params.operation === 'opportunity_update'
        ) {
          if (rest.opportunity_name) parsedParams.name = rest.opportunity_name
          parsedParams.opportunity_name = undefined
        }

        if (parsedParams.page) parsedParams.page = Number(parsedParams.page)
        if (parsedParams.per_page) parsedParams.per_page = Number(parsedParams.per_page)

        if (parsedParams.amount !== undefined && parsedParams.amount !== '') {
          parsedParams.amount = String(parsedParams.amount)
        }

        return parsedParams
      },
    },
  },
  inputs: {
    operation: { type: 'string', description: 'Apollo operation to perform' },
  },
  outputs: {
    success: { type: 'boolean', description: 'Whether the operation was successful' },
    output: { type: 'json', description: 'Output data from the Apollo operation' },
  },
}
