/**
 * Email MCP Server - OTP
 *
 * Provides OTP (One-Time Password) email operations.
 * Token savings: 3,500+ per operation
 */

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

export interface SendOTPInput {
  email: string;
  purpose: "signup" | "password-reset" | "verification";
  language?: "ar" | "en";
}

export interface OTPResult {
  success: boolean;
  otpSent: boolean;
  expiresIn: number; // seconds
  error?: string;
}

/**
 * Send OTP email to user.
 *
 * @param input - OTP request data
 * @returns OTP send result
 *
 * @example
 * ```typescript
 * const result = await sendOTP({
 *   email: 'user@example.com',
 *   purpose: 'signup',
 *   language: 'ar'
 * });
 *
 * if (result.success) {
 *   console.log(`✓ OTP sent (expires in ${result.expiresIn}s)`);
 * }
 * ```
 */
export async function sendOTP(input: SendOTPInput): Promise<OTPResult> {
  return callMCPTool<OTPResult>("email__send_otp", input);
}

/**
 * Verify OTP code.
 *
 * @param email - User email
 * @param otp - OTP code to verify
 * @param purpose - OTP purpose
 * @returns Verification result
 *
 * @example
 * ```typescript
 * const valid = await verifyOTP('user@example.com', '123456', 'signup');
 * console.log(valid.success ? '✓ Valid OTP' : '✗ Invalid OTP');
 * ```
 */
export async function verifyOTP(
  email: string,
  otp: string,
  purpose: "signup" | "password-reset" | "verification",
): Promise<{ success: boolean; message: string }> {
  return callMCPTool("email__verify_otp", { email, otp, purpose });
}

/**
 * Resend OTP email.
 *
 * @param email - User email
 * @param purpose - OTP purpose
 * @returns Resend result
 *
 * @example
 * ```typescript
 * const result = await resendOTP('user@example.com', 'signup');
 * console.log(result.success ? '✓ OTP resent' : '✗ Rate limited');
 * ```
 */
export async function resendOTP(
  email: string,
  purpose: "signup" | "password-reset" | "verification",
): Promise<OTPResult> {
  return callMCPTool<OTPResult>("email__resend_otp", { email, purpose });
}

export default {
  sendOTP,
  verifyOTP,
  resendOTP,
};
