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

const { mockLoadStoredOpenAIApiKey, mockStoreOpenAIApiKey } = vi.hoisted(() => ({
  mockLoadStoredOpenAIApiKey: vi.fn(),
  mockStoreOpenAIApiKey: vi.fn(),
}));

const { mockCreateInterface } = vi.hoisted(() => ({
  mockCreateInterface: vi.fn(),
}));

vi.mock('../auth/index.js', () => ({
  loadStoredOpenAIApiKey: mockLoadStoredOpenAIApiKey,
  storeOpenAIApiKey: mockStoreOpenAIApiKey,
}));

vi.mock('readline', () => ({
  default: {
    createInterface: mockCreateInterface,
  },
}));

function mockReadlineAnswers(answers: string[]): void {
  const queue = [...answers];
  mockCreateInterface.mockImplementation(() => ({
    question: (_prompt: string, cb: (answer: string) => void) => cb(queue.shift() ?? ''),
    close: () => undefined,
  }));
}

describe('openai-key command', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    mockLoadStoredOpenAIApiKey.mockReset();
    mockStoreOpenAIApiKey.mockReset();
    mockCreateInterface.mockReset();
  });

  it('cancels when key exists and user answers no', async () => {
    mockLoadStoredOpenAIApiKey.mockReturnValue('sk-existing');
    mockReadlineAnswers(['n']);

    const program = new Command();
    const { registerOpenaiKeyCommand } = await import('./openai-key.js');
    registerOpenaiKeyCommand(program);

    const consoleLog = vi.spyOn(console, 'log').mockImplementation(() => undefined);
    const exitSpy = vi.spyOn(process, 'exit').mockImplementation(((code?: number) => {
      throw new Error(`EXIT:${code ?? 0}`);
    }) as never);

    await expect(program.parseAsync(['node', 'test', 'openai-key'])).rejects.toThrow('EXIT:0');
    expect(mockStoreOpenAIApiKey).not.toHaveBeenCalled();
    expect(consoleLog).toHaveBeenCalledWith('Cancelled.');

    exitSpy.mockRestore();
    consoleLog.mockRestore();
  });

  it('stores a new key when user confirms re-authentication', async () => {
    mockLoadStoredOpenAIApiKey.mockReturnValue('sk-existing');
    mockReadlineAnswers(['y', 'sk-new']);

    const program = new Command();
    const { registerOpenaiKeyCommand } = await import('./openai-key.js');
    registerOpenaiKeyCommand(program);

    const consoleLog = vi.spyOn(console, 'log').mockImplementation(() => undefined);
    const consoleError = vi.spyOn(console, 'error').mockImplementation(() => undefined);

    await program.parseAsync(['node', 'test', 'openai-key']);

    expect(mockStoreOpenAIApiKey).toHaveBeenCalledWith('sk-new');
    expect(consoleError).not.toHaveBeenCalled();

    consoleLog.mockRestore();
    consoleError.mockRestore();
  });
});
