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

const mockContact = {
  id: '1',
  first_name: 'Max',
  last_name: 'Mustermann',
  email: 'max@example.com',
  phone: '+49 30 12345678',
  position: 'CEO',
  company_ids: ['1', '2'],
  companies: [
    { id: '1', name: 'TestCorp GmbH', industry: 'IT', email: 'info@testcorp.de' },
    { id: '2', name: 'Müller AG', industry: 'Logistik', email: 'kontakt@mueller.de' },
  ],
  created_at: '2025-01-01T00:00:00Z',
  updated_at: '2025-01-01T00:00:00Z',
};

let mockReturnData: any = { data: mockContact, isLoading: false, isError: false };

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

describe('ContactDetailPage', () => {
  it('renders the detail page', () => {
    render(<MemoryRouter initialEntries={['/contacts/1']}><ContactDetailPage /></MemoryRouter>);
    expect(screen.getByTestId('contact-detail-page')).toBeInTheDocument();
  });

  it('renders contact name as heading', () => {
    render(<MemoryRouter initialEntries={['/contacts/1']}><ContactDetailPage /></MemoryRouter>);
    expect(screen.getByText('Max Mustermann')).toBeInTheDocument();
  });

  it('renders tabs for detail sections', () => {
    render(<MemoryRouter initialEntries={['/contacts/1']}><ContactDetailPage /></MemoryRouter>);
    expect(screen.getByRole('tablist')).toBeInTheDocument();
    expect(screen.getByRole('tab', { name: /Übersicht/ })).toBeInTheDocument();
    expect(screen.getByRole('tab', { name: /Firmen/ })).toBeInTheDocument();
    expect(screen.getByRole('tab', { name: /Dateien/ })).toBeInTheDocument();
    expect(screen.getByRole('tab', { name: /Aktivität/ })).toBeInTheDocument();
  });

  it('renders overview tab content by default', () => {
    render(<MemoryRouter initialEntries={['/contacts/1']}><ContactDetailPage /></MemoryRouter>);
    expect(screen.getByText('CEO')).toBeInTheDocument();
    expect(screen.getByText('max@example.com')).toBeInTheDocument();
    expect(screen.getByText('+49 30 12345678')).toBeInTheDocument();
  });

  it('renders companies tab with assigned companies', () => {
    render(<MemoryRouter initialEntries={['/contacts/1']}><ContactDetailPage /></MemoryRouter>);
    const companiesTab = screen.getByRole('tab', { name: /Firmen/ });
    fireEvent.click(companiesTab);
    expect(screen.getByText('TestCorp GmbH')).toBeInTheDocument();
    expect(screen.getByText('Müller AG')).toBeInTheDocument();
  });

  it('renders edit button', () => {
    render(<MemoryRouter initialEntries={['/contacts/1']}><ContactDetailPage /></MemoryRouter>);
    expect(screen.getByText('Bearbeiten')).toBeInTheDocument();
  });

  it('renders back button', () => {
    render(<MemoryRouter initialEntries={['/contacts/1']}><ContactDetailPage /></MemoryRouter>);
    expect(screen.getByText(/Zurück/)).toBeInTheDocument();
  });

  it('shows error state when contact not found', () => {
    mockReturnData = { data: undefined, isLoading: false, isError: true };
    render(<MemoryRouter initialEntries={['/contacts/999']}><ContactDetailPage /></MemoryRouter>);
    expect(screen.getByText('Kontakt nicht gefunden.')).toBeInTheDocument();
    mockReturnData = { data: mockContact, isLoading: false, isError: false };
  });
});
