import React, { ReactElement } from 'react'
import { render, RenderOptions } from '@testing-library/react'
import { mockUser } from '../fixtures/mock-data'

// Mock AuthProvider component for tests
export const MockAuthProvider = ({ children }: { children: React.ReactNode }) => {
  return <>{children}</>
}

// Custom render function that includes providers
export const renderWithProviders = (
  ui: ReactElement,
  options?: Omit<RenderOptions, 'wrapper'>
) => {
  const Wrapper = ({ children }: { children: React.ReactNode }) => {
    return <MockAuthProvider>{children}</MockAuthProvider>
  }

  return render(ui, { wrapper: Wrapper, ...options })
}

// Helper to create mock form data
export const createMockFormData = (overrides?: Partial<any>) => {
  return {
    email: 'test@example.com',
    password: 'Test@1234',
    name: 'Test User',
    ...overrides,
  }
}

// Helper to create mock portfolio data
export const createMockPortfolio = (overrides?: Partial<any>) => {
  return {
    id: '1',
    userId: '1',
    name: 'Test Portfolio',
    domain: 'test.com',
    status: 'Uploaded',
    createdAt: new Date().toISOString(),
    updatedAt: new Date().toISOString(),
    ...overrides,
  }
}

// Helper to wait for async operations
export const waitForAsync = () =>
  new Promise((resolve) => setTimeout(resolve, 0))

// Helper to create mock file for upload tests
export const createMockFile = (name = 'portfolio.zip', size = 1024) => {
  const blob = new Blob(['a'.repeat(size)], { type: 'application/zip' })
  return new File([blob], name, { type: 'application/zip' })
}

// Helper to test form validation
export const getFormError = (element: HTMLElement, fieldName: string) => {
  return element.querySelector(`[data-testid="${fieldName}-error"]`)?.textContent
}

// Helper to simulate user typing in form field
export const typeInFormField = async (
  element: HTMLInputElement,
  value: string,
  { delay = 0 } = {}
) => {
  element.value = value
  element.dispatchEvent(new Event('change', { bubbles: true }))
  if (delay) {
    await new Promise((resolve) => setTimeout(resolve, delay))
  }
}

// Re-export everything from React Testing Library
export * from '@testing-library/react'
export { default as userEvent } from '@testing-library/user-event'
