// @vitest-environment jsdom
import { describe, it, expect, vi, beforeEach } from 'vitest';
import '@testing-library/jest-dom/vitest';
import { render, screen, fireEvent, cleanup, waitFor } from '@testing-library/react';

const { mockSetAi, mockSetCompleted } = vi.hoisted(() => ({
  mockSetAi: vi.fn(async () => ({ ok: true as const })),
  mockSetCompleted: vi.fn(async () => ({ ok: true as const }))
}));
vi.mock('../../src/renderer/inbox/api.js', () => ({
  inboxApi: { setAiEnabled: mockSetAi, setOnboardingCompleted: mockSetCompleted }
}));

import { OnboardingWizard } from '../../src/renderer/inbox/components/OnboardingWizard';

describe('OnboardingWizard', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    cleanup();
  });

  it('renders 3 buttons + 설치 가이드 link', () => {
    render(<OnboardingWizard onClose={vi.fn()} />);
    expect(screen.getByRole('button', { name: /AI 자동 처리 사용/ })).toBeInTheDocument();
    expect(screen.getByRole('button', { name: /원문만 저장/ })).toBeInTheDocument();
    expect(screen.getByRole('button', { name: /나중에 설정/ })).toBeInTheDocument();
    expect(screen.getByRole('link', { name: /ollama\.com/ })).toBeInTheDocument();
  });

  it('"AI 사용" → setAiEnabled(true) + setOnboardingCompleted(true) + onClose', async () => {
    const onClose = vi.fn();
    render(<OnboardingWizard onClose={onClose} />);
    fireEvent.click(screen.getByRole('button', { name: /AI 자동 처리 사용/ }));
    await waitFor(() => {
      expect(mockSetAi).toHaveBeenCalledWith(true);
      expect(mockSetCompleted).toHaveBeenCalledWith(true);
      expect(onClose).toHaveBeenCalled();
    });
  });

  it('"원문만" → setAiEnabled(false) + setOnboardingCompleted(true)', async () => {
    render(<OnboardingWizard onClose={vi.fn()} />);
    fireEvent.click(screen.getByRole('button', { name: /원문만 저장/ }));
    await waitFor(() => {
      expect(mockSetAi).toHaveBeenCalledWith(false);
      expect(mockSetCompleted).toHaveBeenCalledWith(true);
    });
  });

  it('"나중에" → setOnboardingCompleted(true) only (no setAiEnabled)', async () => {
    render(<OnboardingWizard onClose={vi.fn()} />);
    fireEvent.click(screen.getByRole('button', { name: /나중에 설정/ }));
    await waitFor(() => {
      expect(mockSetCompleted).toHaveBeenCalledWith(true);
      expect(mockSetAi).not.toHaveBeenCalled();
    });
  });
});
