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

const mockTaxes = [
  { id: 't1', name: 'MwSt 19%', rate: 19, is_default: true, country: 'DE' },
  { id: 't2', name: 'MwSt 7%', rate: 7, is_default: false, country: 'DE' },
];

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: mockTaxes, total: 2 });
  apiPostMock.mockResolvedValue({ id: 't3', name: 'MwSt 0%', rate: 0, is_default: false, country: null });
  apiPatchMock.mockResolvedValue({});
  apiDeleteMock.mockResolvedValue({});
});

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

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

  it('renders tax list from API', async () => {
    render(<SettingsTaxesPage />);
    await waitFor(() => {
      expect(screen.getByText('MwSt 19%')).toBeInTheDocument();
      expect(screen.getByText('MwSt 7%')).toBeInTheDocument();
    });
  });

  it('shows form when add button clicked', async () => {
    render(<SettingsTaxesPage />);
    await waitFor(() => {
      expect(screen.getByTestId('settings-taxes')).toBeInTheDocument();
    });
    fireEvent.click(screen.getByText(/steuersatz hinzufügen|add tax/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(<SettingsTaxesPage />);
    await waitFor(() => {
      expect(screen.getByText('Network error')).toBeInTheDocument();
    });
  });

  it('calls apiPost on valid submit', async () => {
    render(<SettingsTaxesPage />);
    await waitFor(() => {
      expect(screen.getByTestId('settings-taxes')).toBeInTheDocument();
    });
    fireEvent.click(screen.getByText(/steuersatz hinzufügen|add tax/i));
    await waitFor(() => {
      expect(screen.getByRole('button', { name: /speichern|^save$/i })).toBeInTheDocument();
    });
    const inputs = screen.getAllByRole('textbox');
    fireEvent.change(inputs[0], { target: { value: 'MwSt 0%' } });
    const numberInput = screen.getByRole('spinbutton');
    fireEvent.change(numberInput, { target: { value: '0' } });
    fireEvent.click(screen.getByRole('button', { name: /speichern|^save$/i }));
    await waitFor(() => {
      expect(apiPostMock).toHaveBeenCalledWith('/taxes', expect.objectContaining({ name: 'MwSt 0%' }));
    });
  });
});
