/**
 * API MCP Server - Authentication
 *
 * Provides authentication API operations (register, login, OTP, password reset).
 * Token savings: 5,000+ per operation
 *
 * These tools simulate API calls for testing and development workflows.
 */

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

export interface RegisterInput {
  email: string;
  password: string;
  name?: string;
  companyName?: string;
  phone?: string;
  industry?: string;
  location?: string;
}

export interface LoginInput {
  email: string;
  password: string;
}

export interface VerifyOTPInput {
  email: string;
  otp: string;
  purpose: "signup" | "password-reset";
}

export interface AuthResponse {
  success: boolean;
  message: string;
  userId?: string;
  token?: string;
  error?: string;
}

/**
 * Test user registration endpoint.
 *
 * @param input - Registration data
 * @returns API response
 *
 * @example
 * ```typescript
 * const result = await testRegister({
 *   email: 'test@example.com',
 *   password: 'SecurePass123!',
 *   name: 'Test User',
 *   companyName: 'Test Co',
 *   industry: 'healthcare'
 * });
 *
 * if (result.success) {
 *   console.log(`✓ User registered: ${result.userId}`);
 * } else {
 *   console.log(`✗ Registration failed: ${result.error}`);
 * }
 * ```
 */
export async function testRegister(
  input: RegisterInput,
): Promise<AuthResponse> {
  return callMCPTool<AuthResponse>("api__test_register", input);
}

/**
 * Test user login endpoint.
 *
 * @param input - Login credentials
 * @returns API response with token
 *
 * @example
 * ```typescript
 * const result = await testLogin({
 *   email: 'user@example.com',
 *   password: 'SecurePass123!'
 * });
 *
 * if (result.success) {
 *   console.log(`✓ Login successful`);
 *   console.log(`Token: ${result.token?.substring(0, 20)}...`);
 * } else {
 *   console.log(`✗ Login failed: ${result.error}`);
 * }
 * ```
 */
export async function testLogin(input: LoginInput): Promise<AuthResponse> {
  return callMCPTool<AuthResponse>("api__test_login", input);
}

/**
 * Test OTP verification endpoint.
 *
 * @param input - OTP verification data
 * @returns Verification result
 *
 * @example
 * ```typescript
 * const result = await testVerifyOTP({
 *   email: 'user@example.com',
 *   otp: '123456',
 *   purpose: 'signup'
 * });
 *
 * console.log(result.success ? '✓ OTP verified' : '✗ Invalid OTP');
 * ```
 */
export async function testVerifyOTP(
  input: VerifyOTPInput,
): Promise<AuthResponse> {
  return callMCPTool<AuthResponse>("api__test_verify_otp", input);
}

/**
 * Test forgot password endpoint.
 *
 * @param email - User email
 * @returns Send OTP result
 *
 * @example
 * ```typescript
 * const result = await testForgotPassword('user@example.com');
 * console.log(result.message); // "OTP sent to email"
 * ```
 */
export async function testForgotPassword(email: string): Promise<AuthResponse> {
  return callMCPTool<AuthResponse>("api__test_forgot_password", { email });
}

/**
 * Test password reset endpoint.
 *
 * @param email - User email
 * @param otp - Verification OTP
 * @param newPassword - New password
 * @returns Reset result
 *
 * @example
 * ```typescript
 * const result = await testResetPassword(
 *   'user@example.com',
 *   '123456',
 *   'NewSecurePass123!'
 * );
 *
 * console.log(result.success ? '✓ Password reset' : '✗ Reset failed');
 * ```
 */
export async function testResetPassword(
  email: string,
  otp: string,
  newPassword: string,
): Promise<AuthResponse> {
  return callMCPTool<AuthResponse>("api__test_reset_password", {
    email,
    otp,
    newPassword,
  });
}

/**
 * Test complete auth flow (register → verify → login).
 *
 * @param userData - User registration data
 * @returns Flow test results
 *
 * @example
 * ```typescript
 * const flow = await testAuthFlow({
 *   email: 'newuser@example.com',
 *   password: 'SecurePass123!',
 *   name: 'New User'
 * });
 *
 * console.log('Auth Flow Results:');
 * console.log(`  Registration: ${flow.register.success ? '✓' : '✗'}`);
 * console.log(`  OTP Sent: ${flow.otpSent ? '✓' : '✗'}`);
 * console.log(`  Login: ${flow.login.success ? '✓' : '✗'}`);
 * ```
 */
export async function testAuthFlow(userData: RegisterInput): Promise<{
  register: AuthResponse;
  otpSent: boolean;
  login: AuthResponse;
}> {
  return callMCPTool("api__test_auth_flow", userData);
}

export default {
  testRegister,
  testLogin,
  testVerifyOTP,
  testForgotPassword,
  testResetPassword,
  testAuthFlow,
};
