import React from 'react';
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { screen, fireEvent, waitFor } from '@testing-library/react';
import { AuthManagement } from '../../src/components/AuthManagement';
import { LoginPage } from '../../src/components/LoginPage';
import { renderWithProviders } from '../test-utils';

// Mock dependencies
vi.mock('react-router', () => ({
  useNavigate: () => vi.fn(),
}));

vi.mock('sonner', () => ({
  toast: {
    success: vi.fn(),
    error: vi.fn(),
  },
}));

// Mock shared UI components - 简化mock避免复杂渲染问题
vi.mock('@d8d/shared-ui-components', () => ({
  Button: ({ children, ...props }: any) => React.createElement('button', { ...props, 'data-testid': 'button' }, children),
  Card: ({ children, ...props }: any) => React.createElement('div', { ...props, 'data-testid': 'card' }, children),
  CardContent: ({ children, ...props }: any) => React.createElement('div', { ...props, 'data-testid': 'card-content' }, children),
  CardDescription: ({ children, ...props }: any) => React.createElement('div', { ...props, 'data-testid': 'card-description' }, children),
  CardFooter: ({ children, ...props }: any) => React.createElement('div', { ...props, 'data-testid': 'card-footer' }, children),
  CardHeader: ({ children, ...props }: any) => React.createElement('div', { ...props, 'data-testid': 'card-header' }, children),
  CardTitle: ({ children, ...props }: any) => React.createElement('div', { ...props, 'data-testid': 'card-title' }, children),
  Form: ({ children, ...props }: any) => React.createElement('div', { ...props, 'data-testid': 'form' }, children),
  FormControl: ({ children, ...props }: any) => React.createElement('div', { ...props, 'data-testid': 'form-control' }, children),
  FormField: ({ children, ...props }: any) => React.createElement('div', { ...props, 'data-testid': 'form-field' }, children),
  FormItem: ({ children, ...props }: any) => React.createElement('div', { ...props, 'data-testid': 'form-item' }, children),
  FormLabel: ({ children, ...props }: any) => React.createElement('label', { ...props, 'data-testid': 'form-label' }, children),
  FormMessage: ({ children, ...props }: any) => React.createElement('div', { ...props, 'data-testid': 'form-message' }, children),
  Input: ({ ...props }: any) => React.createElement('input', { ...props, 'data-testid': 'input' }),
}));

describe('AuthManagement Integration', () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it('应该渲染默认登录页面', () => {
    renderWithProviders(React.createElement(AuthManagement));

    // 验证基本组件结构
    expect(screen.getByTestId('card')).toBeInTheDocument();
    expect(screen.getByTestId('form')).toBeInTheDocument();
    expect(screen.getByTestId('button')).toBeInTheDocument();
  });

  it('应该支持自定义子组件', () => {
    const CustomComponent = () => React.createElement('div', { 'data-testid': 'custom-component' }, 'Custom Auth Component');

    renderWithProviders(
      React.createElement(AuthManagement, null,
        React.createElement(CustomComponent)
      )
    );

    expect(screen.getByTestId('custom-component')).toBeInTheDocument();
  });

  it('应该支持自定义登录页面', () => {
    const CustomLoginPage = () => React.createElement('div', { 'data-testid': 'custom-login-page' }, 'Custom Login Page');

    renderWithProviders(React.createElement(AuthManagement, { customLoginPage: CustomLoginPage }));

    expect(screen.getByTestId('custom-login-page')).toBeInTheDocument();
  });
});