import { z } from 'zod'
import { createIntegrationTool } from '../../../tool'
import type { Tool } from '../../../../types'
import { EmailSchema } from '../../../../../../../platform/utils/validation'

/**
 * Create Gmail send email tool
 *
 * Creates a tool that sends emails via Gmail API using organization credentials.
 * Supports both OAuth2 and service account authentication, as well as mock credentials for testing.
 *
 * @param credentialName - Name of credential in credentials table
 * @returns Tool that sends email via Gmail
 *
 * @example
 * // Create tool for support email account
 * const sendEmail = createGmailSendEmailTool('support-gmail')
 *
 * // In agent definition:
 * export const emailAgentDefinition: AgentDefinition = {
 *   config: {
 *     resourceId: 'email-agent',
 *     name: 'Email Agent',
 *     description: 'Sends emails on behalf of users'
 *   },
 *   tools: [sendEmail],
 *   // ...
 * }
 *
 * @example
 * // Create multiple tools for different accounts
 * const sendSupportEmail = createGmailSendEmailTool('support-gmail')
 * const sendSalesEmail = createGmailSendEmailTool('sales-gmail')
 *
 * // Use in agent with multiple email accounts
 * tools: [sendSupportEmail, sendSalesEmail]
 */
export function createGmailSendEmailTool(credentialName: string): Tool {
  return createIntegrationTool({
    name: 'gmail_sendEmail',
    description: 'Send an email via Gmail API',

    inputSchema: z.object({
      to: EmailSchema.describe('Recipient email address'),
      subject: z.string().describe('Email subject line'),
      body: z.string().describe('Email body (HTML supported)'),
      cc: EmailSchema.optional().describe('Optional CC recipient'),
      from: z.string().optional().describe('Optional sender display name (defaults to "me")')
    }),

    outputSchema: z.object({
      messageId: z.string().describe('Gmail message ID'),
      threadId: z.string().describe('Gmail thread ID')
    }),

    integration: 'gmail' as const,
    method: 'sendEmail' as const,
    credentialName: credentialName
  })
}

/**
 * Create Gmail send email tool with custom name
 *
 * Allows creating multiple Gmail tools with different names for the same organization.
 * Useful when you want to distinguish between different email accounts in tool naming.
 *
 * @param toolName - Custom name for the tool (e.g., 'send_support_email')
 * @param credentialName - Name of credential in credentials table
 * @returns Tool that sends email via Gmail
 *
 * @example
 * const sendSupportEmail = createGmailSendEmailToolNamed(
 *   'send_support_email',
 *   'support-gmail'
 * )
 *
 * const sendSalesEmail = createGmailSendEmailToolNamed(
 *   'send_sales_email',
 *   'sales-gmail'
 * )
 */
export function createGmailSendEmailToolNamed(toolName: string, credentialName: string): Tool {
  return createIntegrationTool({
    name: toolName,
    description: `Send an email via Gmail API using ${credentialName} credentials`,

    inputSchema: z.object({
      to: EmailSchema.describe('Recipient email address'),
      subject: z.string().describe('Email subject line'),
      body: z.string().describe('Email body (HTML supported)'),
      cc: EmailSchema.optional().describe('Optional CC recipient'),
      from: z.string().optional().describe('Optional sender display name (defaults to "me")')
    }),

    outputSchema: z.object({
      messageId: z.string().describe('Gmail message ID'),
      threadId: z.string().describe('Gmail thread ID')
    }),

    integration: 'gmail' as const,
    method: 'sendEmail' as const,
    credentialName: credentialName
  })
}
