import { ResendIcon } from '@/components/icons'
import type { BlockConfig } from '@/blocks/types'
import { AuthMode, IntegrationType } from '@/blocks/types'
import { getTrigger } from '@/triggers'

export const ResendBlock: BlockConfig = {
  type: 'resend',
  name: 'Resend',
  description: 'Send emails and manage contacts with Resend.',
  longDescription:
    'Integrate Resend into your workflow. Send emails, retrieve email status, manage contacts, and view domains. Requires API Key.',
  docsLink: 'https://docs.sim.ai/tools/resend',
  category: 'tools',
  integrationType: IntegrationType.Email,
  tags: ['email-marketing', 'messaging'],
  bgColor: '#181C1E',
  icon: ResendIcon,
  authMode: AuthMode.ApiKey,

  triggers: {
    enabled: true,
    available: [
      'resend_email_sent',
      'resend_email_delivered',
      'resend_email_bounced',
      'resend_email_complained',
      'resend_email_opened',
      'resend_email_clicked',
      'resend_email_failed',
      'resend_webhook',
    ],
  },

  subBlocks: [
    {
      id: 'operation',
      title: 'Operation',
      type: 'dropdown',
      options: [
        { label: 'Send Email', id: 'send_email' },
        { label: 'Get Email', id: 'get_email' },
        { label: 'Create Contact', id: 'create_contact' },
        { label: 'List Contacts', id: 'list_contacts' },
        { label: 'Get Contact', id: 'get_contact' },
        { label: 'Update Contact', id: 'update_contact' },
        { label: 'Delete Contact', id: 'delete_contact' },
        { label: 'List Domains', id: 'list_domains' },
      ],
      value: () => 'send_email',
    },
    {
      id: 'resendApiKey',
      title: 'Resend API Key',
      type: 'short-input',
      placeholder: 'Your Resend API key',
      required: true,
      password: true,
    },

    {
      id: 'fromAddress',
      title: 'From Address',
      type: 'short-input',
      placeholder: 'sender@yourdomain.com',
      condition: { field: 'operation', value: 'send_email' },
      required: true,
    },
    {
      id: 'to',
      title: 'To',
      type: 'short-input',
      placeholder: 'recipient@example.com',
      condition: { field: 'operation', value: 'send_email' },
      required: true,
    },
    {
      id: 'subject',
      title: 'Subject',
      type: 'short-input',
      placeholder: 'Email subject',
      condition: { field: 'operation', value: 'send_email' },
      required: true,
      wandConfig: {
        enabled: true,
        prompt: `Generate a compelling email subject line based on the user's description.

### GUIDELINES
- Keep it concise (50 characters or less is ideal)
- Make it attention-grabbing
- Avoid spam trigger words
- Be clear about the email content

### EXAMPLES
"Welcome email for new users" -> "Welcome to Our Platform!"
"Order confirmation" -> "Your Order #12345 is Confirmed"
"Newsletter about new features" -> "New Features You'll Love"

Return ONLY the subject line - no explanations, no extra text.`,
        placeholder: 'Describe the email topic...',
      },
    },
    {
      id: 'body',
      title: 'Body',
      type: 'long-input',
      placeholder: 'Email body content',
      condition: { field: 'operation', value: 'send_email' },
      required: true,
      wandConfig: {
        enabled: true,
        prompt: `Generate email content based on the user's description.

### GUIDELINES
- Use clear, readable formatting
- Keep paragraphs short
- Include appropriate greeting and sign-off

Return ONLY the email body - no explanations, no extra text.`,
        placeholder: 'Describe the email content...',
      },
    },
    {
      id: 'contentType',
      title: 'Content Type',
      type: 'dropdown',
      options: [
        { label: 'Plain Text', id: 'text' },
        { label: 'HTML', id: 'html' },
      ],
      value: () => 'text',
      condition: { field: 'operation', value: 'send_email' },
      mode: 'advanced',
    },
    {
      id: 'cc',
      title: 'CC',
      type: 'short-input',
      placeholder: 'cc@example.com',
      condition: { field: 'operation', value: 'send_email' },
      mode: 'advanced',
    },
    {
      id: 'bcc',
      title: 'BCC',
      type: 'short-input',
      placeholder: 'bcc@example.com',
      condition: { field: 'operation', value: 'send_email' },
      mode: 'advanced',
    },
    {
      id: 'replyTo',
      title: 'Reply To',
      type: 'short-input',
      placeholder: 'reply@example.com',
      condition: { field: 'operation', value: 'send_email' },
      mode: 'advanced',
    },
    {
      id: 'scheduledAt',
      title: 'Schedule At',
      type: 'short-input',
      placeholder: '2024-08-05T11:52:01.858Z',
      condition: { field: 'operation', value: 'send_email' },
      mode: 'advanced',
      wandConfig: {
        enabled: true,
        generationType: 'timestamp',
        prompt:
          'Generate an ISO 8601 timestamp for scheduling email delivery. Return ONLY the timestamp - no explanations, no extra text.',
        placeholder: 'Describe when to send (e.g., "tomorrow at 9am")...',
      },
    },
    {
      id: 'tags',
      title: 'Tags',
      type: 'short-input',
      placeholder: 'category:welcome,type:onboarding',
      condition: { field: 'operation', value: 'send_email' },
      mode: 'advanced',
      wandConfig: {
        enabled: true,
        prompt:
          'Generate comma-separated key:value pairs for email tags based on the description. Example format: "category:welcome,type:onboarding". Return ONLY the tag pairs - no explanations, no extra text.',
        placeholder: 'Describe the email tags...',
      },
    },

    {
      id: 'emailId',
      title: 'Email ID',
      type: 'short-input',
      placeholder: 'Email ID to retrieve',
      condition: { field: 'operation', value: 'get_email' },
      required: true,
    },

    {
      id: 'email',
      title: 'Email',
      type: 'short-input',
      placeholder: 'contact@example.com',
      condition: { field: 'operation', value: 'create_contact' },
      required: true,
    },
    {
      id: 'firstName',
      title: 'First Name',
      type: 'short-input',
      placeholder: 'John',
      condition: { field: 'operation', value: ['create_contact', 'update_contact'] },
    },
    {
      id: 'lastName',
      title: 'Last Name',
      type: 'short-input',
      placeholder: 'Doe',
      condition: { field: 'operation', value: ['create_contact', 'update_contact'] },
    },
    {
      id: 'unsubscribed',
      title: 'Unsubscribed',
      type: 'dropdown',
      options: [
        { label: 'No', id: 'false' },
        { label: 'Yes', id: 'true' },
      ],
      value: () => 'false',
      condition: { field: 'operation', value: ['create_contact', 'update_contact'] },
    },

    {
      id: 'contactId',
      title: 'Contact ID or Email',
      type: 'short-input',
      placeholder: 'Contact ID or email address',
      condition: { field: 'operation', value: ['get_contact', 'update_contact', 'delete_contact'] },
      required: true,
    },

    ...getTrigger('resend_email_sent').subBlocks,
    ...getTrigger('resend_email_delivered').subBlocks,
    ...getTrigger('resend_email_bounced').subBlocks,
    ...getTrigger('resend_email_complained').subBlocks,
    ...getTrigger('resend_email_opened').subBlocks,
    ...getTrigger('resend_email_clicked').subBlocks,
    ...getTrigger('resend_email_failed').subBlocks,
    ...getTrigger('resend_webhook').subBlocks,
  ],

  tools: {
    access: [
      'resend_send',
      'resend_get_email',
      'resend_create_contact',
      'resend_list_contacts',
      'resend_get_contact',
      'resend_update_contact',
      'resend_delete_contact',
      'resend_list_domains',
    ],
    config: {
      tool: (params) => {
        const operation = params.operation || 'send_email'
        if (operation === 'send_email') return 'resend_send'
        return `resend_${operation}`
      },
      params: (params) => {
        const { operation, ...rest } = params

        if (rest.unsubscribed !== undefined) {
          rest.unsubscribed = rest.unsubscribed === 'true'
        }

        return rest
      },
    },
  },

  inputs: {
    operation: { type: 'string', description: 'Operation to perform' },
    resendApiKey: { type: 'string', description: 'Resend API key' },
    fromAddress: { type: 'string', description: 'Email address to send from' },
    to: { type: 'string', description: 'Recipient email address' },
    subject: { type: 'string', description: 'Email subject' },
    body: { type: 'string', description: 'Email body content' },
    contentType: { type: 'string', description: 'Content type (text or html)' },
    cc: { type: 'string', description: 'CC email address' },
    bcc: { type: 'string', description: 'BCC email address' },
    replyTo: { type: 'string', description: 'Reply-to email address' },
    scheduledAt: { type: 'string', description: 'Scheduled send time in ISO 8601 format' },
    tags: { type: 'string', description: 'Email tags as key:value pairs' },
    emailId: { type: 'string', description: 'Email ID to retrieve' },
    email: { type: 'string', description: 'Contact email address' },
    firstName: { type: 'string', description: 'Contact first name' },
    lastName: { type: 'string', description: 'Contact last name' },
    unsubscribed: { type: 'string', description: 'Contact subscription status' },
    contactId: { type: 'string', description: 'Contact ID or email address' },
  },

  outputs: {
    success: { type: 'boolean', description: 'Operation success status' },
    id: { type: 'string', description: 'Email or contact ID' },
    to: { type: 'string', description: 'Recipient email address' },
    subject: { type: 'string', description: 'Email subject' },
    body: { type: 'string', description: 'Email body content' },
    from: { type: 'string', description: 'Sender email address' },
    html: { type: 'string', description: 'HTML email content' },
    text: { type: 'string', description: 'Plain text email content' },
    lastEvent: { type: 'string', description: 'Last event status' },
    createdAt: { type: 'string', description: 'Creation timestamp' },
    scheduledAt: { type: 'string', description: 'Scheduled send timestamp' },
    tags: { type: 'json', description: 'Email tags as name-value pairs' },
    email: { type: 'string', description: 'Contact email address' },
    firstName: { type: 'string', description: 'Contact first name' },
    lastName: { type: 'string', description: 'Contact last name' },
    unsubscribed: { type: 'boolean', description: 'Whether the contact is unsubscribed' },
    contacts: { type: 'json', description: 'Array of contacts' },
    domains: { type: 'json', description: 'Array of domains' },
    hasMore: { type: 'boolean', description: 'Whether more results are available' },
    deleted: { type: 'boolean', description: 'Whether the resource was deleted' },
  },
}
