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

const mockSettings = {
  company_name: 'Test GmbH',
  company_legal_form: 'GmbH',
  company_street: 'Teststr. 1',
  company_city: 'Berlin',
  company_zip: '10115',
  company_country: 'DE',
  tax_number: '12345',
  vat_id: 'DE123456789',
  iban: '',
  bic: '',
  bank_name: '',
  ceo: 'Max Mustermann',
  trade_register: 'HRB 123',
  default_currency_id: 'c1',
  default_tax_id: 't1',
  invoice_prefix: 'RE-',
  quote_prefix: 'AN-',
  payment_terms_days: 14,
};

vi.mock('@/api/hooks', () => ({
  useSystemSettings: () => ({ data: mockSettings, isLoading: false }),
  useUpdateSystemSettings: () => ({ mutateAsync: vi.fn(), isPending: false }),
  useCurrencies: () => ({ data: { items: [{ id: 'c1', code: 'EUR', name: 'Euro', symbol: '€', is_default: true }] } }),
  useTaxes: () => ({ data: { items: [{ id: 't1', name: 'MwSt 19%', rate: 19, is_default: true }] } }),
}));

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

function renderPage() {
  return render(<MemoryRouter><SettingsSystemPage /></MemoryRouter>);
}

describe('SettingsSystemPage', () => {
  it('renders system settings page', () => {
    renderPage();
    expect(screen.getByTestId('settings-system-page')).toBeInTheDocument();
  });

  it('renders company name input with loaded value', () => {
    renderPage();
    expect(screen.getByTestId('settings-company-name')).toBeInTheDocument();
    expect(screen.getByDisplayValue('Test GmbH')).toBeInTheDocument();
  });

  it('renders save button', () => {
    renderPage();
    expect(screen.getByTestId('settings-system-save')).toBeInTheDocument();
  });

  it('renders currency and tax selects', () => {
    renderPage();
    const selects = screen.getAllByRole('combobox');
    expect(selects.length).toBeGreaterThanOrEqual(2);
  });

  it('renders payment terms input', () => {
    renderPage();
    expect(screen.getByDisplayValue('14')).toBeInTheDocument();
  });
});
