/**
 * Tests for useAuthForm 2FA behavior.
 *
 * The sign-in flow only *verifies* an existing 2FA challenge — it never
 * prompts the user to *set up* 2FA (that lives in ProfileLayout). These tests
 * cover the step machine and the shouldPrompt2FA state that the verification
 * branch relies on.
 */

// @vitest-environment jsdom
import { act, renderHook } from '@testing-library/react';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { useAuthForm } from '../hooks/useAuthForm';

// useAuthForm pulls in useAutoAuth → useCfgRouter → next/navigation, which
// throws outside an app-router provider. Stub the navigation surface it uses.
vi.mock('next/navigation', () => ({
  useRouter: () => ({ push: vi.fn(), replace: vi.fn(), prefetch: vi.fn(), back: vi.fn() }),
  usePathname: () => '/auth',
  useSearchParams: () => new URLSearchParams(),
}));

// Mock dependencies (migrated from Jest → Vitest)
vi.mock('../context', () => ({
  useAuth: () => ({
    requestOTP: vi.fn().mockResolvedValue({ success: true }),
    verifyOTP: vi.fn(),
    getSavedEmail: vi.fn().mockReturnValue(null),
    saveEmail: vi.fn(),
    clearSavedEmail: vi.fn(),
    getSavedPhone: vi.fn().mockReturnValue(null),
    savePhone: vi.fn(),
    clearSavedPhone: vi.fn(),
  }),
}));

vi.mock('../hooks/useTwoFactorVerify', () => ({
  useTwoFactorVerify: () => ({
    isLoading: false,
    warning: null,
    verifyTOTP: vi.fn(),
    verifyBackupCode: vi.fn(),
  }),
}));

vi.mock('../utils/logger', () => ({
  authLogger: {
    info: vi.fn(),
    warn: vi.fn(),
    error: vi.fn(),
  },
}));

describe('useAuthForm - 2FA', () => {
  const defaultOptions = {
    sourceUrl: 'http://localhost',
    redirectUrl: '/dashboard',
  };

  beforeEach(() => {
    vi.clearAllMocks();
  });

  describe('step transitions', () => {
    it('should start on the identifier step', () => {
      const { result } = renderHook(() => useAuthForm(defaultOptions));

      expect(result.current.step).toBe('identifier');
    });

    it('should expose a setStep handler', () => {
      const { result } = renderHook(() => useAuthForm(defaultOptions));

      expect(typeof result.current.setStep).toBe('function');
    });

    it('should be able to transition to the success step', () => {
      const { result } = renderHook(() => useAuthForm(defaultOptions));

      act(() => {
        result.current.setStep('success');
      });

      expect(result.current.step).toBe('success');
    });
  });

  describe('shouldPrompt2FA state', () => {
    it('should initialize shouldPrompt2FA as false', () => {
      const { result } = renderHook(() => useAuthForm(defaultOptions));

      expect(result.current.shouldPrompt2FA).toBe(false);
    });

    it('should be able to set shouldPrompt2FA', () => {
      const { result } = renderHook(() => useAuthForm(defaultOptions));

      act(() => {
        result.current.setShouldPrompt2FA(true);
      });

      expect(result.current.shouldPrompt2FA).toBe(true);
    });
  });
});
