import React from 'react';
import { describe, expect, it, vi } from 'vitest';
import { render, screen } from '@testing-library/react';
import { EventDataProvider } from '../context/EventDataContext';

vi.mock('@tamagui/stacks', () => ({
  YStack: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
  XStack: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
}));

vi.mock('@tamagui/text', () => ({
  SizableText: ({ children }: { children: React.ReactNode }) => <span>{children}</span>,
}));

vi.mock('@tamagui/button', () => ({
  Button: ({ children, ...rest }: { children: React.ReactNode }) => (
    <button type="button" {...rest}>
      {children}
    </button>
  ),
}));

vi.mock('@tamagui/sheet', () => {
  const Sheet = ({ children }: { children: React.ReactNode }) => <div>{children}</div>;
  Sheet.Overlay = ({ children }: { children?: React.ReactNode }) => <div>{children}</div>;
  Sheet.Frame = ({ children }: { children?: React.ReactNode }) => <div>{children}</div>;
  Sheet.Handle = ({ children }: { children?: React.ReactNode }) => <div>{children}</div>;
  return { Sheet };
});

vi.mock('react-router-dom', () => ({
  useNavigate: () => vi.fn(),
  useSearchParams: () => [new URLSearchParams()],
}));

vi.mock('lucide-react', () => ({
  Image: () => <span>image</span>,
  Filter: () => <span>filter</span>,
  Camera: () => <span>camera</span>,
  Grid2x2: () => <span>grid</span>,
  Zap: () => <span>zap</span>,
  UploadCloud: () => <span>upload</span>,
  ListVideo: () => <span>list</span>,
  RefreshCcw: () => <span>refresh</span>,
  FlipHorizontal: () => <span>flip</span>,
  X: () => <span>close</span>,
  Sparkles: () => <span>sparkles</span>,
  Trophy: () => <span>trophy</span>,
  Play: () => <span>play</span>,
  Share2: () => <span>share</span>,
  QrCode: () => <span>qr</span>,
  Link: () => <span>link</span>,
  Users: () => <span>users</span>,
}));

vi.mock('../components/AppShell', () => ({
  default: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
}));

vi.mock('../context/EventDataContext', () => ({
  EventDataProvider: ({ children }: { children: React.ReactNode }) => <>{children}</>,
  useEventData: () => ({ token: 'demo', tasksEnabled: true, event: null }),
}));

vi.mock('../services/uploadApi', () => ({
  useUploadQueue: () => ({ items: [], loading: false, add: vi.fn() }),
  uploadPhoto: vi.fn(),
}));

vi.mock('@/guest/services/pendingUploadsApi', () => ({
  fetchPendingUploadsSummary: vi.fn().mockResolvedValue({ items: [], totalCount: 0 }),
}));

vi.mock('../services/photosApi', () => ({
  fetchGallery: vi.fn().mockResolvedValue({ data: [], next_cursor: null, latest_photo_at: null, notModified: false }),
}));

vi.mock('../hooks/usePollGalleryDelta', () => ({
  usePollGalleryDelta: () => ({ data: { photos: [], latestPhotoAt: null, nextCursor: null }, loading: false, error: null }),
}));

vi.mock('@/guest/i18n/useTranslation', () => ({
  useTranslation: () => ({
    t: (key: string, arg2?: Record<string, string | number> | string, arg3?: string) =>
      typeof arg2 === 'string' || arg2 === undefined ? (arg2 ?? arg3 ?? key) : (arg3 ?? key),
    locale: 'de',
  }),
}));

vi.mock('@/guest/i18n/LocaleContext', () => ({
  useLocale: () => ({ locale: 'de', availableLocales: [], setLocale: vi.fn() }),
}));

vi.mock('@/hooks/use-appearance', () => ({
  useAppearance: () => ({ resolved: 'light' }),
}));

vi.mock('../services/tasksApi', () => ({
  fetchTasks: vi.fn().mockResolvedValue([]),
}));

vi.mock('../services/emotionsApi', () => ({
  fetchEmotions: vi.fn().mockResolvedValue([]),
}));

vi.mock('../hooks/usePollStats', () => ({
  usePollStats: () => ({
    stats: {
      onlineGuests: 0,
      tasksSolved: 0,
      guestCount: 0,
      likesCount: 0,
      latestPhotoAt: null,
    },
  }),
}));

vi.mock('../services/qrApi', () => ({
  fetchEventQrCode: () => Promise.resolve({ qr_code_data_url: null }),
}));

vi.mock('@/guest/hooks/useGuestTaskProgress', () => ({
  useGuestTaskProgress: () => ({ completedCount: 0 }),
}));

import GalleryScreen from '../screens/GalleryScreen';
import UploadScreen from '../screens/UploadScreen';
import TasksScreen from '../screens/TasksScreen';
import ShareScreen from '../screens/ShareScreen';

describe('Guest v2 screens copy', () => {
  it('renders gallery header', () => {
    render(
      <EventDataProvider tasksEnabledFallback>
        <GalleryScreen />
      </EventDataProvider>
    );

    expect(screen.getByText('Gallery')).toBeInTheDocument();
  });

  it('renders upload preview prompt', () => {
    render(
      <EventDataProvider tasksEnabledFallback>
        <UploadScreen />
      </EventDataProvider>
    );

    expect(screen.getByText('Camera')).toBeInTheDocument();
  });

  it('renders tasks quest when enabled', () => {
    render(
      <EventDataProvider tasksEnabledFallback>
        <TasksScreen />
      </EventDataProvider>
    );

    expect(screen.getByText('Mission hub')).toBeInTheDocument();
  });

  it('renders share hub header', () => {
    render(
      <EventDataProvider tasksEnabledFallback>
        <ShareScreen />
      </EventDataProvider>
    );

    expect(screen.getByText('Invite guests')).toBeInTheDocument();
  });
});
