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

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

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

vi.mock('../auth/index.js', () => ({
  loadStoredGeminiAuth: mockLoadStoredGeminiAuth,
  storeGeminiAuth: mockStoreGeminiAuth,
}));

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('gemini-auth command', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    mockLoadStoredGeminiAuth.mockReset();
    mockStoreGeminiAuth.mockReset();
    mockCreateInterface.mockReset();
  });

  it('cancels when key exists and user answers no', async () => {
    mockLoadStoredGeminiAuth.mockReturnValue({ api_key: 'gem-existing', created_at: '2026-02-21T00:00:00.000Z' });
    mockReadlineAnswers(['n']);

    const program = new Command();
    const { registerGeminiAuthCommand } = await import('./gemini-auth.js');
    registerGeminiAuthCommand(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', 'gemini-auth'])).rejects.toThrow('EXIT:0');
    expect(mockStoreGeminiAuth).not.toHaveBeenCalled();
    expect(consoleLog).toHaveBeenCalledWith('Cancelled.');

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

  it('stores a new key when user confirms re-authentication', async () => {
    mockLoadStoredGeminiAuth.mockReturnValue({ api_key: 'gem-existing', created_at: '2026-02-21T00:00:00.000Z' });
    mockReadlineAnswers(['y', 'gem-new']);

    const program = new Command();
    const { registerGeminiAuthCommand } = await import('./gemini-auth.js');
    registerGeminiAuthCommand(program);

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

    await program.parseAsync(['node', 'test', 'gemini-auth']);

    expect(mockStoreGeminiAuth).toHaveBeenCalledWith('gem-new');
    expect(consoleError).not.toHaveBeenCalled();

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