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

let mockReturnValue: any = {
  data: {
    items: [
      { timestamp: '2025-06-29T10:00:00Z', user: 'anna.schmidt', action: 'create', entity: 'company', entity_id: '1', details: 'Firma erstellt' },
    ],
    total: 1,
  },
  isLoading: false,
  isError: false,
};

vi.mock('@/api/hooks', () => ({
  useAuditLog: () => mockReturnValue,
}));

describe('AuditLogPage', () => {
  beforeEach(() => {
    mockReturnValue = {
      data: {
        items: [
          { timestamp: '2025-06-29T10:00:00Z', user: 'anna.schmidt', action: 'create', entity: 'company', entity_id: '1', details: 'Firma erstellt' },
        ],
        total: 1,
      },
      isLoading: false,
      isError: false,
    };
  });

  it('renders audit log page', () => {
    render(<MemoryRouter><AuditLogPage /></MemoryRouter>);
    expect(screen.getByTestId('audit-log-page')).toBeInTheDocument();
  });

  it('renders audit log table via DataGrid', () => {
    render(<MemoryRouter><AuditLogPage /></MemoryRouter>);
    expect(screen.getByTestId('audit-log-grid')).toBeInTheDocument();
  });

  it('handles 404 error gracefully', () => {
    mockReturnValue = { data: undefined, isLoading: false, isError: true };
    render(<MemoryRouter><AuditLogPage /></MemoryRouter>);
    expect(screen.getByText(/nicht verf/)).toBeInTheDocument();
  });
});
