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

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

export interface SendEmailInput {
  to: string;
  subject: string;
  html: string;
  text?: string;
}

export interface SendTemplateEmailInput {
  to: string;
  template: "otp" | "welcome" | "password-reset" | "demo-confirmation";
  language: "ar" | "en";
  data: Record<string, any>;
}

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

/**
 * Send a raw email.
 *
 * @param input - Email data
 * @returns Send result
 *
 * @example
 * ```typescript
 * const result = await sendEmail({
 *   to: 'user@example.com',
 *   subject: 'Test Email',
 *   html: '<p>Hello World</p>',
 *   text: 'Hello World'
 * });
 *
 * console.log(result.success ? '✓ Email sent' : '✗ Send failed');
 * ```
 */
export async function sendEmail(input: SendEmailInput): Promise<EmailResponse> {
  return callMCPTool<EmailResponse>("api__send_email", input);
}

/**
 * Send a template-based email (bilingual support).
 *
 * @param input - Template email data
 * @returns Send result
 *
 * @example
 * ```typescript
 * // Send OTP email in Arabic
 * const result = await sendTemplateEmail({
 *   to: 'user@example.com',
 *   template: 'otp',
 *   language: 'ar',
 *   data: { otp: '123456', name: 'أحمد' }
 * });
 *
 * // Send welcome email in English
 * await sendTemplateEmail({
 *   to: 'new@example.com',
 *   template: 'welcome',
 *   language: 'en',
 *   data: { name: 'John', companyName: 'Acme Corp' }
 * });
 * ```
 */
export async function sendTemplateEmail(
  input: SendTemplateEmailInput,
): Promise<EmailResponse> {
  return callMCPTool<EmailResponse>("api__send_template_email", input);
}

/**
 * Test email configuration and connectivity.
 *
 * @returns Connection test result
 *
 * @example
 * ```typescript
 * const test = await testEmailConnection();
 * console.log(test.success ? '✓ Email configured correctly' : '✗ Email config issue');
 * ```
 */
export async function testEmailConnection(): Promise<{
  success: boolean;
  message: string;
}> {
  return callMCPTool("api__test_email_connection", {});
}

export default {
  sendEmail,
  sendTemplateEmail,
  testEmailConnection,
};
