import { describe, it, expect } from 'vitest';
import { HeadlessPrompter } from '../../src/prompter/headless-prompter.js';
import { soulSetupStage } from '../../src/stages/soul-setup.js';
import type { WizardState } from '../../src/types.js';

function createState(overrides: Partial<WizardState> = {}): WizardState {
  return {
    mosaicHome: '/tmp/test-mosaic',
    sourceDir: '/tmp/test-mosaic',
    mode: 'quick',
    installAction: 'fresh',
    soul: {},
    user: {},
    tools: {},
    runtimes: { detected: [], mcpConfigured: false },
    selectedSkills: [],
    ...overrides,
  };
}

describe('soulSetupStage', () => {
  it('sets agent name and style in quick mode', async () => {
    const p = new HeadlessPrompter({
      'What name should agents use?': 'Jarvis',
      'Communication style': 'friendly',
    });

    const state = createState({ mode: 'quick' });
    await soulSetupStage(p, state);

    expect(state.soul.agentName).toBe('Jarvis');
    expect(state.soul.communicationStyle).toBe('friendly');
    expect(state.soul.roleDescription).toBe('execution partner and visibility engine');
  });

  it('uses defaults in quick mode with no answers', async () => {
    const p = new HeadlessPrompter({});
    const state = createState({ mode: 'quick' });
    await soulSetupStage(p, state);

    expect(state.soul.agentName).toBe('Assistant');
    expect(state.soul.communicationStyle).toBe('direct');
  });

  it('skips when install action is keep', async () => {
    const p = new HeadlessPrompter({});
    const state = createState({ installAction: 'keep' });
    state.soul.agentName = 'Existing';
    await soulSetupStage(p, state);

    expect(state.soul.agentName).toBe('Existing');
  });

  it('asks for all fields in advanced mode', async () => {
    const p = new HeadlessPrompter({
      'What name should agents use?': 'Atlas',
      'Agent role description': 'memory keeper',
      'Communication style': 'formal',
      'Accessibility preferences': 'ADHD-friendly',
      'Custom guardrails (optional)': 'Never push to main',
    });

    const state = createState({ mode: 'advanced' });
    await soulSetupStage(p, state);

    expect(state.soul.agentName).toBe('Atlas');
    expect(state.soul.roleDescription).toBe('memory keeper');
    expect(state.soul.communicationStyle).toBe('formal');
    expect(state.soul.accessibility).toBe('ADHD-friendly');
    expect(state.soul.customGuardrails).toBe('Never push to main');
  });
});
