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 unknown 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)
      'n',           // confirm: Configure automation now? (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) as { models?: { default?: { provider?: string } } };
    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)
      'n',                // confirm: Configure automation now? (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]);
  });

  it('can configure operator pack during first-run wizard', 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)
      '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)
      'y',           // confirm: Configure automation now? (yes)
      'y',           // confirm: Enable operator automation pack? (yes)
      '',            // ask: Operator notifications output channel (default)
      '',            // ask: Operator notifications peer/chat ID (default)
      '',            // ask: Backup cron schedule (default)
      '',            // ask: Daily briefing cron schedule (default)
      '',            // confirm: Include default MinIO sync task? (default yes)
      'n',           // confirm: Enable cron scheduler? (no)
      'n',           // confirm: Enable webhook receiver? (no)
      'n',           // confirm: Configure Google services? (no)
    ]);
    const p = createPrompter(rl);

    const builder = await runFirstRunWizard(p);
    const config = builder.build() as Record<string, unknown>;
    const backup = config.backup as Record<string, unknown>;
    const automation = config.automation as Record<string, unknown>;
    const heartbeat = automation.heartbeat as Record<string, unknown>;

    expect(backup.enabled).toBe(true);
    expect(backup.schedule).toBe('0 2 * * *');
    expect(heartbeat.enabled).toBe(true);
  });
});
