import { Command } from 'commander';
import { beforeEach, describe, expect, it, vi } from 'vitest';

const { mockRunSetup } = vi.hoisted(() => ({
  mockRunSetup: vi.fn(async () => undefined),
}));

const { mockGetConfigPath } = vi.hoisted(() => ({
  mockGetConfigPath: vi.fn(() => '/tmp/default-config.yaml'),
}));

vi.mock('./setup.js', () => ({
  runSetup: mockRunSetup,
}));

vi.mock('./shared.js', () => ({
  getConfigPath: mockGetConfigPath,
}));

describe('onboard command', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    mockRunSetup.mockReset();
    mockGetConfigPath.mockReset();
    mockGetConfigPath.mockReturnValue('/tmp/default-config.yaml');
  });

  it('runs setup using default config path when --config is omitted', async () => {
    const program = new Command();
    const { registerOnboardCommand } = await import('./onboard.js');
    registerOnboardCommand(program);

    await program.parseAsync(['node', 'test', 'onboard']);

    expect(mockGetConfigPath).toHaveBeenCalledOnce();
    expect(mockRunSetup).toHaveBeenCalledWith('/tmp/default-config.yaml');
  });

  it('runs setup using explicit --config path when provided', async () => {
    const program = new Command();
    const { registerOnboardCommand } = await import('./onboard.js');
    registerOnboardCommand(program);

    await program.parseAsync(['node', 'test', 'onboard', '--config', '/tmp/custom.yaml']);

    expect(mockGetConfigPath).not.toHaveBeenCalled();
    expect(mockRunSetup).toHaveBeenCalledWith('/tmp/custom.yaml');
  });
});
