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

vi.mock('../settings-sheet', () => ({
  SettingsSheet: () => <div data-testid="settings-sheet" />,
}));

vi.mock('@/components/appearance-dropdown', () => ({
  default: () => <div data-testid="appearance-toggle" />,
}));

vi.mock('../../hooks/useEventData', () => ({
  useEventData: () => ({
    status: 'ready',
    event: {
      name: 'Demo Event',
      type: { icon: 'heart' },
      engagement_mode: 'photo_only',
    },
  }),
}));

vi.mock('../../context/EventStatsContext', () => ({
  useOptionalEventStats: () => null,
}));

vi.mock('../../context/GuestIdentityContext', () => ({
  useOptionalGuestIdentity: () => null,
}));

vi.mock('../../context/NotificationCenterContext', () => ({
  useOptionalNotificationCenter: () => ({
    notifications: [],
    unreadCount: 0,
    queueItems: [],
    queueCount: 0,
    pendingCount: 0,
    loading: false,
    pendingLoading: false,
    refresh: vi.fn(),
    setFilters: vi.fn(),
    markAsRead: vi.fn(),
    dismiss: vi.fn(),
    eventToken: 'demo',
    lastFetchedAt: null,
    isOffline: false,
  }),
}));

vi.mock('../../hooks/useGuestTaskProgress', () => ({
  useGuestTaskProgress: () => ({
    hydrated: false,
    completedCount: 0,
  }),
  TASK_BADGE_TARGET: 10,
}));

vi.mock('../../hooks/usePushSubscription', () => ({
  usePushSubscription: () => ({
    supported: false,
    permission: 'default',
    subscribed: false,
    loading: false,
    error: null,
    enable: vi.fn(),
    disable: vi.fn(),
    refresh: vi.fn(),
  }),
}));

vi.mock('../../i18n/useTranslation', () => ({
  useTranslation: () => ({
    t: (_key: string, fallback?: string | { defaultValue?: string }) => {
      if (typeof fallback === 'string') {
        return fallback;
      }
      if (fallback && typeof fallback.defaultValue === 'string') {
        return fallback.defaultValue;
      }
      return _key;
    },
  }),
}));

describe('Header notifications toggle', () => {
  it('closes the panel when clicking the bell again', () => {
    render(
      <MemoryRouter>
        <Header eventToken="demo" title="Demo" />
      </MemoryRouter>,
    );

    const bellButton = screen.getByLabelText('Benachrichtigungen anzeigen');
    fireEvent.click(bellButton);

    expect(screen.getByText('Updates')).toBeInTheDocument();

    fireEvent.click(bellButton);

    expect(screen.queryByText('Updates')).not.toBeInTheDocument();
  });
});
