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

const fetchStatusMock = vi.fn();
const trackMock = vi.fn();

vi.mock('../../auth/context', () => ({
  useAuth: () => ({ status: 'authenticated', user: { id: 1, role: 'owner' } }),
}));

vi.mock('../../api', () => ({
  fetchOnboardingStatus: () => fetchStatusMock(),
  trackOnboarding: () => trackMock(),
}));

function ProgressProbe() {
  const { progress } = useOnboardingProgress();
  return (
    <div>
      <span data-testid="admin-opened">{progress.adminAppOpenedAt ?? 'null'}</span>
      <span data-testid="invite-created">{String(progress.inviteCreated)}</span>
    </div>
  );
}

describe('OnboardingProgressProvider', () => {
  beforeEach(() => {
    fetchStatusMock.mockResolvedValue({ steps: undefined });
    trackMock.mockResolvedValue(undefined);
    window.localStorage.clear();
  });

  it('handles onboarding status responses without steps', async () => {
    render(
      <OnboardingProgressProvider>
        <ProgressProbe />
      </OnboardingProgressProvider>
    );

    await waitFor(() => expect(fetchStatusMock).toHaveBeenCalled());

    expect(screen.getByTestId('admin-opened').textContent).toBeTruthy();
    expect(screen.getByTestId('invite-created').textContent).toBe('false');
  });
});
