/**
 * Email MCP Server - Templates
 *
 * Provides email template rendering operations.
 * Token savings: 3,000+ per operation
 */

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

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

export interface RenderedTemplate {
  subject: string;
  html: string;
  text: string;
}

/**
 * Render an email template with data.
 *
 * @param input - Template and data
 * @returns Rendered email (subject, HTML, text)
 *
 * @example
 * ```typescript
 * const email = await renderTemplate({
 *   template: 'welcome',
 *   language: 'ar',
 *   data: {
 *     name: 'أحمد',
 *     companyName: 'شركة الطب'
 *   }
 * });
 *
 * console.log(`Subject: ${email.subject}`);
 * console.log(`HTML length: ${email.html.length} chars`);
 * ```
 */
export async function renderTemplate(
  input: RenderTemplateInput,
): Promise<RenderedTemplate> {
  return callMCPTool<RenderedTemplate>("email__render_template", input);
}

/**
 * Get list of available email templates.
 *
 * @returns Array of template names
 *
 * @example
 * ```typescript
 * const templates = await listTemplates();
 * console.log('Available templates:', templates);
 * ```
 */
export async function listTemplates(): Promise<string[]> {
  return callMCPTool<string[]>("email__list_templates", {});
}

/**
 * Preview template in both languages.
 *
 * @param templateName - Template to preview
 * @param data - Sample data
 * @returns Both language versions
 *
 * @example
 * ```typescript
 * const preview = await previewTemplate('welcome', {
 *   name: 'Test User',
 *   companyName: 'Test Co'
 * });
 *
 * console.log('Arabic version:', preview.ar.subject);
 * console.log('English version:', preview.en.subject);
 * ```
 */
export async function previewTemplate(
  templateName: string,
  data: Record<string, any>,
): Promise<{ ar: RenderedTemplate; en: RenderedTemplate }> {
  return callMCPTool("email__preview_template", { templateName, data });
}

export default {
  renderTemplate,
  listTemplates,
  previewTemplate,
};
