import { z } from 'zod'
import type { Tool } from '../../../../types'
import { createIntegrationTool } from '../../../tool'

/**
 * Create a tool that sends transactional emails via Resend
 *
 * @param credentialName - Name of the Resend credential to use
 * @returns Tool instance
 *
 * @example
 * ```typescript
 * const tool = createResendSendEmailTool('elevasis-resend')
 * const result = await tool.execute({
 *   from: 'Alex <alexl@elevasis.io>',
 *   to: 'john@example.com',
 *   subject: 'Discovery Call Reminder',
 *   html: '<p>Your call is tomorrow...</p>'
 * }, context)
 * ```
 */
export function createResendSendEmailTool(credentialName: string): Tool {
  return createIntegrationTool({
    name: 'resend_send_email',
    description: `Send a transactional email via Resend.

Use this tool to send emails for:
- Discovery call reminders (-24h, -1h before meeting)
- Proposal follow-ups (Day 3, 7, 14)
- Onboarding emails (welcome, kickoff scheduling)

Examples:
- Send reminder: from="Alex <alexl@elevasis.io>", to, subject, html with meeting details
- With unsubscribe: Include headers with List-Unsubscribe and List-Unsubscribe-Post

Supports RFC 8058 one-click unsubscribe via headers parameter.`,
    inputSchema: z.object({
      from: z.string().describe('Sender in format "Name <email@domain.com>"'),
      to: z.union([z.string(), z.array(z.string())]).describe('Recipient email(s) - max 50'),
      subject: z.string().describe('Email subject line'),
      html: z.string().optional().describe('HTML email content'),
      text: z.string().optional().describe('Plain text content (auto-generated from HTML if omitted)'),
      replyTo: z.union([z.string(), z.array(z.string())]).optional().describe('Reply-to address(es)'),
      cc: z.union([z.string(), z.array(z.string())]).optional().describe('CC recipient(s)'),
      bcc: z.union([z.string(), z.array(z.string())]).optional().describe('BCC recipient(s)'),
      headers: z.record(z.string(), z.string()).optional().describe('Custom headers (e.g., List-Unsubscribe)'),
      tags: z.array(z.object({
        name: z.string(),
        value: z.string()
      })).optional().describe('Key-value metadata tags')
    }),
    outputSchema: z.object({
      id: z.string().describe('Resend email ID'),
      success: z.boolean().describe('Whether the email was sent successfully')
    }),
    integration: 'resend' as const,
    method: 'sendEmail' as const,
    credentialName
  })
}

/**
 * Create a tool that retrieves email status from Resend
 *
 * @param credentialName - Name of the Resend credential to use
 * @returns Tool instance
 *
 * @example
 * ```typescript
 * const tool = createResendGetEmailTool('elevasis-resend')
 * const result = await tool.execute({
 *   emailId: '49a3999c-0ce1-4ea6-ab68-afcd6dc2e794'
 * }, context)
 * // result.lastEvent: 'sent', 'delivered', 'bounced', 'complained', 'failed'
 * ```
 */
export function createResendGetEmailTool(credentialName: string): Tool {
  return createIntegrationTool({
    name: 'resend_get_email',
    description: `Get email delivery status from Resend.

Use this tool to check if an email was delivered successfully.

Status values:
- sent: Email was sent
- delivered: Email was delivered to recipient
- bounced: Email bounced (hard bounce)
- complained: Recipient marked as spam
- failed: Email failed to send

Not required for MVP, but useful for debugging delivery issues.`,
    inputSchema: z.object({
      emailId: z.string().describe('Resend email ID to retrieve')
    }),
    outputSchema: z.object({
      id: z.string().describe('Email ID'),
      to: z.array(z.string()).describe('Recipient email addresses'),
      from: z.string().describe('Sender email address'),
      subject: z.string().describe('Email subject'),
      lastEvent: z.string().describe('Last delivery event (sent, delivered, bounced, complained, failed)'),
      createdAt: z.string().describe('ISO timestamp when email was created')
    }),
    integration: 'resend' as const,
    method: 'getEmail' as const,
    credentialName
  })
}

/**
 * Create a named version of Resend send email tool (for multi-account support)
 *
 * @param toolName - Unique tool name
 * @param credentialName - Name of the Resend credential to use
 * @returns Tool instance
 *
 * @example
 * ```typescript
 * // Support multiple Resend accounts or domains
 * const salesTool = createResendSendEmailToolNamed('resend_sales_send', 'resend-sales')
 * const supportTool = createResendSendEmailToolNamed('resend_support_send', 'resend-support')
 * ```
 */
export function createResendSendEmailToolNamed(
  toolName: string,
  credentialName: string
): Tool {
  const tool = createResendSendEmailTool(credentialName)
  return {
    ...tool,
    name: toolName
  }
}
