import React from 'react';
import { describe, it, expect, vi } from 'vitest';
import { render, screen, fireEvent } from '@testing-library/react';
import { MemoryRouter } from 'react-router-dom';
import { SettingsRolesPage } from '@/pages/SettingsRoles';

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

describe('SettingsRolesPage', () => {
  it('renders roles page', () => {
    render(<MemoryRouter><SettingsRolesPage /></MemoryRouter>);
    expect(screen.getByTestId('settings-roles-page')).toBeInTheDocument();
  });

  it('renders role editor with create button', () => {
    render(<MemoryRouter><SettingsRolesPage /></MemoryRouter>);
    expect(screen.getByTestId('create-role-btn')).toBeInTheDocument();
  });

  it('renders existing roles with permissions', () => {
    render(<MemoryRouter><SettingsRolesPage /></MemoryRouter>);
    expect(screen.getByText('Administrator')).toBeInTheDocument();
    expect(screen.getByText('Mitarbeiter')).toBeInTheDocument();
    expect(screen.getByText('Gast')).toBeInTheDocument();
  });

  it('renders edit buttons for each role', () => {
    render(<MemoryRouter><SettingsRolesPage /></MemoryRouter>);
    expect(screen.getByTestId('edit-role-1')).toBeInTheDocument();
    expect(screen.getByTestId('edit-role-2')).toBeInTheDocument();
    expect(screen.getByTestId('edit-role-3')).toBeInTheDocument();
  });

  it('opens create role modal when create button clicked', () => {
    render(<MemoryRouter><SettingsRolesPage /></MemoryRouter>);
    fireEvent.click(screen.getByTestId('create-role-btn'));
    expect(screen.getByTestId('create-role-form')).toBeInTheDocument();
  });

  it('create role form has role name input', () => {
    render(<MemoryRouter><SettingsRolesPage /></MemoryRouter>);
    fireEvent.click(screen.getByTestId('create-role-btn'));
    expect(screen.getByTestId('new-role-name')).toBeInTheDocument();
  });

  it('create role form has permission checkboxes', () => {
    render(<MemoryRouter><SettingsRolesPage /></MemoryRouter>);
    fireEvent.click(screen.getByTestId('create-role-btn'));
    const checkboxes = screen.getAllByRole('checkbox');
    expect(checkboxes.length).toBeGreaterThan(0);
  });

  it('can enter role name and save', () => {
    render(<MemoryRouter><SettingsRolesPage /></MemoryRouter>);
    fireEvent.click(screen.getByTestId('create-role-btn'));
    const nameInput = screen.getByTestId('new-role-name');
    fireEvent.change(nameInput, { target: { value: 'Vertrieb' } });
    fireEvent.click(screen.getByTestId('save-role-btn'));
    expect(screen.getByText('Vertrieb')).toBeInTheDocument();
  });

  it('opens edit modal for existing role', () => {
    render(<MemoryRouter><SettingsRolesPage /></MemoryRouter>);
    fireEvent.click(screen.getByTestId('edit-role-1'));
    expect(screen.getByTestId('edit-role-form')).toBeInTheDocument();
  });

  it('can update role name via edit modal', () => {
    render(<MemoryRouter><SettingsRolesPage /></MemoryRouter>);
    fireEvent.click(screen.getByTestId('edit-role-1'));
    const inputs = screen.getByTestId('edit-role-form').querySelectorAll('input:not([type="checkbox"])');
    fireEvent.change(inputs[0], { target: { value: 'Super Admin' } });
    fireEvent.click(screen.getByTestId('update-role-btn'));
    expect(screen.getByText('Super Admin')).toBeInTheDocument();
  });

  it('renders permission badges for roles', () => {
    render(<MemoryRouter><SettingsRolesPage /></MemoryRouter>);
    expect(screen.getAllByText(/Firmen lesen/).length).toBeGreaterThan(0);
    expect(screen.getAllByText(/Kontakte lesen/).length).toBeGreaterThan(0);
  });
});
