import { GoogleTranslateIcon } from '@/components/icons'
import { AuthMode, type BlockConfig, type BlockMeta, IntegrationType } from '@/blocks/types'

const SUPPORTED_LANGUAGES = [
  { label: 'Afrikaans', id: 'af' },
  { label: 'Albanian', id: 'sq' },
  { label: 'Amharic', id: 'am' },
  { label: 'Arabic', id: 'ar' },
  { label: 'Armenian', id: 'hy' },
  { label: 'Assamese', id: 'as' },
  { label: 'Aymara', id: 'ay' },
  { label: 'Azerbaijani', id: 'az' },
  { label: 'Bambara', id: 'bm' },
  { label: 'Basque', id: 'eu' },
  { label: 'Belarusian', id: 'be' },
  { label: 'Bengali', id: 'bn' },
  { label: 'Bhojpuri', id: 'bho' },
  { label: 'Bosnian', id: 'bs' },
  { label: 'Bulgarian', id: 'bg' },
  { label: 'Catalan', id: 'ca' },
  { label: 'Cebuano', id: 'ceb' },
  { label: 'Chinese (Simplified)', id: 'zh-CN' },
  { label: 'Chinese (Traditional)', id: 'zh-TW' },
  { label: 'Corsican', id: 'co' },
  { label: 'Croatian', id: 'hr' },
  { label: 'Czech', id: 'cs' },
  { label: 'Danish', id: 'da' },
  { label: 'Dhivehi', id: 'dv' },
  { label: 'Dogri', id: 'doi' },
  { label: 'Dutch', id: 'nl' },
  { label: 'English', id: 'en' },
  { label: 'Esperanto', id: 'eo' },
  { label: 'Estonian', id: 'et' },
  { label: 'Ewe', id: 'ee' },
  { label: 'Filipino', id: 'tl' },
  { label: 'Finnish', id: 'fi' },
  { label: 'French', id: 'fr' },
  { label: 'Frisian', id: 'fy' },
  { label: 'Galician', id: 'gl' },
  { label: 'Georgian', id: 'ka' },
  { label: 'German', id: 'de' },
  { label: 'Greek', id: 'el' },
  { label: 'Guarani', id: 'gn' },
  { label: 'Gujarati', id: 'gu' },
  { label: 'Haitian Creole', id: 'ht' },
  { label: 'Hausa', id: 'ha' },
  { label: 'Hawaiian', id: 'haw' },
  { label: 'Hebrew', id: 'he' },
  { label: 'Hindi', id: 'hi' },
  { label: 'Hmong', id: 'hmn' },
  { label: 'Hungarian', id: 'hu' },
  { label: 'Icelandic', id: 'is' },
  { label: 'Igbo', id: 'ig' },
  { label: 'Ilocano', id: 'ilo' },
  { label: 'Indonesian', id: 'id' },
  { label: 'Irish', id: 'ga' },
  { label: 'Italian', id: 'it' },
  { label: 'Japanese', id: 'ja' },
  { label: 'Javanese', id: 'jv' },
  { label: 'Kannada', id: 'kn' },
  { label: 'Kazakh', id: 'kk' },
  { label: 'Khmer', id: 'km' },
  { label: 'Kinyarwanda', id: 'rw' },
  { label: 'Konkani', id: 'gom' },
  { label: 'Korean', id: 'ko' },
  { label: 'Krio', id: 'kri' },
  { label: 'Kurdish', id: 'ku' },
  { label: 'Kurdish (Sorani)', id: 'ckb' },
  { label: 'Kyrgyz', id: 'ky' },
  { label: 'Lao', id: 'lo' },
  { label: 'Latin', id: 'la' },
  { label: 'Latvian', id: 'lv' },
  { label: 'Lingala', id: 'ln' },
  { label: 'Lithuanian', id: 'lt' },
  { label: 'Luganda', id: 'lg' },
  { label: 'Luxembourgish', id: 'lb' },
  { label: 'Macedonian', id: 'mk' },
  { label: 'Maithili', id: 'mai' },
  { label: 'Malagasy', id: 'mg' },
  { label: 'Malay', id: 'ms' },
  { label: 'Malayalam', id: 'ml' },
  { label: 'Maltese', id: 'mt' },
  { label: 'Maori', id: 'mi' },
  { label: 'Marathi', id: 'mr' },
  { label: 'Meiteilon (Manipuri)', id: 'mni-Mtei' },
  { label: 'Mizo', id: 'lus' },
  { label: 'Mongolian', id: 'mn' },
  { label: 'Myanmar (Burmese)', id: 'my' },
  { label: 'Nepali', id: 'ne' },
  { label: 'Norwegian', id: 'no' },
  { label: 'Nyanja (Chichewa)', id: 'ny' },
  { label: 'Odia (Oriya)', id: 'or' },
  { label: 'Oromo', id: 'om' },
  { label: 'Pashto', id: 'ps' },
  { label: 'Persian', id: 'fa' },
  { label: 'Polish', id: 'pl' },
  { label: 'Portuguese', id: 'pt' },
  { label: 'Punjabi', id: 'pa' },
  { label: 'Quechua', id: 'qu' },
  { label: 'Romanian', id: 'ro' },
  { label: 'Russian', id: 'ru' },
  { label: 'Samoan', id: 'sm' },
  { label: 'Sanskrit', id: 'sa' },
  { label: 'Scots Gaelic', id: 'gd' },
  { label: 'Sepedi', id: 'nso' },
  { label: 'Serbian', id: 'sr' },
  { label: 'Sesotho', id: 'st' },
  { label: 'Shona', id: 'sn' },
  { label: 'Sindhi', id: 'sd' },
  { label: 'Sinhala', id: 'si' },
  { label: 'Slovak', id: 'sk' },
  { label: 'Slovenian', id: 'sl' },
  { label: 'Somali', id: 'so' },
  { label: 'Spanish', id: 'es' },
  { label: 'Sundanese', id: 'su' },
  { label: 'Swahili', id: 'sw' },
  { label: 'Swedish', id: 'sv' },
  { label: 'Tajik', id: 'tg' },
  { label: 'Tamil', id: 'ta' },
  { label: 'Tatar', id: 'tt' },
  { label: 'Telugu', id: 'te' },
  { label: 'Thai', id: 'th' },
  { label: 'Tigrinya', id: 'ti' },
  { label: 'Tsonga', id: 'ts' },
  { label: 'Turkish', id: 'tr' },
  { label: 'Turkmen', id: 'tk' },
  { label: 'Twi (Akan)', id: 'ak' },
  { label: 'Ukrainian', id: 'uk' },
  { label: 'Urdu', id: 'ur' },
  { label: 'Uyghur', id: 'ug' },
  { label: 'Uzbek', id: 'uz' },
  { label: 'Vietnamese', id: 'vi' },
  { label: 'Welsh', id: 'cy' },
  { label: 'Xhosa', id: 'xh' },
  { label: 'Yiddish', id: 'yi' },
  { label: 'Yoruba', id: 'yo' },
  { label: 'Zulu', id: 'zu' },
] satisfies { label: string; id: string }[]

