import type { APIRequestContext } from '@playwright/test'
import type { MockEmailHelper } from './mock-email'
import { createMockEmailHelper } from './mock-email'
import type { MailpitHelper } from './mailpit'
import { createMailpitHelper } from './mailpit'

/**
 * Unified email helper interface
 * Supports both mock email mode (log parsing) and Mailpit (SMTP testing)
 */
export type EmailHelper = MockEmailHelper | MailpitHelper

/**
 * Create appropriate email helper based on environment configuration
 *
 * Usage in tests:
 * ```typescript
 * test('should send verification email', async ({ page, request }) => {
 *   const emailHelper = createEmailHelper(request)
 *   await emailHelper.deleteAllMessages()
 *
 *   // ... perform actions that trigger email ...
 *
 *   const email = await emailHelper.waitForEmail('user@example.com')
 *   const verificationLink = await emailHelper.getVerificationLink(email.ID)
 *   await page.goto(verificationLink!)
 * })
 * ```
 */
export function createEmailHelper(request: APIRequestContext): EmailHelper {
  // Check if mock email mode is enabled via environment variable
  // Default to true for better platform compatibility
  const isMockMode = process.env.NUXT_MOCK_EMAIL !== 'false'

  if (isMockMode) {
    return createMockEmailHelper(request)
  } else {
    return createMailpitHelper(request)
  }
}

// Re-export types and helpers for convenience
export type { MockEmailMessage } from './mock-email'
export type { MailpitMessage } from './mailpit'
export { createMockEmailHelper, MockEmailHelper } from './mock-email'
export { createMailpitHelper, MailpitHelper } from './mailpit'
