/**
 * Email MCP Server - Send
 *
 * Provides email sending operations with bilingual support.
 * Token savings: 4,000+ per operation
 */

import { callMCPTool } from "../../client.js";

export interface SendEmailInput {
  to: string;
  subject: string;
  html: string;
  text?: string;
  language?: "ar" | "en";
}

export interface SendBilingualEmailInput {
  to: string;
  template: string;
  data: Record<string, any>;
  preferredLanguage?: "ar" | "en";
}

export interface EmailResult {
  success: boolean;
  messageId?: string;
  error?: string;
}

/**
 * Send an email using configured SMTP.
 *
 * @param input - Email data
 * @returns Send result
 *
 * @example
 * ```typescript
 * const result = await sendEmail({
 *   to: 'user@example.com',
 *   subject: 'Welcome to Mawidi',
 *   html: '<h1>Welcome!</h1>',
 *   text: 'Welcome!',
 *   language: 'en'
 * });
 *
 * console.log(result.success ? '✓ Email sent' : '✗ Failed');
 * ```
 */
export async function sendEmail(input: SendEmailInput): Promise<EmailResult> {
  return callMCPTool<EmailResult>("email__send", input);
}

/**
 * Send bilingual email (both Arabic and English).
 *
 * @param input - Template and data
 * @returns Send result
 *
 * @example
 * ```typescript
 * const result = await sendBilingualEmail({
 *   to: 'user@example.com',
 *   template: 'welcome',
 *   data: {
 *     name: 'John Doe',
 *     companyName: 'Acme Corp'
 *   },
 *   preferredLanguage: 'en'
 * });
 *
 * console.log(`Email sent in ${result.success ? 'both languages' : 'preferred language only'}`);
 * ```
 */
export async function sendBilingualEmail(
  input: SendBilingualEmailInput,
): Promise<EmailResult> {
  return callMCPTool<EmailResult>("email__send_bilingual", input);
}

/**
 * Send bulk emails efficiently.
 *
 * @param emails - Array of email inputs
 * @returns Array of send results
 *
 * @example
 * ```typescript
 * const emails = users.map(u => ({
 *   to: u.email,
 *   subject: 'Newsletter',
 *   html: '<p>Monthly update...</p>'
 * }));
 *
 * const results = await sendBulkEmails(emails);
 * const sent = results.filter(r => r.success).length;
 * console.log(`Sent: ${sent}/${results.length} emails`);
 * ```
 */
export async function sendBulkEmails(
  emails: SendEmailInput[],
): Promise<EmailResult[]> {
  return callMCPTool<EmailResult[]>("email__send_bulk", { emails });
}

export default {
  sendEmail,
  sendBilingualEmail,
  sendBulkEmails,
};
