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

const fixtures = vi.hoisted(() => ({
  event: {
    id: 1,
    name: 'Demo Event',
    slug: 'demo-event',
    event_date: '2026-02-19',
    event_type_id: null,
    event_type: null,
    status: 'published',
    settings: {},
  },
  assignedTasks: [
    { id: 1, title: 'Task A', description: 'Desc', emotion: null, event_type_id: null },
    { id: 2, title: 'Task B', description: '', emotion: null, event_type_id: null },
  ],
  libraryTasks: [
    { id: 3, title: 'Task C', description: '', emotion: null, event_type_id: null },
  ],
  collections: [{ id: 1, name: 'Starter Pack', description: '' }],
  emotions: [{ id: 1, name: 'Joy', color: '#ff6b6b' }],
}));

const navigateMock = vi.fn();
const backMock = vi.fn();

vi.mock('react-router-dom', () => ({
  useNavigate: () => navigateMock,
  useParams: () => ({ slug: fixtures.event.slug }),
}));

vi.mock('react-i18next', () => ({
  useTranslation: () => ({
    t: (key: string, fallback?: string | Record<string, unknown>) => {
      if (typeof fallback === 'string') {
        return fallback;
      }
      if (fallback && typeof fallback === 'object' && typeof fallback.defaultValue === 'string') {
        return fallback.defaultValue;
      }
      return key;
    },
  }),
}));

vi.mock('../hooks/useBackNavigation', () => ({
  useBackNavigation: () => backMock,
}));

vi.mock('../../context/EventContext', () => ({
  useEventContext: () => ({
    activeEvent: fixtures.event,
    selectEvent: vi.fn(),
  }),
}));

vi.mock('../../api', () => ({
  getEvent: vi.fn().mockResolvedValue(fixtures.event),
  getEvents: vi.fn().mockResolvedValue([fixtures.event]),
  getEventTasks: vi.fn().mockResolvedValue({ data: fixtures.assignedTasks }),
  getTasks: vi.fn().mockResolvedValue({ data: fixtures.libraryTasks }),
  getTaskCollections: vi.fn().mockResolvedValue({ data: fixtures.collections }),
  getEmotions: vi.fn().mockResolvedValue(fixtures.emotions),
  assignTasksToEvent: vi.fn(),
  updateTask: vi.fn(),
  importTaskCollection: vi.fn(),
  createTask: vi.fn(),
  detachTasksFromEvent: vi.fn(),
  createEmotion: vi.fn(),
  updateEmotion: vi.fn(),
  deleteEmotion: vi.fn(),
}));

vi.mock('react-hot-toast', () => ({
  default: {
    error: vi.fn(),
    success: vi.fn(),
  },
}));

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

vi.mock('@tamagui/scroll-view', () => ({
  ScrollView: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
}));

vi.mock('@tamagui/toggle-group', () => ({
  ToggleGroup: Object.assign(({ children }: { children: React.ReactNode }) => <div>{children}</div>, {
    Item: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
  }),
}));

vi.mock('@tamagui/group', () => ({
  YGroup: Object.assign(({ children }: { children: React.ReactNode }) => <div>{children}</div>, {
    Item: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
  }),
}));

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/list-item', () => ({
  ListItem: ({ title, subTitle, iconAfter }: { title?: React.ReactNode; subTitle?: React.ReactNode; iconAfter?: React.ReactNode }) => (
    <div>
      {title}
      {subTitle}
      {iconAfter}
    </div>
  ),
}));

vi.mock('@tamagui/react-native-web-lite', () => ({
  Pressable: ({ children, onPress }: { children: React.ReactNode; onPress?: () => void }) => (
    <button type="button" onClick={onPress}>
      {children}
    </button>
  ),
}));

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

vi.mock('@tamagui/radio-group', () => ({
  RadioGroup: Object.assign(({ children }: { children: React.ReactNode }) => <div>{children}</div>, {
    Item: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
    Indicator: () => <div />,
  }),
}));

vi.mock('@tamagui/alert-dialog', () => ({
  AlertDialog: Object.assign(({ children }: { children: React.ReactNode }) => <div>{children}</div>, {
    Portal: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
    Overlay: ({ children }: { children?: React.ReactNode }) => <div>{children}</div>,
    Content: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
    Title: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
    Description: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
    Cancel: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
    Action: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
  }),
}));

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

vi.mock('../components/Primitives', () => ({
  MobileCard: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
  CTAButton: ({ label, onPress }: { label: string; onPress?: () => void }) => (
    <button type="button" onClick={onPress}>
      {label}
    </button>
  ),
  SkeletonCard: () => <div>Loading...</div>,
  FloatingActionButton: ({ label }: { label: string }) => <div>{label}</div>,
}));

vi.mock('../components/FormControls', () => ({
  MobileField: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
  MobileInput: (props: React.InputHTMLAttributes<HTMLInputElement>) => <input {...props} />,
  MobileSelect: ({ children }: { children: React.ReactNode }) => <select>{children}</select>,
  MobileTextArea: (props: React.TextareaHTMLAttributes<HTMLTextAreaElement>) => <textarea {...props} />,
}));

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

vi.mock('../components/Tag', () => ({
  Tag: ({ label }: { label: string }) => <span>{label}</span>,
}));

vi.mock('../theme', () => ({
  ADMIN_ACTION_COLORS: {
    settings: '#0ea5e9',
    tasks: '#f59e0b',
    qr: '#6366f1',
    branding: '#22c55e',
  },
  useAdminTheme: () => ({
    textStrong: '#111827',
    muted: '#6b7280',
    subtle: '#94a3b8',
    border: '#e5e7eb',
    primary: '#ff5a5f',
    danger: '#dc2626',
    surface: '#ffffff',
    surfaceMuted: '#f9fafb',
    dangerBg: '#fee2e2',
    dangerText: '#b91c1c',
    overlay: 'rgba(15,23,42,0.5)',
    shadow: 'rgba(15,23,42,0.12)',
  }),
}));

import MobileEventTasksPage from '../EventTasksPage';

describe('MobileEventTasksPage', () => {
  it('renders the task overview summary and quick jump chips', async () => {
    render(<MobileEventTasksPage />);

    expect(await screen.findByText('Task overview')).toBeInTheDocument();
    expect(screen.getByText('Tasks total')).toBeInTheDocument();
    expect(screen.getByText('Quick jump')).toBeInTheDocument();
    expect(screen.getByText('Assigned')).toBeInTheDocument();
  });
});
