import React from 'react';
import { describe, it, expect, vi } from 'vitest';
import { render, screen } from '@testing-library/react';
import { MemoryRouter } from 'react-router-dom';
import { DashboardPage } from '@/pages/Dashboard';

vi.mock('@/api/hooks', () => ({
  useUnifiedContacts: (page: number, pageSize: number, search: any, type: string) => ({
    data: { items: [], total: type === 'company' ? 24 : 156, page: 1, page_size: 25 },
    isLoading: false,
  }),
  useAuditLog: () => ({
    data: {
      items: [
        { timestamp: '2025-06-29T10:00:00Z', user: 'anna.schmidt', action: 'create', entity: 'company', entity_id: '1', details: 'Firma erstellt' },
        { timestamp: '2025-06-28T14:00:00Z', user: 'max.mustermann', action: 'update', entity: 'contact', entity_id: '2', details: 'Kontakt aktualisiert' },
        { timestamp: '2025-06-27T09:00:00Z', user: 'admin', action: 'delete', entity: 'company', entity_id: '3', details: 'Firma gelöscht' },
      ],
      total: 3,
    },
    isError: false,
  }),
}));

describe('DashboardPage', () => {
  it('renders dashboard page', () => {
    render(<MemoryRouter><DashboardPage /></MemoryRouter>);
    expect(screen.getByTestId('dashboard-page')).toBeInTheDocument();
  });

  it('renders page title', () => {
    render(<MemoryRouter><DashboardPage /></MemoryRouter>);
    expect(screen.getByText('Dashboard')).toBeInTheDocument();
  });

  it('renders stat card for companies', () => {
    render(<MemoryRouter><DashboardPage /></MemoryRouter>);
    expect(screen.getByTestId('stat-companies')).toBeInTheDocument();
  });

  it('renders stat card for contacts', () => {
    render(<MemoryRouter><DashboardPage /></MemoryRouter>);
    expect(screen.getByTestId('stat-contacts')).toBeInTheDocument();
  });

  it('renders stat card for active this week', () => {
    render(<MemoryRouter><DashboardPage /></MemoryRouter>);
    expect(screen.getByTestId('stat-active-week')).toBeInTheDocument();
  });

  it('renders stat card for new this month', () => {
    render(<MemoryRouter><DashboardPage /></MemoryRouter>);
    expect(screen.getByTestId('stat-new-month')).toBeInTheDocument();
  });

  it('renders correct company count in stat card', () => {
    render(<MemoryRouter><DashboardPage /></MemoryRouter>);
    const statCompanies = screen.getByTestId('stat-companies');
    expect(statCompanies).toHaveTextContent('24');
  });

  it('renders correct contact count in stat card', () => {
    render(<MemoryRouter><DashboardPage /></MemoryRouter>);
    const statContacts = screen.getByTestId('stat-contacts');
    expect(statContacts).toHaveTextContent('156');
  });

  it('renders activity feed', () => {
    render(<MemoryRouter><DashboardPage /></MemoryRouter>);
    expect(screen.getByTestId('activity-feed')).toBeInTheDocument();
  });

  it('renders activity feed with user names from audit log', () => {
    render(<MemoryRouter><DashboardPage /></MemoryRouter>);
    expect(screen.getByText('anna.schmidt')).toBeInTheDocument();
    expect(screen.getByText('max.mustermann')).toBeInTheDocument();
  });

  it('renders activity feed with action descriptions', () => {
    render(<MemoryRouter><DashboardPage /></MemoryRouter>);
    expect(screen.getByText(/create/)).toBeInTheDocument();
    expect(screen.getByText(/update/)).toBeInTheDocument();
    expect(screen.getByText(/delete/)).toBeInTheDocument();
  });
});
