import { describe, it, expect, vi } from 'vitest';
import type { Interface as ReadlineInterface } from 'readline/promises';
import { createPrompter } from './prompts.js';
import { runFirstRunWizard } from './orchestrator.js';
import { parse } from 'yaml';

function mockReadline(inputs: string[]): ReadlineInterface {
  let idx = 0;
  return {
    question: vi.fn(async () => {
      if (idx < inputs.length) {
        return inputs[idx++];
      }
      throw new Error('No more inputs');
    }),
    close: vi.fn(),
  } as any as ReadlineInterface;
}

describe('first-run wizard integration', () => {
  it('produces valid config with anthropic + webchat only', async () => {
    const rl = mockReadline([
      '1',           // choose: Anthropic
      'sk-ant-key',  // password: API key
      '',            // ask: Model (default)
      'n',           // confirm: Configure a fast tier? (no)
      '',            // ask: Gateway port (default)
      'n',           // confirm: Add a messaging channel? (no)
    ]);
    const p = createPrompter(rl);

    const builder = await runFirstRunWizard(p);
    const config = builder.build();
    const yaml = builder.toYaml();

    expect(config.models.default.provider).toBe('anthropic');
    expect(config.models.default.api_key).toBe('sk-ant-key');
    expect(config.server.port).toBeDefined();

    const reparsed = parse(yaml);
    expect(reparsed.models.default.provider).toBe('anthropic');
  });

  it('produces valid config with ollama + telegram', async () => {
    const rl = mockReadline([
      '3',                // choose: Ollama
      '',                 // ask: Host (default)
      '',                 // ask: Model (default)
      'n',                // confirm: Configure a fast tier? (no)
      '',                 // ask: Gateway port (default)
      'y',                // confirm: Add a messaging channel? (yes)
      '1',                // choose: Telegram
      '123:ABCdef',       // password: Bot token
      '12345678',         // ask: Allowed chat IDs
      'n',                // confirm: Add another channel? (no)
    ]);
    const p = createPrompter(rl);

    const builder = await runFirstRunWizard(p);
    const config = builder.build();

    expect(config.models.default.provider).toBe('ollama');
    expect(config.telegram.bot_token).toBe('123:ABCdef');
    expect(config.telegram.allowed_chat_ids).toEqual([12345678]);
  });
});
