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

const mockNotificationTypes = {
  items: [
    { type_key: 'mail.new', plugin_name: 'mail', category: 'mail', label: 'Neue E-Mail', description: 'Bei neuen E-Mails', is_enabled_by_default: true, is_enabled: true },
    { type_key: 'task.overdue', plugin_name: 'tasks', category: 'tasks', label: 'Überfällige Aufgabe', description: 'Aufgabe ist überfällig', is_enabled_by_default: false, is_enabled: false },
  ],
};

vi.mock('@/api/hooks', () => ({
  useNotificationTypes: () => ({ data: mockNotificationTypes, isLoading: false, error: null }),
  useUpdateNotificationPreference: () => ({
    mutate: vi.fn(),
    isPending: false,
  }),
}));

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

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

  it('renders notification type labels', () => {
    render(<SettingsNotificationsPage />);
    expect(screen.getByText('Neue E-Mail')).toBeInTheDocument();
    expect(screen.getByText('Überfällige Aufgabe')).toBeInTheDocument();
  });

  it('renders notification descriptions', () => {
    render(<SettingsNotificationsPage />);
    expect(screen.getByText('Bei neuen E-Mails')).toBeInTheDocument();
    expect(screen.getByText('Aufgabe ist überfällig')).toBeInTheDocument();
  });

  it('renders plugin group headers', () => {
    render(<SettingsNotificationsPage />);
    expect(screen.getByText('mail')).toBeInTheDocument();
    expect(screen.getByText('tasks')).toBeInTheDocument();
  });

  it('renders toggle switches for each notification type', () => {
    render(<SettingsNotificationsPage />);
    const switches = screen.getAllByRole('switch');
    expect(switches.length).toBeGreaterThanOrEqual(2);
  });
});
