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

const mockMutateAsync = vi.fn().mockResolvedValue({ id: 'new-1' });

vi.mock('@/api/hooks', () => ({
  useCompany: () => ({ data: undefined, isLoading: false }),
  useCreateCompany: () => ({
    mutateAsync: mockMutateAsync,
    isPending: false,
  }),
  useUpdateCompany: () => ({ mutateAsync: vi.fn(), isPending: false }),
}));

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

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

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

  it('renders email and phone fields', () => {
    render(<MemoryRouter><CompanyFormPage /></MemoryRouter>);
    expect(screen.getByLabelText(/E-Mail/)).toBeInTheDocument();
    expect(screen.getByLabelText(/Telefon/)).toBeInTheDocument();
  });

  it('renders website and description fields', () => {
    render(<MemoryRouter><CompanyFormPage /></MemoryRouter>);
    expect(screen.getByLabelText(/Website/)).toBeInTheDocument();
    expect(screen.getByLabelText(/Beschreibung/)).toBeInTheDocument();
  });

  it('renders account number and industry fields', () => {
    render(<MemoryRouter><CompanyFormPage /></MemoryRouter>);
    expect(screen.getByLabelText(/Kontonummer/)).toBeInTheDocument();
    expect(screen.getByLabelText(/Branche/)).toBeInTheDocument();
  });

  it('submit button is present', () => {
    render(<MemoryRouter><CompanyFormPage /></MemoryRouter>);
    expect(screen.getByTestId('company-submit-btn')).toBeInTheDocument();
  });

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

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

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

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

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

  it('accepts valid form data and submits', async () => {
    render(<MemoryRouter><CompanyFormPage /></MemoryRouter>);
    fireEvent.change(screen.getByTestId('company-name-input'), { target: { value: 'Acme Corp' } });
    fireEvent.click(screen.getByTestId('company-submit-btn'));
    await waitFor(() => {
      expect(mockMutateAsync).toHaveBeenCalledWith(expect.objectContaining({ name: 'Acme Corp' }));
    });
  });
});