export const GoogleTranslateBlock: BlockConfig = {
  type: 'google_translate',
  name: 'Google Translate',
  description: 'Translate text using Google Cloud Translation',
  longDescription:
    'Translate and detect languages using the Google Cloud Translation API. Supports auto-detection of the source language.',
  docsLink: 'https://docs.sim.ai/integrations/google_translate',
  category: 'tools',
  integrationType: IntegrationType.AI,
  bgColor: '#FFFFFF',
  icon: GoogleTranslateIcon,
  authMode: AuthMode.ApiKey,
  subBlocks: [
    {
      id: 'operation',
      title: 'Operation',
      type: 'dropdown',
      options: [
        { label: 'Translate Text', id: 'text' },
        { label: 'Detect Language', id: 'detect' },
      ],
      value: () => 'text',
    },
    {
      id: 'text',
      title: 'Text',
      type: 'long-input',
      placeholder: 'Enter text...',
      required: true,
    },
    {
      id: 'target',
      title: 'Target Language',
      type: 'dropdown',
      condition: { field: 'operation', value: 'text' },
      searchable: true,
      options: SUPPORTED_LANGUAGES,
      value: () => 'es',
      required: { field: 'operation', value: 'text' },
    },
    {
      id: 'source',
      title: 'Source Language',
      type: 'dropdown',
      condition: { field: 'operation', value: 'text' },
      searchable: true,
      options: [{ label: 'Auto-detect', id: '' }, ...SUPPORTED_LANGUAGES],
      value: () => '',
    },
    {
      id: 'apiKey',
      title: 'API Key',
      type: 'short-input',
      placeholder: 'Enter your Google Cloud API key',
      password: true,
      required: true,
      hideWhenHosted: true,
    },
  ],
  tools: {
    access: ['google_translate_text', 'google_translate_detect'],
    config: {
      tool: (params) => `google_translate_${params.operation}`,
    },
  },
  inputs: {
    text: { type: 'string', description: 'Text to translate or detect language of' },
    target: { type: 'string', description: 'Target language code' },
    source: { type: 'string', description: 'Source language code (optional, auto-detected)' },
    apiKey: { type: 'string', description: 'Google Cloud API key' },
  },
  outputs: {
    translatedText: { type: 'string', description: 'Translated text' },
    detectedSourceLanguage: { type: 'string', description: 'Detected source language code' },
    language: { type: 'string', description: 'Detected language code' },
    confidence: { type: 'number', description: 'Detection confidence score' },
  },
}

