/**
 * @fileoverview Wizard Questions - Defines interactive wizard questions
 * @module morph-spec/ui/wizard-questions
 */

/**
 * Get wizard questions for interactive mode
 * @returns {Array} Inquirer questions
 */
export function getWizardQuestions() {
  return [
    {
      type: 'input',
      name: 'name',
      message: '1/7 Project name:',
      default: process.cwd().split(/[/\\]/).pop(),
      validate: (input) => {
        if (!input || input.trim().length === 0) {
          return 'Project name is required';
        }
        return true;
      }
    },
    {
      type: 'input',
      name: 'description',
      message: '2/7 Project description (1-2 sentences):',
      validate: (input) => {
        if (!input || input.trim().length < 10) {
          return 'Description must be at least 10 characters';
        }
        return true;
      }
    },
    {
      type: 'list',
      name: 'type',
      message: '3/7 Project type:',
      choices: [
        { name: 'Blazor Server (.NET)', value: 'blazor-server' },
        { name: 'Next.js (React)', value: 'nextjs' },
        { name: '.NET Web API', value: 'dotnet-api' },
        { name: 'CLI Tool', value: 'cli-tool' },
        { name: 'Monorepo (multiple projects)', value: 'monorepo' },
        { name: 'Other', value: 'other' }
      ]
    },
    {
      type: 'list',
      name: 'frontend',
      message: '4/7 Frontend technology:',
      choices: [
        { name: 'Blazor Server', value: 'blazor' },
        { name: 'Next.js', value: 'nextjs' },
        { name: 'React', value: 'react' },
        { name: 'Vue.js', value: 'vue' },
        { name: 'Angular', value: 'angular' },
        { name: 'None (backend-only)', value: null }
      ],
      when: (answers) => answers.type !== 'cli-tool' && answers.type !== 'dotnet-api'
    },
    {
      type: 'list',
      name: 'backend',
      message: '5/7 Backend technology:',
      choices: [
        { name: '.NET 10', value: 'dotnet-10' },
        { name: '.NET 9', value: 'dotnet-9' },
        { name: '.NET 8', value: 'dotnet-8' },
        { name: 'Node.js', value: 'nodejs' },
        { name: 'Other', value: 'other' }
      ],
      default: 'dotnet-10'
    },
    {
      type: 'list',
      name: 'database',
      message: '6/7 Database:',
      choices: [
        { name: 'Azure SQL Database', value: 'azure-sql' },
        { name: 'PostgreSQL', value: 'postgresql' },
        { name: 'Cosmos DB', value: 'cosmosdb' },
        { name: 'MongoDB', value: 'mongodb' },
        { name: 'SQLite', value: 'sqlite' },
        { name: 'Supabase (PostgreSQL)', value: 'supabase' },
        { name: 'None (no database)', value: null }
      ]
    },
    {
      type: 'confirm',
      name: 'hasAzure',
      message: '7a/7 Uses Azure infrastructure?',
      default: false
    },
    {
      type: 'confirm',
      name: 'hasDocker',
      message: '7b/7 Uses Docker containerization?',
      default: true
    }
  ];
}

/**
 * Map wizard answers to ProjectConfig
 * @param {Object} answers - Wizard answers from inquirer
 * @returns {ProjectConfig} Mapped project config
 */
export function mapAnswersToConfig(answers) {
  // Parse backend tech and version
  const backendParts = answers.backend.split('-');
  const backendTech = backendParts[0] === 'dotnet' ? '.NET' :
                      backendParts[0] === 'nodejs' ? 'Node.js' :
                      answers.backend;
  const backendVersion = backendParts[1] || 'latest';

  // Map frontend
  let frontend = null;
  if (answers.frontend) {
    const frontendMap = {
      'blazor': { tech: 'Blazor', version: backendVersion, details: 'Blazor Server' },
      'nextjs': { tech: 'Next.js', version: '15', details: 'App Router' },
      'react': { tech: 'React', version: '18', details: null },
      'vue': { tech: 'Vue.js', version: '3', details: null },
      'angular': { tech: 'Angular', version: '17', details: null }
    };
    frontend = frontendMap[answers.frontend] || null;
  }

  // Map database
  let database = null;
  if (answers.database) {
    const databaseMap = {
      'azure-sql': { tech: 'Azure SQL', version: 'latest', details: 'Managed SQL Database' },
      'postgresql': { tech: 'PostgreSQL', version: '16', details: null },
      'cosmosdb': { tech: 'Cosmos DB', version: 'latest', details: 'NoSQL' },
      'mongodb': { tech: 'MongoDB', version: '7', details: null },
      'sqlite': { tech: 'SQLite', version: '3', details: null },
      'supabase': { tech: 'PostgreSQL', version: '16', details: 'Managed by Supabase' }
    };
    database = databaseMap[answers.database] || null;
  }

  // Infer architecture from project type
  const architectureMap = {
    'blazor-server': 'clean-architecture',
    'nextjs': 'layered',
    'dotnet-api': 'clean-architecture',
    'cli-tool': 'monolith',
    'monorepo': 'microservices',
    'other': 'layered'
  };

  const architecture = architectureMap[answers.type] || 'layered';

  // Infer hosting
  let hosting = null;
  if (answers.hasAzure) {
    hosting = 'Azure';
  } else if (answers.hasDocker) {
    hosting = 'Docker';
  }

  return {
    name: answers.name,
    description: answers.description,
    type: answers.type,
    stack: {
      frontend,
      backend: {
        tech: backendTech,
        version: backendVersion,
        details: null
      },
      database,
      hosting
    },
    architecture,
    projectStructure: 'User-specified configuration via interactive wizard',
    conventions: 'Standard conventions for ' + backendTech,
    repository: null,
    hasAzure: answers.hasAzure,
    hasDocker: answers.hasDocker,
    hasDevOps: false,
    confidence: 100, // User input is 100% confident
    warnings: ['Configuration was manually entered, not auto-detected']
  };
}
