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

vi.mock('@/api/hooks', () => ({
  useContact: () => ({ data: undefined, isLoading: false }),
  useCreateContact: () => ({
    mutateAsync: vi.fn().mockResolvedValue({ id: 'new-1' }),
    isPending: false,
  }),
  useUpdateContact: () => ({ mutateAsync: vi.fn(), isPending: false }),
  useCompanies: () => ({
    data: {
      items: [
        { id: '1', name: 'TestCorp GmbH', industry: 'IT' },
        { id: '2', name: 'Müller AG', industry: 'Logistik' },
        { id: '3', name: 'Schmidt GmbH', industry: 'Handel' },
      ],
      total: 3,
    },
    isLoading: false,
  }),
}));

vi.mock('@/components/ui/Toast', () => ({
  useToast: () => ({ success: vi.fn(), error: vi.fn(), info: vi.fn() }),
}));

vi.mock('@/components/shared/UnsavedChangesGuard', () => ({
  UnsavedChangesGuard: () => null,
}));

describe('ContactFormPage', () => {
  it('renders form with first name field', () => {
    render(<MemoryRouter><ContactFormPage /></MemoryRouter>);
    expect(screen.getByLabelText(/Vorname/)).toBeInTheDocument();
  });

  it('renders form with last name field', () => {
    render(<MemoryRouter><ContactFormPage /></MemoryRouter>);
    expect(screen.getByLabelText(/Nachname/)).toBeInTheDocument();
  });

  it('renders email field', () => {
    render(<MemoryRouter><ContactFormPage /></MemoryRouter>);
    expect(screen.getByLabelText(/E-Mail/)).toBeInTheDocument();
  });

  it('renders phone and position fields', () => {
    render(<MemoryRouter><ContactFormPage /></MemoryRouter>);
    expect(screen.getByLabelText(/Telefon/)).toBeInTheDocument();
    expect(screen.getByLabelText(/Position/)).toBeInTheDocument();
  });

  it('renders submit button', () => {
    render(<MemoryRouter><ContactFormPage /></MemoryRouter>);
    expect(screen.getByTestId('contact-submit-btn')).toBeInTheDocument();
  });

  it('renders cancel button', () => {
    render(<MemoryRouter><ContactFormPage /></MemoryRouter>);
    expect(screen.getByText('Abbrechen')).toBeInTheDocument();
  });

  it('renders form page with correct test id', () => {
    render(<MemoryRouter><ContactFormPage /></MemoryRouter>);
    expect(screen.getByTestId('contact-form-page')).toBeInTheDocument();
  });

  it('renders create title for new contact', () => {
    render(<MemoryRouter><ContactFormPage /></MemoryRouter>);
    expect(screen.getByText('Kontakt erstellen')).toBeInTheDocument();
  });

  it('first name field is marked as required', () => {
    render(<MemoryRouter><ContactFormPage /></MemoryRouter>);
    const firstNameInput = screen.getByTestId('contact-first-name-input');
    expect(firstNameInput).toHaveAttribute('required');
  });

  it('last name field is marked as required', () => {
    render(<MemoryRouter><ContactFormPage /></MemoryRouter>);
    const lastNameInput = screen.getByTestId('contact-last-name-input');
    expect(lastNameInput).toHaveAttribute('required');
  });

  it('renders company assignment section with available companies', () => {
    render(<MemoryRouter><ContactFormPage /></MemoryRouter>);
    expect(screen.getByTestId('company-assignment-list')).toBeInTheDocument();
    expect(screen.getByText('TestCorp GmbH')).toBeInTheDocument();
    expect(screen.getByText('Müller AG')).toBeInTheDocument();
    expect(screen.getByText('Schmidt GmbH')).toBeInTheDocument();
  });

  it('can select multiple companies for assignment', () => {
    render(<MemoryRouter><ContactFormPage /></MemoryRouter>);
    const checkboxes = screen.getAllByRole('checkbox');
    expect(checkboxes).toHaveLength(3);
    fireEvent.click(checkboxes[0]);
    fireEvent.click(checkboxes[1]);
    expect(checkboxes[0]).toBeChecked();
    expect(checkboxes[1]).toBeChecked();
    expect(checkboxes[2]).not.toBeChecked();
  });

  it('shows inline validation error when submitting empty form', async () => {
    render(<MemoryRouter><ContactFormPage /></MemoryRouter>);
    fireEvent.click(screen.getByTestId('contact-submit-btn'));
    await waitFor(() => {
      expect(screen.getByText(/Vorname ist erforderlich/)).toBeInTheDocument();
      expect(screen.getByText(/Nachname ist erforderlich/)).toBeInTheDocument();
    });
  });
});
