import { HubspotIcon } from '@/components/icons'
import { getScopesForService } from '@/lib/oauth/utils'
import type { BlockConfig } from '@/blocks/types'
import { AuthMode, IntegrationType } from '@/blocks/types'
import type { HubSpotResponse } from '@/tools/hubspot/types'
import { getTrigger } from '@/triggers'
import { hubspotAllTriggerOptions } from '@/triggers/hubspot/utils'

export const HubSpotBlock: BlockConfig<HubSpotResponse> = {
  type: 'hubspot',
  name: 'HubSpot',
  description: 'Interact with HubSpot CRM or trigger workflows from HubSpot events',
  authMode: AuthMode.OAuth,
  longDescription:
    'Integrate HubSpot into your workflow. Manage contacts, companies, deals, tickets, and other CRM objects with powerful automation capabilities. Can be used in trigger mode to start workflows when contacts are created, deleted, or updated.',
  docsLink: 'https://docs.sim.ai/tools/hubspot',
  category: 'tools',
  integrationType: IntegrationType.CRM,
  tags: ['marketing', 'sales-engagement', 'customer-support'],
  bgColor: '#FF7A59',
  icon: HubspotIcon,
  subBlocks: [
    {
      id: 'operation',
      title: 'Operation',
      type: 'dropdown',
      options: [
        { label: 'Get Contacts', id: 'get_contacts' },
        { label: 'Create Contact', id: 'create_contact' },
        { label: 'Update Contact', id: 'update_contact' },
        { label: 'Search Contacts', id: 'search_contacts' },
        { label: 'Get Companies', id: 'get_companies' },
        { label: 'Create Company', id: 'create_company' },
        { label: 'Update Company', id: 'update_company' },
        { label: 'Search Companies', id: 'search_companies' },
        { label: 'Get Deals', id: 'get_deals' },
        { label: 'Create Deal', id: 'create_deal' },
        { label: 'Update Deal', id: 'update_deal' },
        { label: 'Search Deals', id: 'search_deals' },
        { label: 'Get Tickets', id: 'get_tickets' },
        { label: 'Create Ticket', id: 'create_ticket' },
        { label: 'Update Ticket', id: 'update_ticket' },
        { label: 'Search Tickets', id: 'search_tickets' },
        { label: 'Get Line Items', id: 'get_line_items' },
        { label: 'Create Line Item', id: 'create_line_item' },
        { label: 'Update Line Item', id: 'update_line_item' },
        { label: 'Get Quotes', id: 'get_quotes' },
        { label: 'Get Appointments', id: 'get_appointments' },
        { label: 'Create Appointment', id: 'create_appointment' },
        { label: 'Update Appointment', id: 'update_appointment' },
        { label: 'Get Carts', id: 'get_carts' },
        { label: 'List Owners', id: 'list_owners' },
        { label: 'Get Marketing Events', id: 'get_marketing_events' },
        { label: 'Get Lists', id: 'get_lists' },
        { label: 'Create List', id: 'create_list' },
        { label: 'Get Users', id: 'get_users' },
      ],
      value: () => 'get_contacts',
    },
    {
      id: 'credential',
      title: 'HubSpot Account',
      type: 'oauth-input',
      canonicalParamId: 'oauthCredential',
      mode: 'basic',
      serviceId: 'hubspot',
      requiredScopes: getScopesForService('hubspot'),
      placeholder: 'Select HubSpot account',
      required: true,
    },
    {
      id: 'manualCredential',
      title: 'HubSpot Account',
      type: 'short-input',
      canonicalParamId: 'oauthCredential',
      mode: 'advanced',
      placeholder: 'Enter credential ID',
      required: true,
    },
    {
      id: 'contactId',
      title: 'Contact ID or Email',
      type: 'short-input',
      placeholder: 'Leave empty to list all contacts',
      condition: { field: 'operation', value: 'get_contacts' },
    },
    {
      id: 'contactId',
      title: 'Contact ID or Email',
      type: 'short-input',
      placeholder: 'Numeric ID, or email (requires ID Property below)',
      condition: { field: 'operation', value: 'update_contact' },
      required: true,
    },
    {
      id: 'companyId',
      title: 'Company ID or Domain',
      type: 'short-input',
      placeholder: 'Leave empty to list all companies',
      condition: { field: 'operation', value: 'get_companies' },
    },
    {
      id: 'companyId',
      title: 'Company ID or Domain',
      type: 'short-input',
      placeholder: 'Numeric ID, or domain (requires ID Property below)',
      condition: { field: 'operation', value: 'update_company' },
      required: true,
    },
    {
      id: 'dealId',
      title: 'Deal ID',
      type: 'short-input',
      placeholder: 'Leave empty to list all deals',
      condition: { field: 'operation', value: 'get_deals' },
    },
    {
      id: 'dealId',
      title: 'Deal ID',
      type: 'short-input',
      placeholder: 'Numeric ID, or custom ID (requires ID Property below)',
      condition: { field: 'operation', value: 'update_deal' },
      required: true,
    },
    {
      id: 'ticketId',
      title: 'Ticket ID',
      type: 'short-input',
      placeholder: 'Leave empty to list all tickets',
      condition: { field: 'operation', value: 'get_tickets' },
    },
    {
      id: 'ticketId',
      title: 'Ticket ID',
      type: 'short-input',
      placeholder: 'Numeric ID, or custom ID (requires ID Property below)',
      condition: { field: 'operation', value: 'update_ticket' },
      required: true,
    },
    {
      id: 'lineItemId',
      title: 'Line Item ID',
      type: 'short-input',
      placeholder: 'Leave empty to list all line items',
      condition: { field: 'operation', value: 'get_line_items' },
    },
    {
      id: 'lineItemId',
      title: 'Line Item ID',
      type: 'short-input',
      placeholder: 'Numeric ID, or custom ID (requires ID Property below)',
      condition: { field: 'operation', value: 'update_line_item' },
      required: true,
    },
    {
      id: 'quoteId',
      title: 'Quote ID',
      type: 'short-input',
      placeholder: 'Leave empty to list all quotes',
      condition: { field: 'operation', value: 'get_quotes' },
    },
    {
      id: 'appointmentId',
      title: 'Appointment ID',
      type: 'short-input',
      placeholder: 'Leave empty to list all appointments',
      condition: { field: 'operation', value: 'get_appointments' },
    },
    {
      id: 'appointmentId',
      title: 'Appointment ID',
      type: 'short-input',
      placeholder: 'Numeric ID, or custom ID (requires ID Property below)',
      condition: { field: 'operation', value: 'update_appointment' },
      required: true,
    },
    {
      id: 'cartId',
      title: 'Cart ID',
      type: 'short-input',
      placeholder: 'Leave empty to list all carts',
      condition: { field: 'operation', value: 'get_carts' },
    },
    {
      id: 'eventId',
      title: 'Marketing Event ID',
      type: 'short-input',
      placeholder: 'Leave empty to list all marketing events',
      condition: { field: 'operation', value: 'get_marketing_events' },
    },
    {
      id: 'listId',
      title: 'List ID',
      type: 'short-input',
      placeholder: 'Leave empty to search all lists',
      condition: { field: 'operation', value: 'get_lists' },
    },
    {
      id: 'listName',
      title: 'List Name',
      type: 'short-input',
      placeholder: 'Name for the new list',
      condition: { field: 'operation', value: 'create_list' },
      required: true,
    },
    {
      id: 'objectTypeId',
      title: 'Object Type ID',
      type: 'short-input',
      placeholder: 'e.g., "0-1" for contacts, "0-2" for companies',
      condition: { field: 'operation', value: 'create_list' },
      required: true,
    },
    {
      id: 'processingType',
      title: 'Processing Type',
      type: 'dropdown',
      options: [
        { label: 'Manual (Static)', id: 'MANUAL' },
        { label: 'Dynamic (Active)', id: 'DYNAMIC' },
      ],
      condition: { field: 'operation', value: 'create_list' },
      required: true,
    },
    {
      id: 'idProperty',
      title: 'ID Property',
      type: 'short-input',
      placeholder: 'Required if using email/domain (e.g., "email" or "domain")',
      condition: {
        field: 'operation',
        value: [
          'get_contacts',
          'update_contact',
          'get_companies',
          'update_company',
          'get_deals',
          'update_deal',
          'get_tickets',
          'update_ticket',
          'get_line_items',
          'update_line_item',
          'get_quotes',
          'get_appointments',
          'update_appointment',
        ],
      },
    },
    {
      id: 'propertiesToSet',
      title: 'Properties',
      type: 'long-input',
      placeholder:
        'JSON object with properties (e.g., {"email": "test@example.com", "firstname": "John"})',
      condition: {
        field: 'operation',
        value: [
          'create_contact',
          'update_contact',
          'create_company',
          'update_company',
          'create_deal',
          'update_deal',
          'create_ticket',
          'update_ticket',
          'create_line_item',
          'update_line_item',
          'create_appointment',
          'update_appointment',
        ],
      },
      wandConfig: {
        enabled: true,
        maintainHistory: true,
        prompt: `You are an expert HubSpot CRM developer. Generate HubSpot property objects as JSON based on the user's request.

### CONTEXT
{context}

### CRITICAL INSTRUCTION
Return ONLY the JSON object with HubSpot properties. Do not include any explanations, markdown formatting, comments, or additional text. Just the raw JSON object that can be used directly in HubSpot API create/update operations.

### HUBSPOT PROPERTIES STRUCTURE
HubSpot properties are defined as a flat JSON object with property names as keys and their values as the corresponding values. Property names must match HubSpot's internal property names (usually lowercase, snake_case or no spaces).

### COMMON CONTACT PROPERTIES
**Standard Properties**:
- **email**: Email address (required for most operations)
- **firstname**: First name
- **lastname**: Last name
- **phone**: Phone number
- **mobilephone**: Mobile phone number
- **company**: Company name
- **jobtitle**: Job title
- **website**: Website URL
- **address**: Street address
- **city**: City
- **state**: State/Region
- **zip**: Postal code
- **country**: Country
- **lifecyclestage**: Lifecycle stage (e.g., "lead", "customer", "subscriber", "opportunity")
- **hs_lead_status**: Lead status (e.g., "NEW", "OPEN", "IN_PROGRESS", "QUALIFIED")

**Additional Properties**:
- **salutation**: Salutation (e.g., "Mr.", "Ms.", "Dr.")
- **degree**: Degree
- **industry**: Industry
- **fax**: Fax number
- **numemployees**: Number of employees (for companies)
- **annualrevenue**: Annual revenue (for companies)

### COMMON COMPANY PROPERTIES
**Standard Properties**:
- **name**: Company name (required)
- **domain**: Company domain (e.g., "example.com")
- **city**: City
- **state**: State/Region
- **zip**: Postal code
- **country**: Country
- **phone**: Phone number
- **industry**: Industry
- **type**: Company type (e.g., "PROSPECT", "PARTNER", "RESELLER", "VENDOR", "OTHER")
- **description**: Company description
- **website**: Website URL
- **numberofemployees**: Number of employees
- **annualrevenue**: Annual revenue

**Additional Properties**:
- **timezone**: Timezone
- **linkedin_company_page**: LinkedIn URL
- **twitterhandle**: Twitter handle
- **facebook_company_page**: Facebook URL
- **founded_year**: Year founded

### EXAMPLES

**Simple Contact**: "Create contact with email john@example.com and name John Doe"
→ {
  "email": "john@example.com",
  "firstname": "John",
  "lastname": "Doe"
}

**Complete Contact**: "Create a lead contact with full details"
→ {
  "email": "jane.smith@acme.com",
  "firstname": "Jane",
  "lastname": "Smith",
  "phone": "+1-555-123-4567",
  "company": "Acme Corp",
  "jobtitle": "Marketing Manager",
  "website": "https://acme.com",
  "city": "San Francisco",
  "state": "California",
  "country": "United States",
  "lifecyclestage": "lead",
  "hs_lead_status": "NEW"
}

**Simple Company**: "Create company Acme Corp with domain acme.com"
→ {
  "name": "Acme Corp",
  "domain": "acme.com"
}

**Complete Company**: "Create a technology company with full details"
→ {
  "name": "TechStart Inc",
  "domain": "techstart.io",
  "industry": "TECHNOLOGY",
  "phone": "+1-555-987-6543",
  "city": "Austin",
  "state": "Texas",
  "country": "United States",
  "website": "https://techstart.io",
  "description": "Innovative software solutions",
  "numberofemployees": 50,
  "annualrevenue": 5000000,
  "type": "PROSPECT"
}

**Update Contact**: "Update contact phone and job title"
→ {
  "phone": "+1-555-999-8888",
  "jobtitle": "Senior Manager"
}

### REMEMBER
Return ONLY the JSON object with properties - no explanations, no markdown, no extra text.`,
        placeholder: 'Describe the properties you want to set...',
        generationType: 'json-object',
      },
    },
    {
      id: 'properties',
      title: 'Properties to Return',
      type: 'short-input',
      placeholder: 'Comma-separated list (e.g., "email,firstname,lastname")',
      mode: 'advanced',
      condition: {
        field: 'operation',
        value: [
          'get_contacts',
          'get_companies',
          'get_deals',
          'get_tickets',
          'get_line_items',
          'get_quotes',
          'get_appointments',
          'get_carts',
        ],
      },
    },
    {
      id: 'associations',
      title: 'Associations',
      type: 'short-input',
      placeholder: 'Comma-separated object types (e.g., "companies,deals")',
      mode: 'advanced',
      condition: {
        field: 'operation',
        value: [
          'get_contacts',
          'get_companies',
          'get_deals',
          'get_tickets',
          'get_line_items',
          'get_quotes',
          'get_appointments',
          'get_carts',
          'create_contact',
          'create_company',
          'create_deal',
          'create_ticket',
          'create_line_item',
          'create_appointment',
        ],
      },
    },
    {
      id: 'limit',
      title: 'Results Per Page',
      type: 'short-input',
      placeholder: 'Max results (list: 100, search: 200)',
      mode: 'advanced',
      condition: {
        field: 'operation',
        value: [
          'get_users',
          'get_contacts',
          'get_companies',
          'get_deals',
          'get_tickets',
          'get_line_items',
          'get_quotes',
          'get_appointments',
          'get_carts',
          'list_owners',
          'get_marketing_events',
          'get_lists',
          'search_contacts',
          'search_companies',
          'search_deals',
          'search_tickets',
        ],
      },
    },
    {
      id: 'after',
      title: 'Pagination Cursor',
      type: 'short-input',
      placeholder: 'Cursor from previous response paging.next.after',
      mode: 'advanced',
      condition: {
        field: 'operation',
        value: [
          'get_contacts',
          'get_companies',
          'get_deals',
          'get_tickets',
          'get_line_items',
          'get_quotes',
          'get_appointments',
          'get_carts',
          'list_owners',
          'get_users',
          'get_marketing_events',
          'get_lists',
          'search_contacts',
          'search_companies',
          'search_deals',
          'search_tickets',
        ],
      },
    },
    {
      id: 'query',
      title: 'Search Query',
      type: 'short-input',
      placeholder: 'Search term (e.g., company name, contact email)',
      condition: {
        field: 'operation',
        value: [
          'search_contacts',
          'search_companies',
          'search_deals',
          'search_tickets',
          'get_lists',
        ],
      },
    },
    {
      id: 'filterGroups',
      title: 'Filter Groups',
      type: 'long-input',
      placeholder:
        'JSON array of filter groups (e.g., [{"filters":[{"propertyName":"email","operator":"EQ","value":"test@example.com"}]}])',
      condition: {
        field: 'operation',
        value: ['search_contacts', 'search_companies', 'search_deals', 'search_tickets'],
      },
      wandConfig: {
        enabled: true,
        maintainHistory: true,
        prompt: `You are an expert HubSpot CRM developer. Generate HubSpot filter groups as JSON arrays based on the user's request.

### CONTEXT
{context}

### CRITICAL INSTRUCTION
Return ONLY the JSON array of filter groups. Do not include any explanations, markdown formatting, comments, or additional text. Just the raw JSON array that can be used directly in HubSpot API search operations.

### HUBSPOT FILTER GROUPS STRUCTURE
Filter groups are arrays of filter objects. Each filter group contains an array of filters. Multiple filter groups are combined with OR logic, while filters within a group are combined with AND logic.

Structure:
[
  {
    "filters": [
      {
        "propertyName": "property_name",
        "operator": "OPERATOR",
        "value": "value"
      }
    ]
  }
]

### FILTER OPERATORS
HubSpot supports the following operators:

**Comparison Operators**:
- **EQ**: Equals - exact match
- **NEQ**: Not equals
- **LT**: Less than (for numbers and dates)
- **LTE**: Less than or equal to
- **GT**: Greater than (for numbers and dates)
- **GTE**: Greater than or equal to
- **BETWEEN**: Between two values (requires "highValue" field)

**String Operators**:
- **CONTAINS_TOKEN**: Contains the token (word)
- **NOT_CONTAINS_TOKEN**: Does not contain the token

**Existence Operators**:
- **HAS_PROPERTY**: Property has any value (value can be "*")
- **NOT_HAS_PROPERTY**: Property has no value (value can be "*")

**Set Operators**:
- **IN**: Value is in the provided list (value is semicolon-separated)
- **NOT_IN**: Value is not in the provided list

### COMMON CONTACT PROPERTIES FOR FILTERING
- **email**: Email address
- **firstname**: First name
- **lastname**: Last name
- **lifecyclestage**: Lifecycle stage (lead, customer, subscriber, opportunity)
- **hs_lead_status**: Lead status (NEW, OPEN, IN_PROGRESS, QUALIFIED)
- **createdate**: Creation date (milliseconds timestamp)
- **lastmodifieddate**: Last modified date
- **phone**: Phone number
- **company**: Company name
- **jobtitle**: Job title

### COMMON COMPANY PROPERTIES FOR FILTERING
- **name**: Company name
- **domain**: Company domain
- **industry**: Industry
- **type**: Company type
- **city**: City
- **state**: State
- **country**: Country
- **numberofemployees**: Number of employees
- **annualrevenue**: Annual revenue
- **createdate**: Creation date

### EXAMPLES

**Simple Equality**: "Find contacts with email john@example.com"
→ [
  {
    "filters": [
      {
        "propertyName": "email",
        "operator": "EQ",
        "value": "john@example.com"
      }
    ]
  }
]

**Multiple Filters (AND)**: "Find lead contacts in San Francisco"
→ [
  {
    "filters": [
      {
        "propertyName": "lifecyclestage",
        "operator": "EQ",
        "value": "lead"
      },
      {
        "propertyName": "city",
        "operator": "EQ",
        "value": "San Francisco"
      }
    ]
  }
]

**Multiple Filter Groups (OR)**: "Find contacts who are either leads or customers"
→ [
  {
    "filters": [
      {
        "propertyName": "lifecyclestage",
        "operator": "EQ",
        "value": "lead"
      }
    ]
  },
  {
    "filters": [
      {
        "propertyName": "lifecyclestage",
        "operator": "EQ",
        "value": "customer"
      }
    ]
  }
]

**Contains Text**: "Find contacts with Gmail addresses"
→ [
  {
    "filters": [
      {
        "propertyName": "email",
        "operator": "CONTAINS_TOKEN",
        "value": "@gmail.com"
      }
    ]
  }
]

**IN Operator**: "Find companies in tech or finance industries"
→ [
  {
    "filters": [
      {
        "propertyName": "industry",
        "operator": "IN",
        "value": "TECHNOLOGY;FINANCE"
      }
    ]
  }
]

**Has Property**: "Find contacts with phone numbers"
→ [
  {
    "filters": [
      {
        "propertyName": "phone",
        "operator": "HAS_PROPERTY",
        "value": "*"
      }
    ]
  }
]

**Range Filter**: "Find companies with 10 to 100 employees"
→ [
  {
    "filters": [
      {
        "propertyName": "numberofemployees",
        "operator": "GTE",
        "value": "10"
      },
      {
        "propertyName": "numberofemployees",
        "operator": "LTE",
        "value": "100"
      }
    ]
  }
]

### REMEMBER
Return ONLY the JSON array of filter groups - no explanations, no markdown, no extra text.`,
        placeholder: 'Describe the filters you want to apply...',
        generationType: 'json-object',
      },
    },
    {
      id: 'sorts',
      title: 'Sort Order',
      type: 'long-input',
      placeholder:
        'JSON array of sort objects (e.g., [{"propertyName":"createdate","direction":"DESCENDING"}])',
      mode: 'advanced',
      condition: {
        field: 'operation',
        value: ['search_contacts', 'search_companies', 'search_deals', 'search_tickets'],
      },
      wandConfig: {
        enabled: true,
        maintainHistory: true,
        prompt: `You are an expert HubSpot CRM developer. Generate HubSpot sort arrays as JSON based on the user's request.

### CONTEXT
{context}

### CRITICAL INSTRUCTION
Return ONLY the JSON array of sort objects. Do not include any explanations, markdown formatting, comments, or additional text. Just the raw JSON array that can be used directly in HubSpot API search operations.

### HUBSPOT SORT STRUCTURE
Sorts are defined as an array of objects, each containing a property name and a direction. Results will be sorted by the first sort object, then by the second if values are equal, and so on.

Structure:
[
  {
    "propertyName": "property_name",
    "direction": "ASCENDING" | "DESCENDING"
  }
]

### SORT DIRECTIONS
- **ASCENDING**: Sort from lowest to highest (A-Z, 0-9, oldest to newest)
- **DESCENDING**: Sort from highest to lowest (Z-A, 9-0, newest to oldest)

### COMMON SORTABLE PROPERTIES

**Contact Properties**:
- **createdate**: Creation date (when the contact was created)
- **lastmodifieddate**: Last modified date (when the contact was last updated)
- **firstname**: First name (alphabetical)
- **lastname**: Last name (alphabetical)
- **email**: Email address (alphabetical)
- **lifecyclestage**: Lifecycle stage
- **hs_lead_status**: Lead status
- **company**: Company name (alphabetical)
- **jobtitle**: Job title (alphabetical)
- **phone**: Phone number

**Company Properties**:
- **createdate**: Creation date
- **lastmodifieddate**: Last modified date
- **name**: Company name (alphabetical)
- **domain**: Domain (alphabetical)
- **industry**: Industry
- **city**: City (alphabetical)
- **state**: State (alphabetical)
- **numberofemployees**: Number of employees (numeric)
- **annualrevenue**: Annual revenue (numeric)

### EXAMPLES

**Simple Sort**: "Sort by creation date, newest first"
→ [
  {
    "propertyName": "createdate",
    "direction": "DESCENDING"
  }
]

**Alphabetical Sort**: "Sort contacts by last name A to Z"
→ [
  {
    "propertyName": "lastname",
    "direction": "ASCENDING"
  }
]

**Multiple Sorts**: "Sort by lifecycle stage, then by last name"
→ [
  {
    "propertyName": "lifecyclestage",
    "direction": "ASCENDING"
  },
  {
    "propertyName": "lastname",
    "direction": "ASCENDING"
  }
]

**Numeric Sort**: "Sort companies by revenue, highest first"
→ [
  {
    "propertyName": "annualrevenue",
    "direction": "DESCENDING"
  }
]

**Recent First**: "Show most recently updated contacts first"
→ [
  {
    "propertyName": "lastmodifieddate",
    "direction": "DESCENDING"
  }
]

**Name and Date**: "Sort by company name, then by creation date newest first"
→ [
  {
    "propertyName": "name",
    "direction": "ASCENDING"
  },
  {
    "propertyName": "createdate",
    "direction": "DESCENDING"
  }
]

### REMEMBER
Return ONLY the JSON array of sort objects - no explanations, no markdown, no extra text.`,
        placeholder: 'Describe how you want to sort the results...',
        generationType: 'json-object',
      },
    },
    {
      id: 'searchProperties',
      title: 'Properties to Return',
      type: 'long-input',
      placeholder: 'JSON array of properties (e.g., ["email","firstname","lastname"])',
      mode: 'advanced',
      condition: {
        field: 'operation',
        value: ['search_contacts', 'search_companies', 'search_deals', 'search_tickets'],
      },
      wandConfig: {
        enabled: true,
        maintainHistory: true,
        prompt: `You are an expert HubSpot CRM developer. Generate HubSpot property arrays as JSON based on the user's request.

### CONTEXT
{context}

### CRITICAL INSTRUCTION
Return ONLY the JSON array of property names. Do not include any explanations, markdown formatting, comments, or additional text. Just the raw JSON array of strings that can be used directly in HubSpot API search operations.

### HUBSPOT PROPERTIES ARRAY STRUCTURE
Properties to return are defined as a simple array of property name strings. These specify which fields should be included in the search results.

Structure:
["property1", "property2", "property3"]

### COMMON CONTACT PROPERTIES

**Basic Information**:
- **email**: Email address
- **firstname**: First name
- **lastname**: Last name
- **phone**: Phone number
- **mobilephone**: Mobile phone number

**Professional Information**:
- **company**: Company name
- **jobtitle**: Job title
- **industry**: Industry
- **department**: Department
- **seniority**: Seniority level

**Address Information**:
- **address**: Street address
- **city**: City
- **state**: State/Region
- **zip**: Postal code
- **country**: Country

**CRM Information**:
- **lifecyclestage**: Lifecycle stage
- **hs_lead_status**: Lead status
- **hubspot_owner_id**: Owner ID
- **hs_analytics_source**: Original source

**Dates**:
- **createdate**: Creation date
- **lastmodifieddate**: Last modified date
- **hs_lifecyclestage_lead_date**: Lead date
- **hs_lifecyclestage_customer_date**: Customer date

**Website & Social**:
- **website**: Website URL
- **linkedin_url**: LinkedIn profile URL
- **twitterhandle**: Twitter handle

### COMMON COMPANY PROPERTIES

**Basic Information**:
- **name**: Company name
- **domain**: Company domain
- **phone**: Phone number
- **industry**: Industry
- **type**: Company type

**Address Information**:
- **city**: City
- **state**: State/Region
- **zip**: Postal code
- **country**: Country
- **address**: Street address

**Business Information**:
- **numberofemployees**: Number of employees
- **annualrevenue**: Annual revenue
- **founded_year**: Year founded
- **description**: Company description

**Website & Social**:
- **website**: Website URL
- **linkedin_company_page**: LinkedIn company page
- **twitterhandle**: Twitter handle
- **facebook_company_page**: Facebook page

**CRM Information**:
- **hubspot_owner_id**: Owner ID
- **createdate**: Creation date
- **lastmodifieddate**: Last modified date
- **hs_lastmodifieddate**: Last modified date (detailed)

### EXAMPLES

**Basic Contact Fields**: "Return email, name, and phone"
→ ["email", "firstname", "lastname", "phone"]

**Complete Contact Profile**: "Return all contact details"
→ ["email", "firstname", "lastname", "phone", "mobilephone", "company", "jobtitle", "address", "city", "state", "zip", "country", "lifecyclestage", "hs_lead_status", "createdate"]

**Business Contact Info**: "Return professional information"
→ ["email", "firstname", "lastname", "company", "jobtitle", "phone", "industry"]

**Basic Company Fields**: "Return company name, domain, and industry"
→ ["name", "domain", "industry"]

**Complete Company Profile**: "Return all company information"
→ ["name", "domain", "industry", "phone", "city", "state", "country", "numberofemployees", "annualrevenue", "website", "description", "type", "createdate"]

**Contact with Dates**: "Return contact info with timestamps"
→ ["email", "firstname", "lastname", "createdate", "lastmodifieddate", "lifecyclestage"]

**Company Financial Info**: "Return company size and revenue"
→ ["name", "domain", "numberofemployees", "annualrevenue", "industry"]

**Social Media Properties**: "Return social media links"
→ ["email", "firstname", "lastname", "linkedin_url", "twitterhandle"]

**CRM Status Fields**: "Return lifecycle and owner information"
→ ["email", "firstname", "lastname", "lifecyclestage", "hs_lead_status", "hubspot_owner_id"]

### REMEMBER
Return ONLY the JSON array of property names - no explanations, no markdown, no extra text.`,
        placeholder: 'Describe which properties you want to return...',
        generationType: 'json-object',
      },
    },
    {
      id: 'selectedTriggerId',
      title: 'Trigger Type',
      type: 'dropdown',
      mode: 'trigger',
      options: hubspotAllTriggerOptions,
      value: () => 'hubspot_contact_created',
      required: true,
    },
    ...getTrigger('hubspot_contact_created').subBlocks.slice(1),
    ...getTrigger('hubspot_contact_deleted').subBlocks.slice(1),
    ...getTrigger('hubspot_contact_merged').subBlocks.slice(1),
    ...getTrigger('hubspot_contact_privacy_deleted').subBlocks.slice(1),
    ...getTrigger('hubspot_contact_property_changed').subBlocks.slice(1),
    ...getTrigger('hubspot_contact_restored').subBlocks.slice(1),
    ...getTrigger('hubspot_company_created').subBlocks.slice(1),
    ...getTrigger('hubspot_company_deleted').subBlocks.slice(1),
    ...getTrigger('hubspot_company_merged').subBlocks.slice(1),
    ...getTrigger('hubspot_company_property_changed').subBlocks.slice(1),
    ...getTrigger('hubspot_company_restored').subBlocks.slice(1),
    ...getTrigger('hubspot_conversation_creation').subBlocks.slice(1),
    ...getTrigger('hubspot_conversation_deletion').subBlocks.slice(1),
    ...getTrigger('hubspot_conversation_new_message').subBlocks.slice(1),
    ...getTrigger('hubspot_conversation_privacy_deletion').subBlocks.slice(1),
    ...getTrigger('hubspot_conversation_property_changed').subBlocks.slice(1),
    ...getTrigger('hubspot_deal_created').subBlocks.slice(1),
    ...getTrigger('hubspot_deal_deleted').subBlocks.slice(1),
    ...getTrigger('hubspot_deal_merged').subBlocks.slice(1),
    ...getTrigger('hubspot_deal_property_changed').subBlocks.slice(1),
    ...getTrigger('hubspot_deal_restored').subBlocks.slice(1),
    ...getTrigger('hubspot_ticket_created').subBlocks.slice(1),
    ...getTrigger('hubspot_ticket_deleted').subBlocks.slice(1),
    ...getTrigger('hubspot_ticket_merged').subBlocks.slice(1),
    ...getTrigger('hubspot_ticket_property_changed').subBlocks.slice(1),
    ...getTrigger('hubspot_ticket_restored').subBlocks.slice(1),
    ...getTrigger('hubspot_webhook').subBlocks.slice(1),
  ],
  tools: {
    access: [
      'hubspot_get_users',
      'hubspot_list_contacts',
      'hubspot_get_contact',
      'hubspot_create_contact',
      'hubspot_update_contact',
      'hubspot_search_contacts',
      'hubspot_list_companies',
      'hubspot_get_company',
      'hubspot_create_company',
      'hubspot_update_company',
      'hubspot_search_companies',
      'hubspot_list_deals',
      'hubspot_get_deal',
      'hubspot_create_deal',
      'hubspot_update_deal',
      'hubspot_search_deals',
      'hubspot_list_tickets',
      'hubspot_get_ticket',
      'hubspot_create_ticket',
      'hubspot_update_ticket',
      'hubspot_search_tickets',
      'hubspot_list_line_items',
      'hubspot_get_line_item',
      'hubspot_create_line_item',
      'hubspot_update_line_item',
      'hubspot_list_quotes',
      'hubspot_get_quote',
      'hubspot_list_appointments',
      'hubspot_get_appointment',
      'hubspot_create_appointment',
      'hubspot_update_appointment',
      'hubspot_list_carts',
      'hubspot_get_cart',
      'hubspot_list_owners',
      'hubspot_list_marketing_events',
      'hubspot_get_marketing_event',
      'hubspot_list_lists',
      'hubspot_get_list',
      'hubspot_create_list',
    ],
    config: {
      tool: (params) => {
        switch (params.operation) {
          case 'get_users':
            return 'hubspot_get_users'
          case 'get_contacts':
            return params.contactId ? 'hubspot_get_contact' : 'hubspot_list_contacts'
          case 'create_contact':
            return 'hubspot_create_contact'
          case 'update_contact':
            return 'hubspot_update_contact'
          case 'search_contacts':
            return 'hubspot_search_contacts'
          case 'get_companies':
            return params.companyId ? 'hubspot_get_company' : 'hubspot_list_companies'
          case 'create_company':
            return 'hubspot_create_company'
          case 'update_company':
            return 'hubspot_update_company'
          case 'search_companies':
            return 'hubspot_search_companies'
          case 'get_deals':
            return params.dealId ? 'hubspot_get_deal' : 'hubspot_list_deals'
          case 'create_deal':
            return 'hubspot_create_deal'
          case 'update_deal':
            return 'hubspot_update_deal'
          case 'search_deals':
            return 'hubspot_search_deals'
          case 'get_tickets':
            return params.ticketId ? 'hubspot_get_ticket' : 'hubspot_list_tickets'
          case 'create_ticket':
            return 'hubspot_create_ticket'
          case 'update_ticket':
            return 'hubspot_update_ticket'
          case 'search_tickets':
            return 'hubspot_search_tickets'
          case 'get_line_items':
            return params.lineItemId ? 'hubspot_get_line_item' : 'hubspot_list_line_items'
          case 'create_line_item':
            return 'hubspot_create_line_item'
          case 'update_line_item':
            return 'hubspot_update_line_item'
          case 'get_quotes':
            return params.quoteId ? 'hubspot_get_quote' : 'hubspot_list_quotes'
          case 'get_appointments':
            return params.appointmentId ? 'hubspot_get_appointment' : 'hubspot_list_appointments'
          case 'create_appointment':
            return 'hubspot_create_appointment'
          case 'update_appointment':
            return 'hubspot_update_appointment'
          case 'get_carts':
            return params.cartId ? 'hubspot_get_cart' : 'hubspot_list_carts'
          case 'list_owners':
            return 'hubspot_list_owners'
          case 'get_marketing_events':
            return params.eventId ? 'hubspot_get_marketing_event' : 'hubspot_list_marketing_events'
          case 'get_lists':
            return params.listId ? 'hubspot_get_list' : 'hubspot_list_lists'
          case 'create_list':
            return 'hubspot_create_list'
          default:
            throw new Error(`Unknown operation: ${params.operation}`)
        }
      },
      params: (params) => {
        const {
          oauthCredential,
          operation,
          propertiesToSet,
          properties,
          searchProperties,
          filterGroups,
          sorts,
          associations,
          listName,
          ...rest
        } = params

        const cleanParams: Record<string, any> = {
          oauthCredential,
        }

        const createUpdateOps = [
          'create_contact',
          'update_contact',
          'create_company',
          'update_company',
          'create_deal',
          'update_deal',
          'create_ticket',
          'update_ticket',
          'create_line_item',
          'update_line_item',
          'create_appointment',
          'update_appointment',
        ]
        if (propertiesToSet && createUpdateOps.includes(operation as string)) {
          cleanParams.properties = propertiesToSet
        }

        const getListOps = [
          'get_contacts',
          'get_companies',
          'get_deals',
          'get_tickets',
          'get_line_items',
          'get_quotes',
          'get_appointments',
          'get_carts',
        ]
        if (properties && !searchProperties && getListOps.includes(operation as string)) {
          cleanParams.properties = properties
        }

        const searchOps = ['search_contacts', 'search_companies', 'search_deals', 'search_tickets']
        if (searchProperties && searchOps.includes(operation as string)) {
          cleanParams.properties = searchProperties
        }

        if (filterGroups && searchOps.includes(operation as string)) {
          cleanParams.filterGroups = filterGroups
        }

        if (sorts && searchOps.includes(operation as string)) {
          cleanParams.sorts = sorts
        }

        const associationOps = [
          ...getListOps,
          'create_contact',
          'create_company',
          'create_deal',
          'create_ticket',
          'create_line_item',
          'create_appointment',
        ]
        if (associations && associationOps.includes(operation as string)) {
          cleanParams.associations = associations
        }

        if (listName && operation === 'create_list') {
          cleanParams.name = listName
        }

        if (operation === 'get_lists') {
          if (rest.limit) {
            cleanParams.count = rest.limit
            rest.limit = undefined
          }
          if (rest.after) {
            cleanParams.offset = rest.after
            rest.after = undefined
          }
        }

        const excludeKeys = [
          'propertiesToSet',
          'properties',
          'searchProperties',
          'filterGroups',
          'sorts',
          'associations',
          'listName',
        ]
        Object.entries(rest).forEach(([key, value]) => {
          if (value !== undefined && value !== null && value !== '' && !excludeKeys.includes(key)) {
            cleanParams[key] = value
          }
        })

        return cleanParams
      },
    },
  },
  inputs: {
    operation: { type: 'string', description: 'Operation to perform' },
    oauthCredential: { type: 'string', description: 'HubSpot access token' },
    contactId: { type: 'string', description: 'Contact ID or email' },
    companyId: { type: 'string', description: 'Company ID or domain' },
    dealId: { type: 'string', description: 'Deal ID' },
    ticketId: { type: 'string', description: 'Ticket ID' },
    lineItemId: { type: 'string', description: 'Line item ID' },
    quoteId: { type: 'string', description: 'Quote ID' },
    appointmentId: { type: 'string', description: 'Appointment ID' },
    cartId: { type: 'string', description: 'Cart ID' },
    eventId: { type: 'string', description: 'Marketing event ID' },
    listId: { type: 'string', description: 'List ID' },
    idProperty: { type: 'string', description: 'Property name to use as unique identifier' },
    propertiesToSet: { type: 'json', description: 'Properties to create/update (JSON object)' },
    properties: {
      type: 'string',
      description: 'Comma-separated properties to return (for list/get)',
    },
    associations: { type: 'string', description: 'Comma-separated object types for associations' },
    limit: { type: 'string', description: 'Maximum results per page' },
    after: { type: 'string', description: 'Pagination cursor' },
    query: { type: 'string', description: 'Search query string' },
    filterGroups: { type: 'json', description: 'Filter groups for search (JSON array)' },
    sorts: { type: 'json', description: 'Sort order (JSON array of strings or objects)' },
    searchProperties: { type: 'json', description: 'Properties to return in search (JSON array)' },
    listName: { type: 'string', description: 'Name for new list' },
    objectTypeId: { type: 'string', description: 'Object type ID for list' },
    processingType: { type: 'string', description: 'List processing type (MANUAL or DYNAMIC)' },
  },
  outputs: {
    users: { type: 'json', description: 'Array of user objects' },
    contacts: { type: 'json', description: 'Array of contact objects' },
    contact: { type: 'json', description: 'Single contact object' },
    companies: { type: 'json', description: 'Array of company objects' },
    company: { type: 'json', description: 'Single company object' },
    deals: { type: 'json', description: 'Array of deal objects' },
    deal: { type: 'json', description: 'Single deal object' },
    tickets: { type: 'json', description: 'Array of ticket objects' },
    ticket: { type: 'json', description: 'Single ticket object' },
    lineItems: { type: 'json', description: 'Array of line item objects' },
    lineItem: { type: 'json', description: 'Single line item object' },
    quotes: { type: 'json', description: 'Array of quote objects' },
    quote: { type: 'json', description: 'Single quote object' },
    appointments: { type: 'json', description: 'Array of appointment objects' },
    appointment: { type: 'json', description: 'Single appointment object' },
    carts: { type: 'json', description: 'Array of cart objects' },
    cart: { type: 'json', description: 'Single cart object' },
    owners: { type: 'json', description: 'Array of owner objects' },
    events: { type: 'json', description: 'Array of marketing event objects' },
    event: { type: 'json', description: 'Single marketing event object' },
    lists: { type: 'json', description: 'Array of list objects' },
    list: { type: 'json', description: 'Single list object' },
    total: { type: 'number', description: 'Total number of matching results (for search)' },
    paging: { type: 'json', description: 'Pagination info with next/prev cursors' },
    metadata: { type: 'json', description: 'Operation metadata' },
    success: { type: 'boolean', description: 'Operation success status' },
    payload: {
      type: 'json',
      description: 'Full webhook payload array from HubSpot containing event details',
    },
    provider: {
      type: 'string',
      description: 'Provider name (hubspot)',
    },
    providerConfig: {
      appId: {
        type: 'string',
        description: 'HubSpot App ID',
      },
      clientId: {
        type: 'string',
        description: 'HubSpot Client ID',
      },
      triggerId: {
        type: 'string',
        description: 'Trigger ID (e.g., hubspot_company_created)',
      },
      clientSecret: {
        type: 'string',
        description: 'HubSpot Client Secret',
      },
      developerApiKey: {
        type: 'string',
        description: 'HubSpot Developer API Key',
      },
      curlSetWebhookUrl: {
        type: 'string',
        description: 'curl command to set webhook URL',
      },
      curlCreateSubscription: {
        type: 'string',
        description: 'curl command to create subscription',
      },
      webhookUrlDisplay: {
        type: 'string',
        description: 'Webhook URL display value',
      },
      propertyName: {
        type: 'string',
        description: 'Optional property name filter (for property change triggers)',
      },
    },
  } as any,
  triggerAllowed: true,
  triggers: {
    enabled: true,
    available: [
      'hubspot_contact_created',
      'hubspot_contact_deleted',
      'hubspot_contact_merged',
      'hubspot_contact_privacy_deleted',
      'hubspot_contact_property_changed',
      'hubspot_contact_restored',
      'hubspot_company_created',
      'hubspot_company_deleted',
      'hubspot_company_merged',
      'hubspot_company_property_changed',
      'hubspot_company_restored',
      'hubspot_conversation_creation',
      'hubspot_conversation_deletion',
      'hubspot_conversation_new_message',
      'hubspot_conversation_privacy_deletion',
      'hubspot_conversation_property_changed',
      'hubspot_deal_created',
      'hubspot_deal_deleted',
      'hubspot_deal_merged',
      'hubspot_deal_property_changed',
      'hubspot_deal_restored',
      'hubspot_ticket_created',
      'hubspot_ticket_deleted',
      'hubspot_ticket_merged',
      'hubspot_ticket_property_changed',
      'hubspot_ticket_restored',
      'hubspot_webhook',
    ],
  },
}
