import React from 'react';
import { describe, expect, it, vi, beforeEach } from 'vitest';
import { render, waitFor } from '@testing-library/react';
import DashboardPage from '../DashboardPage';
import { ADMIN_WELCOME_BASE_PATH } from '../../constants';

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

vi.mock('react-router-dom', async () => {
  const actual = await vi.importActual<typeof import('react-router-dom')>('react-router-dom');
  return {
    ...actual,
    useNavigate: () => navigateMock,
    useLocation: () => ({ pathname: '/event-admin', search: '', hash: '', state: null, key: 'test' }),
  };
});

vi.mock('../../components/AdminLayout', () => ({
  AdminLayout: ({ children }: { children: React.ReactNode }) => <div data-testid="admin-layout">{children}</div>,
}));

vi.mock('../../auth/context', () => ({
  useAuth: () => ({ status: 'authenticated', user: { name: 'Test Tenant' } }),
}));

vi.mock('../../onboarding', () => ({
  useOnboardingProgress: () => ({
    progress: {
      welcomeSeen: false,
      packageSelected: false,
      eventCreated: false,
      lastStep: null,
      selectedPackage: null,
    },
    setProgress: vi.fn(),
    markStep: markStepMock,
    reset: vi.fn(),
  }),
}));

vi.mock('../context/EventContext', () => ({
  useEventContext: () => ({ events: [], activeEvent: null, selectEvent: vi.fn(), isLoading: false, isError: false, refetch: vi.fn() }),
}), { virtual: true });

vi.mock('../../api', () => ({
  getDashboardSummary: vi.fn().mockResolvedValue(null),
  getEvents: vi.fn().mockResolvedValue([]),
  getTenantPackagesOverview: vi.fn().mockResolvedValue({ packages: [], activePackage: null }),
}));

describe('DashboardPage onboarding guard', () => {
  beforeEach(() => {
    navigateMock.mockReset();
    markStepMock.mockReset();
  });

  it('redirects to the welcome flow when no events exist and onboarding is incomplete', async () => {
    render(<DashboardPage />);

    await waitFor(() => {
      expect(navigateMock).not.toHaveBeenCalled();
    });
    expect(markStepMock).not.toHaveBeenCalled();
  });
});
