import { render, screen } from '@testing-library/react';
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { BrowserRouter } from 'react-router-dom';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { ProfilePage } from './ProfilePage';

// Mock useTranslation
vi.mock('@/hooks/useTranslation', () => ({
  useTranslation: () => ({
    t: (key: string) => key,
    i18n: { changeLanguage: vi.fn() },
    language: 'fr',
    changeLanguage: vi.fn(),
    isReady: true,
  }),
}));

// Mock useAuthStore
vi.mock('@/features/auth/store/authStore', () => ({
  useAuthStore: () => ({
    user: {
      id: '1',
      username: 'testuser',
      email: 'test@example.com',
      first_name: 'Test',
      last_name: 'User',
    },
    refreshUser: vi.fn(),
    isAuthenticated: true,
  }),
}));

// Mock useUIStore
vi.mock('@/stores/ui', () => ({
  useUIStore: () => ({
    addNotification: vi.fn(),
  }),
}));

// Mock apiClient
vi.mock('@/services/api/client', () => ({
  apiClient: {
    put: vi.fn(),
  },
}));

const createTestQueryClient = () =>
  new QueryClient({
    defaultOptions: {
      queries: { retry: false },
      mutations: { retry: false },
    },
  });

const TestWrapper = ({ children }: { children: React.ReactNode }) => {
  const queryClient = createTestQueryClient();
  return (
    <QueryClientProvider client={queryClient}>
      <BrowserRouter>{children}</BrowserRouter>
    </QueryClientProvider>
  );
};

describe('ProfilePage Component', () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it('renders profile page correctly', () => {
    render(
      <TestWrapper>
        <ProfilePage />
      </TestWrapper>,
    );

    // Vérifier que la page a la structure correcte avec padding
    const pageContent = document.querySelector('.p-6');
    expect(pageContent).toBeInTheDocument();
  });

  it('renders ProfileForm component', () => {
    render(
      <TestWrapper>
        <ProfilePage />
      </TestWrapper>,
    );

    // Le ProfileForm devrait être rendu
    expect(document.querySelector('.p-6')).toBeInTheDocument();
  });
});
