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

vi.mock('@/api/client', () => ({
  apiGet: vi.fn().mockResolvedValue({ items: [], total: 0 }),
  apiPost: vi.fn().mockResolvedValue({ id: '1', code: 'EUR', name: 'Euro', symbol: '€', is_default: true }),
  apiPatch: vi.fn().mockResolvedValue({}),
  apiDelete: vi.fn().mockResolvedValue({}),
}));

import { SettingsCurrenciesPage } from '@/pages/SettingsCurrencies';
import { apiPost } from '@/api/client';

beforeEach(() => {
  vi.clearAllMocks();
});

describe('SettingsCurrencies validation (RHF + Zod)', () => {
  it('shows validation error when code is empty', async () => {
    render(<SettingsCurrenciesPage />);
    await waitFor(() => {
      expect(screen.getByTestId('settings-currencies')).toBeInTheDocument();
    });
    // Click add button to show form (German locale default in test env)
    const addBtn = screen.getByText(/währung hinzufügen|add currency/i);
    fireEvent.click(addBtn);
    // Submit empty form
    const saveBtn = screen.getByRole('button', { name: /speichern|^save$/i });
    fireEvent.click(saveBtn);
    await waitFor(() => {
      const errors = screen.getAllByText(/erforderlich|required/i);
      expect(errors.length).toBeGreaterThan(0);
    });
  });

  it('submits successfully with valid data', async () => {
    render(<SettingsCurrenciesPage />);
    await waitFor(() => {
      expect(screen.getByTestId('settings-currencies')).toBeInTheDocument();
    });
    const addBtn = screen.getByText(/währung hinzufügen|add currency/i);
    fireEvent.click(addBtn);
    // Fill form — code, symbol, name
    const inputs = screen.getAllByRole('textbox');
    fireEvent.change(inputs[0], { target: { value: 'EUR' } }); // code
    fireEvent.change(inputs[1], { target: { value: '€' } }); // symbol
    fireEvent.change(inputs[2], { target: { value: 'Euro' } }); // name
    const saveBtn = screen.getByRole('button', { name: /speichern|^save$/i });
    fireEvent.click(saveBtn);
    await waitFor(() => {
      expect(apiPost).toHaveBeenCalledWith('/currencies', expect.objectContaining({ code: 'EUR', name: 'Euro', symbol: '€' }));
    });
  });
});
