// Mock database and Stripe for testing
// In a real scenario, these would be imported from the actual modules
// For now, we'll create mock functions that don't require database access

/**
 * Generate a unique test email
 */
export function generateTestEmail(): string {
  const timestamp = Date.now();
  const random = Math.random().toString(36).substring(7);
  return `test_${timestamp}_${random}@example.com`;
}

/**
 * Create a test user in the database
 */
export async function createTestUser(userData: {
  email: string;
  name: string;
  password?: string;
}) {
  // Mock implementation for testing
  console.log(`Mock: Creating test user ${userData.email}`);
  return {
    id: 'test_user_id',
    email: userData.email,
    name: userData.name,
    createdAt: new Date(),
  };
}

/**
 * Clean up test user and related data
 */
export async function cleanupTestUser(email: string) {
  // Mock implementation for testing
  console.log(`Mock: Cleaning up test user ${email}`);
  return true;
}

/**
 * Mock Stripe checkout session for testing
 */
export async function mockStripeCheckout(sessionId: string) {
  return {
    id: sessionId,
    object: 'checkout.session',
    customer: 'cus_test123',
    customer_details: {
      email: 'test@example.com',
    },
    metadata: {
      userId: 'test_user_id',
    },
    mode: 'subscription',
    payment_status: 'paid',
    status: 'complete',
    subscription: 'sub_test123',
    success_url: 'http://localhost:9000/en/signup/success',
  };
}

/**
 * Generate test company data
 */
export function generateCompanyData() {
  const timestamp = Date.now();
  return {
    companyName: `Test Company ${timestamp}`,
    industry: 'healthcare',
    companySize: '11-50',
    country: 'UAE',
    city: 'dubai',
    phoneNumber: '+971 50 ' + Math.floor(Math.random() * 9000000 + 1000000),
    website: `www.testcompany${timestamp}.com`,
  };
}

/**
 * Wait for element with retry
 */
export async function waitForElement(
  page: any,
  selector: string,
  options = { timeout: 10000 }
) {
  const maxRetries = 3;
  let retries = 0;

  while (retries < maxRetries) {
    try {
      await page.waitForSelector(selector, options);
      return true;
    } catch (error) {
      retries++;
      if (retries === maxRetries) {
        throw error;
      }
      await page.waitForTimeout(1000);
    }
  }
  return false;
}

/**
 * Check if running in CI environment
 */
export function isCI(): boolean {
  return process.env.CI === 'true' || process.env.GITHUB_ACTIONS === 'true';
}

/**
 * Get base URL for tests
 */
export function getBaseUrl(): string {
  if (process.env.BASE_URL) {
    return process.env.BASE_URL;
  }
  return isCI() ? 'http://localhost:9000' : 'http://localhost:9000';
}

/**
 * Setup test database
 */
export async function setupTestDatabase() {
  // Mock implementation for testing
  console.log('Mock: Setting up test database');
  return true;
}

/**
 * Clear test data from database
 */
export async function clearTestData() {
  // Mock implementation for testing
  console.log('Mock: Clearing test data');
  return true;
}

/**
 * Generate random phone number for testing
 */
export function generatePhoneNumber(countryCode: string = '+971'): string {
  const areaCode = Math.floor(Math.random() * 90 + 10);
  const number = Math.floor(Math.random() * 9000000 + 1000000);
  return `${countryCode} ${areaCode} ${number}`;
}

/**
 * Mock OTP for testing
 */
export function generateMockOTP(): string {
  return Math.floor(100000 + Math.random() * 900000).toString();
}

/**
 * Validate email format
 */
export function isValidEmail(email: string): boolean {
  const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
  return emailRegex.test(email);
}

/**
 * Validate URL format
 */
export function isValidUrl(url: string): boolean {
  try {
    new URL(url.startsWith('http') ? url : `https://${url}`);
    return true;
  } catch {
    return false;
  }
}