import { createAction, Property } from '@activepieces/pieces-framework';
import { serpApiAuth } from '../auth';
import { SerpApiClient } from '../services/serp-api-client';
import { GoogleTrendsSearchConfig, SerpApiEngine } from '../types';

export const googleTrendsSearch = createAction({
  auth: serpApiAuth,
  name: 'google_trends_search',
  displayName: 'Google Trends Search',
  description: 'Discover trending keywords over time to inform content strategy and market research with geographic insights.',
  audience: 'both',
  aiMetadata: { description: 'Queries Google Trends via SerpApi for interest in a keyword over time and across regions. Use to gauge a topic\'s popularity trajectory, compare geographic interest, or find related/rising queries for content and market research. Read-only and idempotent; requires the query, a SerpApi API key, and a chosen data type (interest over time, by region, related topics, or related queries).', idempotent: true },

  props: {
    query: Property.ShortText({
      displayName: 'Search Query',
      description: 'The keyword or topic to analyze trends for (e.g., "artificial intelligence", "cryptocurrency", "climate change")',
      required: true,
    }),

    hl: Property.StaticDropdown({
      displayName: 'Host Language (hl)',
      description: 'Language for search results',
      required: false,
      defaultValue: 'en',
      options: {
        options: [
          { label: 'English', value: 'en' },
          { label: 'Afrikaans', value: 'af' },
          { label: 'Akan', value: 'ak' },
          { label: 'Albanian', value: 'sq' },
          { label: 'Amharic', value: 'am' },
          { label: 'Arabic', value: 'ar' },
          { label: 'Armenian', value: 'hy' },
          { label: 'Azerbaijani', value: 'az' },
          { label: 'Basque', value: 'eu' },
          { label: 'Belarusian', value: 'be' },
          { label: 'Bemba', value: 'bem' },
          { label: 'Bengali', value: 'bn' },
          { label: 'Bihari', value: 'bh' },
          { label: 'Bosnian', value: 'bs' },
          { label: 'Breton', value: 'br' },
          { label: 'Bulgarian', value: 'bg' },
          { label: 'Bhutanese', value: 'bt' },
          { label: 'Cambodian', value: 'km' },
          { label: 'Catalan', value: 'ca' },
          { label: 'Cherokee', value: 'chr' },
          { label: 'Chichewa', value: 'ny' },
          { label: 'Chinese (Simplified)', value: 'zh-cn' },
          { label: 'Chinese (Traditional)', value: 'zh-tw' },
          { label: 'Corsican', value: 'co' },
          { label: 'Croatian', value: 'hr' },
          { label: 'Czech', value: 'cs' },
          { label: 'Danish', value: 'da' },
          { label: 'Dutch', value: 'nl' },
          { label: 'Esperanto', value: 'eo' },
          { label: 'Estonian', value: 'et' },
          { label: 'Ewe', value: 'ee' },
          { label: 'Faroese', value: 'fo' },
          { label: 'Filipino', value: 'tl' },
          { label: 'Finnish', value: 'fi' },
          { label: 'French', value: 'fr' },
          { label: 'Frisian', value: 'fy' },
          { label: 'Ga', value: 'gaa' },
          { label: 'Galician', value: 'gl' },
          { label: 'Georgian', value: 'ka' },
          { label: 'German', value: 'de' },
          { label: 'Greek', value: 'el' },
          { label: 'Greenlandic', value: 'kl' },
          { label: 'Guarani', value: 'gn' },
          { label: 'Gujarati', value: 'gu' },
          { label: 'Haitian Creole', value: 'ht' },
          { label: 'Hausa', value: 'ha' },
          { label: 'Hawaiian', value: 'haw' },
          { label: 'Hebrew', value: 'he' },
          { label: 'Hindi', value: 'hi' },
          { label: 'Hungarian', value: 'hu' },
          { label: 'Icelandic', value: 'is' },
          { label: 'Igbo', value: 'ig' },
          { label: 'Indonesian', value: 'id' },
          { label: 'Interlingua', value: 'ia' },
          { label: 'Irish', value: 'ga' },
          { label: 'Italian', value: 'it' },
          { label: 'Japanese', value: 'ja' },
          { label: 'Javanese', value: 'jw' },
          { label: 'Kannada', value: 'kn' },
          { label: 'Kazakh', value: 'kk' },
          { label: 'Kinyarwanda', value: 'rw' },
          { label: 'Kirundi', value: 'rn' },
          { label: 'Kongo', value: 'kg' },
          { label: 'Korean', value: 'ko' },
          { label: 'Krio (Sierra Leone)', value: 'kri' },
          { label: 'Kurdish', value: 'ku' },
          { label: 'Kurdish (Soranî)', value: 'ckb' },
          { label: 'Kyrgyz', value: 'ky' },
          { label: 'Laothian', value: 'lo' },
          { label: 'Latin', value: 'la' },
          { label: 'Latvian', value: 'lv' },
          { label: 'Lingala', value: 'ln' },
          { label: 'Lithuanian', value: 'lt' },
          { label: 'Lozi', value: 'loz' },
          { label: 'Luganda', value: 'lg' },
          { label: 'Luo', value: 'ach' },
          { label: 'Macedonian', value: 'mk' },
          { label: 'Malagasy', value: 'mg' },
          { label: 'Malay', value: 'ms' },
          { label: 'Malayalam', value: 'ml' },
          { label: 'Maltese', value: 'mt' },
          { label: 'Maldives', value: 'mv' },
          { label: 'Maori', value: 'mi' },
          { label: 'Marathi', value: 'mr' },
          { label: 'Mauritian Creole', value: 'mfe' },
          { label: 'Moldavian', value: 'mo' },
          { label: 'Mongolian', value: 'mn' },
          { label: 'Montenegrin', value: 'sr-me' },
          { label: 'Myanmar', value: 'my' },
          { label: 'Nepali', value: 'ne' },
          { label: 'Nigerian Pidgin', value: 'pcm' },
          { label: 'Northern Sotho', value: 'nso' },
          { label: 'Norwegian', value: 'no' },
          { label: 'Norwegian (Nynorsk)', value: 'nn' },
          { label: 'Occitan', value: 'oc' },
          { label: 'Oriya', value: 'or' },
          { label: 'Oromo', value: 'om' },
          { label: 'Pashto', value: 'ps' },
          { label: 'Persian', value: 'fa' },
          { label: 'Polish', value: 'pl' },
          { label: 'Portuguese', value: 'pt' },
          { label: 'Portuguese (Brazil)', value: 'pt-br' },
          { label: 'Portuguese (Portugal)', value: 'pt-pt' },
          { label: 'Punjabi', value: 'pa' },
          { label: 'Quechua', value: 'qu' },
          { label: 'Romanian', value: 'ro' },
          { label: 'Romansh', value: 'rm' },
          { label: 'Runyakitara', value: 'nyn' },
          { label: 'Russian', value: 'ru' },
          { label: 'Samoa', value: 'ws' },
          { label: 'Scots Gaelic', value: 'gd' },
          { label: 'Serbian', value: 'sr' },
          { label: 'Serbo-Croatian', value: 'sh' },
          { label: 'Sesotho', value: 'st' },
          { label: 'Setswana', value: 'tn' },
          { label: 'Seychellois Creole', value: 'crs' },
          { label: 'Shona', value: 'sn' },
          { label: 'Sindhi', value: 'sd' },
          { label: 'Sinhalese', value: 'si' },
          { label: 'Slovak', value: 'sk' },
          { label: 'Slovenian', value: 'sl' },
          { label: 'Somali', value: 'so' },
          { label: 'Spanish', value: 'es' },
          { label: 'Spanish (Latin American)', value: 'es-419' },
          { label: 'Sundanese', value: 'su' },
          { label: 'Swahili', value: 'sw' },
          { label: 'Swedish', value: 'sv' },
          { label: 'Tajik', value: 'tg' },
          { label: 'Tamil', value: 'ta' },
          { label: 'Tatar', value: 'tt' },
          { label: 'Telugu', value: 'te' },
          { label: 'Thai', value: 'th' },
          { label: 'Tigrinya', value: 'ti' },
          { label: 'Tonga', value: 'to' },
          { label: 'Tshiluba', value: 'lua' },
          { label: 'Tumbuka', value: 'tum' },
          { label: 'Turkish', value: 'tr' },
          { label: 'Turkmen', value: 'tk' },
          { label: 'Twi', value: 'tw' },
          { label: 'Uighur', value: 'ug' },
          { label: 'Ukrainian', value: 'uk' },
          { label: 'Urdu', value: 'ur' },
          { label: 'Uzbek', value: 'uz' },
          { label: 'Vanuatu', value: 'vu' },
          { label: 'Vietnamese', value: 'vi' },
          { label: 'Welsh', value: 'cy' },
          { label: 'Wolof', value: 'wo' },
          { label: 'Xhosa', value: 'xh' },
          { label: 'Yiddish', value: 'yi' },
          { label: 'Yoruba', value: 'yo' },
          { label: 'Zulu', value: 'zu' },
        ],
      },
    }),

    geo: Property.StaticDropdown({
      displayName: 'Geographic Location',
      description: 'Location from where you want the search to originate.',
      required: false,
      defaultValue: '',
      options: {
        options: [
          { label: 'Worldwide', value: '' },
          { label: 'Afghanistan', value: 'AF' },
          { label: 'Albania', value: 'AL' },
          { label: 'Algeria', value: 'DZ' },
          { label: 'American Samoa', value: 'AS' },
          { label: 'Andorra', value: 'AD' },
          { label: 'Angola', value: 'AO' },
          { label: 'Anguilla', value: 'AI' },
          { label: 'Antarctica', value: 'AQ' },
          { label: 'Antigua & Barbuda', value: 'AG' },
          { label: 'Argentina', value: 'AR' },
          { label: 'Armenia', value: 'AM' },
          { label: 'Aruba', value: 'AW' },
          { label: 'Australia', value: 'AU' },
          { label: 'Austria', value: 'AT' },
          { label: 'Azerbaijan', value: 'AZ' },
          { label: 'Bahamas', value: 'BS' },
          { label: 'Bahrain', value: 'BH' },
          { label: 'Bangladesh', value: 'BD' },
          { label: 'Barbados', value: 'BB' },
          { label: 'Belarus', value: 'BY' },
          { label: 'Belgium', value: 'BE' },
          { label: 'Belize', value: 'BZ' },
          { label: 'Benin', value: 'BJ' },
          { label: 'Bermuda', value: 'BM' },
          { label: 'Bhutan', value: 'BT' },
          { label: 'Bolivia', value: 'BO' },
          { label: 'Bosnia & Herzegovina', value: 'BA' },
          { label: 'Botswana', value: 'BW' },
          { label: 'Brazil', value: 'BR' },
          { label: 'British Virgin Islands', value: 'VG' },
          { label: 'Brunei', value: 'BN' },
          { label: 'Bulgaria', value: 'BG' },
          { label: 'Burkina Faso', value: 'BF' },
          { label: 'Burundi', value: 'BI' },
          { label: 'Cambodia', value: 'KH' },
          { label: 'Cameroon', value: 'CM' },
          { label: 'Canada', value: 'CA' },
          { label: 'Cape Verde', value: 'CV' },
          { label: 'Cayman Islands', value: 'KY' },
          { label: 'Central African Republic', value: 'CF' },
          { label: 'Chad', value: 'TD' },
          { label: 'Chile', value: 'CL' },
          { label: 'China', value: 'CN' },
          { label: 'Colombia', value: 'CO' },
          { label: 'Comoros', value: 'KM' },
          { label: 'Congo - Brazzaville', value: 'CG' },
          { label: 'Congo - Kinshasa', value: 'CD' },
          { label: 'Cook Islands', value: 'CK' },
          { label: 'Costa Rica', value: 'CR' },
          { label: 'Croatia', value: 'HR' },
          { label: 'Cuba', value: 'CU' },
          { label: 'Cyprus', value: 'CY' },
          { label: 'Czechia', value: 'CZ' },
          { label: "Côte d'Ivoire", value: 'CI' },
          { label: 'Denmark', value: 'DK' },
          { label: 'Djibouti', value: 'DJ' },
          { label: 'Dominica', value: 'DM' },
          { label: 'Dominican Republic', value: 'DO' },
          { label: 'Ecuador', value: 'EC' },
          { label: 'Egypt', value: 'EG' },
          { label: 'El Salvador', value: 'SV' },
          { label: 'Equatorial Guinea', value: 'GQ' },
          { label: 'Eritrea', value: 'ER' },
          { label: 'Estonia', value: 'EE' },
          { label: 'Eswatini', value: 'SZ' },
          { label: 'Ethiopia', value: 'ET' },
          { label: 'Falkland Islands', value: 'FK' },
          { label: 'Faroe Islands', value: 'FO' },
          { label: 'Fiji', value: 'FJ' },
          { label: 'Finland', value: 'FI' },
          { label: 'France', value: 'FR' },
          { label: 'French Guiana', value: 'GF' },
          { label: 'French Polynesia', value: 'PF' },
          { label: 'Gabon', value: 'GA' },
          { label: 'Gambia', value: 'GM' },
          { label: 'Georgia', value: 'GE' },
          { label: 'Germany', value: 'DE' },
          { label: 'Ghana', value: 'GH' },
          { label: 'Gibraltar', value: 'GI' },
          { label: 'Greece', value: 'GR' },
          { label: 'Greenland', value: 'GL' },
          { label: 'Grenada', value: 'GD' },
          { label: 'Guadeloupe', value: 'GP' },
          { label: 'Guam', value: 'GU' },
          { label: 'Guatemala', value: 'GT' },
          { label: 'Guernsey', value: 'GG' },
          { label: 'Guinea', value: 'GN' },
          { label: 'Guinea-Bissau', value: 'GW' },
          { label: 'Guyana', value: 'GY' },
          { label: 'Haiti', value: 'HT' },
          { label: 'Honduras', value: 'HN' },
          { label: 'Hong Kong SAR China', value: 'HK' },
          { label: 'Hungary', value: 'HU' },
          { label: 'Iceland', value: 'IS' },
          { label: 'India', value: 'IN' },
          { label: 'Indonesia', value: 'ID' },
          { label: 'Iran', value: 'IR' },
          { label: 'Iraq', value: 'IQ' },
          { label: 'Ireland', value: 'IE' },
          { label: 'Isle of Man', value: 'IM' },
          { label: 'Israel', value: 'IL' },
          { label: 'Italy', value: 'IT' },
          { label: 'Jamaica', value: 'JM' },
          { label: 'Japan', value: 'JP' },
          { label: 'Jersey', value: 'JE' },
          { label: 'Jordan', value: 'JO' },
          { label: 'Kazakhstan', value: 'KZ' },
          { label: 'Kenya', value: 'KE' },
          { label: 'Kiribati', value: 'KI' },
          { label: 'Kuwait', value: 'KW' },
          { label: 'Kyrgyzstan', value: 'KG' },
          { label: 'Laos', value: 'LA' },
          { label: 'Latvia', value: 'LV' },
          { label: 'Lebanon', value: 'LB' },
          { label: 'Lesotho', value: 'LS' },
          { label: 'Liberia', value: 'LR' },
          { label: 'Libya', value: 'LY' },
          { label: 'Liechtenstein', value: 'LI' },
          { label: 'Lithuania', value: 'LT' },
          { label: 'Luxembourg', value: 'LU' },
          { label: 'Macao SAR China', value: 'MO' },
          { label: 'Madagascar', value: 'MG' },
          { label: 'Malawi', value: 'MW' },
          { label: 'Malaysia', value: 'MY' },
          { label: 'Maldives', value: 'MV' },
          { label: 'Mali', value: 'ML' },
          { label: 'Malta', value: 'MT' },
          { label: 'Marshall Islands', value: 'MH' },
          { label: 'Martinique', value: 'MQ' },
          { label: 'Mauritania', value: 'MR' },
          { label: 'Mauritius', value: 'MU' },
          { label: 'Mayotte', value: 'YT' },
          { label: 'Mexico', value: 'MX' },
          { label: 'Micronesia', value: 'FM' },
          { label: 'Moldova', value: 'MD' },
          { label: 'Monaco', value: 'MC' },
          { label: 'Mongolia', value: 'MN' },
          { label: 'Montenegro', value: 'ME' },
          { label: 'Montserrat', value: 'MS' },
          { label: 'Morocco', value: 'MA' },
          { label: 'Mozambique', value: 'MZ' },
          { label: 'Myanmar (Burma)', value: 'MM' },
          { label: 'Namibia', value: 'NA' },
          { label: 'Nauru', value: 'NR' },
          { label: 'Nepal', value: 'NP' },
          { label: 'Netherlands', value: 'NL' },
          { label: 'New Caledonia', value: 'NC' },
          { label: 'New Zealand', value: 'NZ' },
          { label: 'Nicaragua', value: 'NI' },
          { label: 'Niger', value: 'NE' },
          { label: 'Nigeria', value: 'NG' },
          { label: 'Niue', value: 'NU' },
          { label: 'Norfolk Island', value: 'NF' },
          { label: 'North Korea', value: 'KP' },
          { label: 'North Macedonia', value: 'MK' },
          { label: 'Northern Mariana Islands', value: 'MP' },
          { label: 'Norway', value: 'NO' },
          { label: 'Oman', value: 'OM' },
          { label: 'Pakistan', value: 'PK' },
          { label: 'Palau', value: 'PW' },
          { label: 'Palestinian Territories', value: 'PS' },
          { label: 'Panama', value: 'PA' },
          { label: 'Papua New Guinea', value: 'PG' },
          { label: 'Paraguay', value: 'PY' },
          { label: 'Peru', value: 'PE' },
          { label: 'Philippines', value: 'PH' },
          { label: 'Pitcairn Islands', value: 'PN' },
          { label: 'Poland', value: 'PL' },
          { label: 'Portugal', value: 'PT' },
          { label: 'Puerto Rico', value: 'PR' },
          { label: 'Qatar', value: 'QA' },
          { label: 'Romania', value: 'RO' },
          { label: 'Russia', value: 'RU' },
          { label: 'Rwanda', value: 'RW' },
          { label: 'Réunion', value: 'RE' },
          { label: 'Samoa', value: 'WS' },
          { label: 'San Marino', value: 'SM' },
          { label: 'Saudi Arabia', value: 'SA' },
          { label: 'Senegal', value: 'SN' },
          { label: 'Serbia', value: 'RS' },
          { label: 'Seychelles', value: 'SC' },
          { label: 'Sierra Leone', value: 'SL' },
          { label: 'Singapore', value: 'SG' },
          { label: 'Sint Maarten', value: 'SX' },
          { label: 'Slovakia', value: 'SK' },
          { label: 'Slovenia', value: 'SI' },
          { label: 'Solomon Islands', value: 'SB' },
          { label: 'Somalia', value: 'SO' },
          { label: 'South Africa', value: 'ZA' },
          { label: 'South Korea', value: 'KR' },
          { label: 'South Sudan', value: 'SS' },
          { label: 'Spain', value: 'ES' },
          { label: 'Sri Lanka', value: 'LK' },
          { label: 'St. Barthélemy', value: 'BL' },
          { label: 'St. Helena', value: 'SH' },
          { label: 'St. Kitts & Nevis', value: 'KN' },
          { label: 'St. Lucia', value: 'LC' },
          { label: 'St. Martin', value: 'MF' },
          { label: 'St. Pierre & Miquelon', value: 'PM' },
          { label: 'St. Vincent & Grenadines', value: 'VC' },
          { label: 'Sudan', value: 'SD' },
          { label: 'Suriname', value: 'SR' },
          { label: 'Svalbard & Jan Mayen', value: 'SJ' },
          { label: 'Sweden', value: 'SE' },
          { label: 'Switzerland', value: 'CH' },
          { label: 'Syria', value: 'SY' },
          { label: 'São Tomé & Príncipe', value: 'ST' },
          { label: 'Taiwan', value: 'TW' },
          { label: 'Tajikistan', value: 'TJ' },
          { label: 'Tanzania', value: 'TZ' },
          { label: 'Thailand', value: 'TH' },
          { label: 'Timor-Leste', value: 'TL' },
          { label: 'Togo', value: 'TG' },
          { label: 'Tokelau', value: 'TK' },
          { label: 'Tonga', value: 'TO' },
          { label: 'Trinidad & Tobago', value: 'TT' },
          { label: 'Tunisia', value: 'TN' },
          { label: 'Turkey', value: 'TR' },
          { label: 'Turkmenistan', value: 'TM' },
          { label: 'Turks & Caicos Islands', value: 'TC' },
          { label: 'Tuvalu', value: 'TV' },
          { label: 'U.S. Outlying Islands', value: 'UM' },
          { label: 'U.S. Virgin Islands', value: 'VI' },
          { label: 'Uganda', value: 'UG' },
          { label: 'Ukraine', value: 'UA' },
          { label: 'United Arab Emirates', value: 'AE' },
          { label: 'United Kingdom', value: 'GB' },
          { label: 'United States', value: 'US' },
          { label: 'Uruguay', value: 'UY' },
          { label: 'Uzbekistan', value: 'UZ' },
          { label: 'Vanuatu', value: 'VU' },
          { label: 'Vatican City', value: 'VA' },
          { label: 'Venezuela', value: 'VE' },
          { label: 'Vietnam', value: 'VN' },
          { label: 'Wallis & Futuna', value: 'WF' },
          { label: 'Western Sahara', value: 'EH' },
          { label: 'Yemen', value: 'YE' },
          { label: 'Zambia', value: 'ZM' },
          { label: 'Zimbabwe', value: 'ZW' },
        ],
      },
    }),

    data_type: Property.StaticDropdown({
      displayName: 'Data Type',
      description: 'Type of trends data to retrieve',
      required: false,
      defaultValue: 'TIMESERIES',
      options: {
        options: [
          { label: 'Interest Over Time', value: 'TIMESERIES' },
          { label: 'Compared breakdown by region', value: 'GEO_MAP' },
          { label: 'Interest by Region', value: 'GEO_MAP_0' },
          { label: 'Related Topics', value: 'RELATED_TOPICS' },
          { label: 'Related Queries', value: 'RELATED_QUERIES' },
        ],
      },
    }),

    time_period: Property.StaticDropdown({
      displayName: 'Time Period',
      description: 'Time range for the trends data',
      required: false,
      defaultValue: 'today 12-m',
      options: {
        options: [
          { label: 'Past Hour', value: 'now 1-H' },
          { label: 'Past 4 Hours', value: 'now 4-H' },
          { label: 'Past Day', value: 'now 1-d' },
          { label: 'Past 7 Days', value: 'now 7-d' },
          { label: 'Past 30 Days', value: 'today 1-m' },
          { label: 'Past 90 Days', value: 'today 3-m' },
          { label: 'Past 12 Months', value: 'today 12-m' },
          { label: 'Past 5 Years', value: 'today 5-y' },
          { label: 'Since 2004', value: 'all' },
        ],
      },
    }),

    category: Property.Number({
      displayName: 'Category',
      description: 'Google Trends category ID (0 for all categories)',
      required: false,
      defaultValue: 0,
    }),

    property_filter: Property.StaticDropdown({
      displayName: 'Property Filter (grop)',
      description: 'Filter by Google property',
      required: false,
      defaultValue: '',
      options: {
        options: [
          { label: 'Web Search', value: '' },
          { label: 'Image Search', value: 'images' },
          { label: 'News Search', value: 'news' },
          { label: 'Google Shopping', value: 'froogle' },
          { label: 'YouTube Search', value: 'youtube' },
        ],
      },
    }),

    no_cache: Property.StaticDropdown({
      displayName: 'No Cache',
      description: 'Force SerpApi to fetch the Google results even if a cached version is already present.',
      required: false,
      defaultValue: 'false',
      options: {
        options: [
          { label: 'false (Allow cache)', value: 'false' },
          { label: 'true (Force fresh results)', value: 'true' },
        ],
      },
    }),

    async: Property.StaticDropdown({
      displayName: 'Async',
      description: 'Defines the way you want to submit your search to SerpApi. It can be set to false (default) to open an HTTP connection and keep it open until you got your search results, or true to just submit your search to SerpApi and retrieve them later.',
      required: false,
      defaultValue: 'false',
      options: {
        options: [
          { label: 'false (Synchronous)', value: 'false' },
          { label: 'true (Asynchronous)', value: 'true' },
        ],
      },
    }),

    output: Property.StaticDropdown({
      displayName: 'Output',
      description: 'Defines the final output format you want.',
      required: false,
      defaultValue: 'json',
      options: {
        options: [
          { label: 'JSON (Structured)', value: 'json' },
          { label: 'HTML (Raw)', value: 'html' },
        ],
      },
    }),
  },

  async run({ auth, propsValue }) {
    try {
      const client = new SerpApiClient({
        defaultTimeout: 30000,
        defaultRetries: 3,
        defaultRetryDelay: 1000,
        enableLogging: false,
      });

      const searchConfig: GoogleTrendsSearchConfig = {
        api_key: auth.secret_text,
        engine: SerpApiEngine.GOOGLE_TRENDS,
        q: propsValue.query,
        hl: propsValue.hl,
        data_type: propsValue.data_type as 'TIMESERIES' | 'GEO_MAP' | 'RELATED_TOPICS' | 'RELATED_QUERIES',
        geo: propsValue.geo,
        date: propsValue.time_period,
        cat: propsValue.category,
      };

      // Add property filter if specified
      if (propsValue.property_filter) {
        searchConfig.gprop = propsValue.property_filter;
      }

      // Add optional parameters
      if (propsValue.no_cache) {
        (searchConfig as any).no_cache = propsValue.no_cache;
      }

      if (propsValue.async) {
        (searchConfig as any).async = propsValue.async;
      }

      if (propsValue.output) {
        (searchConfig as any).output = propsValue.output;
      }

      const response = await client.executeSearch(searchConfig);

      return {
        success: true,
        ...response,
      };
    } catch (error: unknown) {
      const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';

      return {
        success: false,
        error: errorMessage,
        error_type: error instanceof Error ? error.constructor.name : 'UnknownError',
        timestamp: new Date().toISOString(),
        search_query: propsValue.query,
      };
    }
  },
});
