/**
 * @fileoverview Interactive Wizard - Manual configuration when LLM fails
 * @module morph-spec/ui/interactive-wizard
 */

import inquirer from 'inquirer';
import chalk from 'chalk';
import { getWizardQuestions, mapAnswersToConfig } from './wizard-questions.js';

/**
 * @typedef {import('../types/index.js').ProjectConfig} ProjectConfig
 */

/**
 * InteractiveWizard - Guides user through manual project configuration
 * @class
 */
export class InteractiveWizard {
  /**
   * Run interactive wizard to collect project configuration
   * @returns {Promise<ProjectConfig>}
   */
  async run() {
    console.log(chalk.bold.cyan('\n🧙 Interactive Project Configuration Wizard\n'));
    console.log(chalk.dim('  Answer 7 questions to configure your project\n'));

    const questions = getWizardQuestions();
    const answers = await inquirer.prompt(questions);

    // Map answers to ProjectConfig
    const config = mapAnswersToConfig(answers);

    // Show summary
    this.displaySummary(config);

    // Confirm before proceeding
    const { confirmed } = await inquirer.prompt([
      {
        type: 'confirm',
        name: 'confirmed',
        message: 'Is this configuration correct?',
        default: true
      }
    ]);

    if (!confirmed) {
      console.log(chalk.yellow('\n❌ Configuration canceled. Please run the wizard again.\n'));
      process.exit(0);
    }

    return config;
  }

  /**
   * Display summary of collected configuration
   * @param {ProjectConfig} config - Project configuration
   */
  displaySummary(config) {
    console.log(chalk.bold.green('\n✅ Configuration Summary:\n'));

    console.log(chalk.bold('  Name:        ') + chalk.cyan(config.name));
    console.log(chalk.bold('  Type:        ') + chalk.yellow(config.type));
    console.log(chalk.bold('  Description: ') + chalk.white(config.description));
    console.log();

    console.log(chalk.bold('  Stack:'));
    if (config.stack.frontend) {
      console.log(chalk.gray('    Frontend:  ') + chalk.white(`${config.stack.frontend.tech} ${config.stack.frontend.version}`));
    } else {
      console.log(chalk.gray('    Frontend:  ') + chalk.dim('None (backend-only)'));
    }
    console.log(chalk.gray('    Backend:   ') + chalk.white(`${config.stack.backend.tech} ${config.stack.backend.version}`));
    if (config.stack.database) {
      console.log(chalk.gray('    Database:  ') + chalk.white(`${config.stack.database.tech} ${config.stack.database.version}`));
    } else {
      console.log(chalk.gray('    Database:  ') + chalk.dim('None'));
    }
    if (config.stack.hosting) {
      console.log(chalk.gray('    Hosting:   ') + chalk.white(config.stack.hosting));
    }
    console.log();

    console.log(chalk.bold('  Architecture: ') + chalk.magenta(config.architecture));
    console.log();

    // Infrastructure flags
    const flags = [];
    if (config.hasAzure) flags.push(chalk.blue('Azure'));
    if (config.hasDocker) flags.push(chalk.cyan('Docker'));

    if (flags.length > 0) {
      console.log(chalk.bold('  Infrastructure: ') + flags.join(' • '));
      console.log();
    }
  }
}
