import type { WizardPrompter } from '../prompter/interface.js';
import type { WizardState, CommunicationStyle } from '../types.js';
import { DEFAULTS } from '../constants.js';

export async function soulSetupStage(p: WizardPrompter, state: WizardState): Promise<void> {
  if (state.installAction === 'keep') return;

  p.separator();
  p.note(
    'Your agent identity defines how AI assistants behave,\n' +
      'their principles, and communication style.\n' +
      'This creates SOUL.md.',
    'Agent Identity',
  );

  if (!state.soul.agentName) {
    state.soul.agentName = await p.text({
      message: 'What name should agents use?',
      placeholder: 'e.g., Jarvis, Assistant, Mosaic',
      defaultValue: DEFAULTS.agentName,
      validate: (v) => {
        if (v.length === 0) return 'Name cannot be empty';
        if (v.length > 50) return 'Name must be under 50 characters';
        return undefined;
      },
    });
  } else if (state.installAction === 'reconfigure') {
    // Show existing value as default so the user can accept or change it
    state.soul.agentName = await p.text({
      message: 'What name should agents use?',
      placeholder: state.soul.agentName,
      defaultValue: state.soul.agentName,
      validate: (v) => {
        if (v.length === 0) return 'Name cannot be empty';
        if (v.length > 50) return 'Name must be under 50 characters';
        return undefined;
      },
    });
  }

  if (state.mode === 'advanced') {
    if (!state.soul.roleDescription) {
      state.soul.roleDescription = await p.text({
        message: 'Agent role description',
        placeholder: 'e.g., execution partner and visibility engine',
        defaultValue: DEFAULTS.roleDescription,
      });
    }
  } else {
    state.soul.roleDescription ??= DEFAULTS.roleDescription;
  }

  if (!state.soul.communicationStyle || state.installAction === 'reconfigure') {
    state.soul.communicationStyle = await p.select<CommunicationStyle>({
      message: 'Communication style',
      options: [
        { value: 'direct', label: 'Direct', hint: 'Concise, no fluff, actionable' },
        { value: 'friendly', label: 'Friendly', hint: 'Warm but efficient, conversational' },
        { value: 'formal', label: 'Formal', hint: 'Professional, structured, thorough' },
      ],
      initialValue: state.soul.communicationStyle ?? 'direct',
    });
  }

  if (state.mode === 'advanced') {
    if (!state.soul.accessibility) {
      state.soul.accessibility = await p.text({
        message: 'Accessibility preferences',
        placeholder: "e.g., ADHD-friendly chunking, dyslexia-aware formatting, or 'none'",
        defaultValue: 'none',
      });
    }

    if (!state.soul.customGuardrails) {
      state.soul.customGuardrails = await p.text({
        message: 'Custom guardrails (optional)',
        placeholder: 'e.g., Never auto-commit to main',
        defaultValue: '',
      });
    }
  }
}
