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 { CompanyDetailPage } from '@/pages/CompanyDetail';

const mockCompany = {
  id: '1',
  name: 'TestCorp GmbH',
  email: 'info@testcorp.de',
  phone: '+49 30 12345678',
  website: 'https://testcorp.de',
  industry: 'IT & Software',
  account_number: 'K-00123',
  description: 'Ein führendes IT-Unternehmen',
  created_at: '2025-01-01T00:00:00Z',
  updated_at: '2025-01-01T00:00:00Z',
  contacts: [
    { id: 'c1', first_name: 'Max', last_name: 'Mustermann', email: 'max@testcorp.de', position: 'CEO' },
    { id: 'c2', first_name: 'Anna', last_name: 'Schmidt', email: 'anna@testcorp.de', position: 'CTO' },
  ],
};

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

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

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

  it('renders company name as heading', () => {
    render(<MemoryRouter initialEntries={['/companies/1']}><CompanyDetailPage /></MemoryRouter>);
    expect(screen.getByRole('heading', { level: 1, name: 'TestCorp GmbH' })).toBeInTheDocument();
  });

  it('renders tabs for detail sections', () => {
    render(<MemoryRouter initialEntries={['/companies/1']}><CompanyDetailPage /></MemoryRouter>);
    expect(screen.getByRole('tablist')).toBeInTheDocument();
    expect(screen.getByRole('tab', { name: /Übersicht/ })).toBeInTheDocument();
    expect(screen.getByRole('tab', { name: /Kontakte/ })).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={['/companies/1']}><CompanyDetailPage /></MemoryRouter>);
    expect(screen.getByText('K-00123')).toBeInTheDocument();
    expect(screen.getByText('IT & Software')).toBeInTheDocument();
    expect(screen.getByText('+49 30 12345678')).toBeInTheDocument();
  });

  it('renders contacts tab with contact list and badge count', () => {
    render(<MemoryRouter initialEntries={['/companies/1']}><CompanyDetailPage /></MemoryRouter>);
    const contactsTab = screen.getByRole('tab', { name: /Kontakte/ });
    fireEvent.click(contactsTab);
    expect(screen.getByText('Max Mustermann')).toBeInTheDocument();
    expect(screen.getByText('Anna Schmidt')).toBeInTheDocument();
  });

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

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

  it('shows error state when company not found', () => {
    mockReturnData = { data: undefined, isLoading: false, isError: true };
    render(<MemoryRouter initialEntries={['/companies/999']}><CompanyDetailPage /></MemoryRouter>);
    expect(screen.getByText('Firma nicht gefunden.')).toBeInTheDocument();
    mockReturnData = { data: mockCompany, isLoading: false, isError: false };
  });

  it('shows skeleton loading state', () => {
    mockReturnData = { data: undefined, isLoading: true, isError: false };
    const { container } = render(<MemoryRouter initialEntries={['/companies/1']}><CompanyDetailPage /></MemoryRouter>);
    expect(screen.getByTestId('company-detail-page')).toBeInTheDocument();
    mockReturnData = { data: mockCompany, isLoading: false, isError: false };
  });
});