export const GoogleTranslateBlockMeta = {
  tags: ['google-workspace', 'content-management', 'automation'],
  url: 'https://cloud.google.com/translate',
  templates: [
    {
      icon: GoogleTranslateIcon,
      title: 'Google Translate doc localizer',
      prompt:
        'Create a workflow that watches a Google Drive folder of source-language docs, translates each into target languages with Google Translate while preserving structure, and writes the localized files back.',
      modules: ['files', 'agent', 'workflows'],
      category: 'marketing',
      tags: ['content', 'enterprise'],
      alsoIntegrations: ['google_drive'],
    },
    {
      icon: GoogleTranslateIcon,
      title: 'Multilingual support replier',
      prompt:
        'Build a workflow that detects the language of a new Intercom message, translates it to the agent language with Google Translate, drafts a reply, then translates the reply back before sending.',
      modules: ['agent', 'workflows'],
      category: 'support',
      tags: ['support', 'communication'],
      alsoIntegrations: ['intercom'],
    },
    {
      icon: GoogleTranslateIcon,
      title: 'Google Translate + Hugging Face confidence',
      prompt:
        'Create a workflow that runs Google Translate then scores translation quality with a Hugging Face model, flags low-confidence segments for human review, and writes the scored output.',
      modules: ['agent', 'workflows'],
      category: 'marketing',
      tags: ['content', 'enterprise'],
      alsoIntegrations: ['huggingface'],
    },
    {
      icon: GoogleTranslateIcon,
      title: 'Google Translate + Mistral Parser localization',
      prompt:
        'Build a workflow that uses Mistral Parser to extract structured content from source-language PDFs and Google Translate to localize each section while preserving structure.',
      modules: ['files', 'agent', 'workflows'],
      category: 'marketing',
      tags: ['content', 'enterprise'],
      alsoIntegrations: ['mistral_parse'],
    },
    {
      icon: GoogleTranslateIcon,
      title: 'Google Translate Confluence localization',
      prompt:
        'Create a workflow that watches Confluence pages tagged for localization, translates each into the target languages with Google Translate, and publishes localized copies.',
      modules: ['agent', 'workflows'],
      category: 'operations',
      tags: ['content', 'enterprise'],
      alsoIntegrations: ['confluence'],
    },
    {
      icon: GoogleTranslateIcon,
      title: 'Multilingual support replies',
      prompt:
        "Build a workflow that on a new support ticket detects the customer's language with Google Translate, translates the message to English for the agent, drafts a reply, then translates the response back into the customer's language before sending.",
      modules: ['agent', 'workflows'],
      category: 'support',
      tags: ['support', 'communication', 'automation'],
    },
    {
      icon: GoogleTranslateIcon,
      title: 'Slack channel translator',
      prompt:
        "Create a workflow that watches an international Slack channel, detects non-English messages with Google Translate, translates them to the team's language, and posts the translation in a thread so everyone stays in the loop.",
      modules: ['agent', 'workflows'],
      category: 'productivity',
      tags: ['team', 'communication'],
      alsoIntegrations: ['slack'],
    },
  ],
  skills: [
    {
      name: 'translate-to-language',
      description:
        'Translate a block of text into a target language using Google Cloud Translation.',
      content:
        '# Translate To Language\n\nTranslate text into a specified target language.\n\n## Steps\n1. Take the source text and the target language code (e.g. es, fr, ja).\n2. If the source language is unknown, detect it first; otherwise pass it explicitly for accuracy.\n3. Call translate text with the target language and capture the translated output.\n4. For long content, split into paragraph-sized chunks and translate each to preserve formatting.\n\n## Output\nReturn the translated text along with the detected or supplied source language and the target language. Preserve line breaks from the original.',
    },
    {
      name: 'detect-and-route-language',
      description: 'Detect the language of incoming text and route or label it accordingly.',
      content:
        '# Detect and Route Language\n\nIdentify the language of a message so it can be routed, labeled, or translated.\n\n## Steps\n1. Call detect language on the input text and capture the language code and confidence.\n2. If confidence is low, fall back to detecting on a longer sample or flag as uncertain.\n3. Decide the route: if the detected language differs from the team language, translate it; otherwise pass through unchanged.\n\n## Output\nReturn the detected language code, confidence, and a recommended action (translate or pass-through). Include the translated text when translation was performed.',
    },
    {
      name: 'localize-message-set',
      description:
        'Translate one source message into several target languages for multilingual delivery.',
      content:
        '# Localize Message Set\n\nProduce localized versions of a single message for multiple audiences.\n\n## Steps\n1. Take the source text and the list of target language codes.\n2. For each target language, call translate text with the source language set explicitly for consistency.\n3. Keep placeholders, names, and URLs intact across all translations.\n\n## Output\nReturn a mapping of language code to translated text. Note any target language where translation appeared incomplete or unchanged.',
    },
  ],
} as const satisfies BlockMeta
