import React from 'react';
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import { SettingsSequencesPage } from '@/pages/SettingsSequences';

const mockSequences = [
  { id: 's1', name: 'Rechnungen', prefix: 'RE-', next_number: 1, padding: 4 },
  { id: 's2', name: 'Angebote', prefix: 'AN-', next_number: 5, padding: 3 },
];

const apiGetMock = vi.fn();
const apiPostMock = vi.fn();
const apiPatchMock = vi.fn();
const apiDeleteMock = vi.fn();

vi.mock('@/api/client', () => ({
  apiGet: (...args: any[]) => apiGetMock(...args),
  apiPost: (...args: any[]) => apiPostMock(...args),
  apiPatch: (...args: any[]) => apiPatchMock(...args),
  apiDelete: (...args: any[]) => apiDeleteMock(...args),
}));

beforeEach(() => {
  vi.clearAllMocks();
  apiGetMock.mockResolvedValue({ items: mockSequences, total: 2 });
  apiPostMock.mockResolvedValue({ id: 's3', name: 'Aufträge', prefix: 'AU-', next_number: 1, padding: 4 });
  apiPatchMock.mockResolvedValue({});
  apiDeleteMock.mockResolvedValue({});
});

describe('SettingsSequencesPage', () => {
  it('renders sequences page', async () => {
    render(<SettingsSequencesPage />);
    await waitFor(() => {
      expect(screen.getByTestId('settings-sequences')).toBeInTheDocument();
    });
  });

  it('renders add sequence button', async () => {
    render(<SettingsSequencesPage />);
    await waitFor(() => {
      expect(screen.getByTestId('settings-sequences')).toBeInTheDocument();
    });
    expect(screen.getByText(/nummernkreis hinzufügen|add sequence/i)).toBeInTheDocument();
  });

  it('renders sequence list from API', async () => {
    render(<SettingsSequencesPage />);
    await waitFor(() => {
      expect(screen.getByText('Rechnungen')).toBeInTheDocument();
      expect(screen.getByText('Angebote')).toBeInTheDocument();
    });
  });

  it('shows form when add button clicked', async () => {
    render(<SettingsSequencesPage />);
    await waitFor(() => {
      expect(screen.getByTestId('settings-sequences')).toBeInTheDocument();
    });
    fireEvent.click(screen.getByText(/nummernkreis hinzufügen|add sequence/i));
    await waitFor(() => {
      expect(screen.getByRole('button', { name: /speichern|^save$/i })).toBeInTheDocument();
    });
  });

  it('displays error when API fails', async () => {
    apiGetMock.mockRejectedValueOnce(new Error('Network error'));
    render(<SettingsSequencesPage />);
    await waitFor(() => {
      expect(screen.getByText('Network error')).toBeInTheDocument();
    });
  });

  it('calls apiPost on valid submit', async () => {
    render(<SettingsSequencesPage />);
    await waitFor(() => {
      expect(screen.getByTestId('settings-sequences')).toBeInTheDocument();
    });
    fireEvent.click(screen.getByText(/nummernkreis hinzufügen|add sequence/i));
    await waitFor(() => {
      expect(screen.getByRole('button', { name: /speichern|^save$/i })).toBeInTheDocument();
    });
    const inputs = screen.getAllByRole('textbox');
    fireEvent.change(inputs[0], { target: { value: 'Aufträge' } });
    fireEvent.change(inputs[1], { target: { value: 'AU-' } });
    fireEvent.click(screen.getByRole('button', { name: /speichern|^save$/i }));
    await waitFor(() => {
      expect(apiPostMock).toHaveBeenCalledWith('/sequences', expect.objectContaining({ name: 'Aufträge' }));
    });
  });
});
