const { renderBanner } = require('../lib/banner');
const { resolveRepoRoot, resolveTargetRoot } = require('../lib/paths');
const { copyPackagedAssets } = require('../lib/copy-assets');
const { writeUserConfig } = require('../lib/write-config');
const { askInstallQuestions } = require('../lib/prompts');
const { resolveSkillSet } = require('../lib/profiles');
const path = require('node:path');
const fs = require('node:fs');

async function installCommand(options = {}) {
  const repoRoot = options.repoRoot || resolveRepoRoot();
  const cwd = options.cwd || process.cwd();
  const io = options.io || console;
  const promptAdapter = options.promptAdapter || askInstallQuestions;

  io.log(renderBanner());

  const answers = await promptAdapter({ cwd });
  if (!answers.confirmed) {
    io.log('RTExit install cancelled.');
    return;
  }

  const targetRoot = resolveTargetRoot(answers.targetDirectory);

  const ides = answers.ides && answers.ides.length ? answers.ides : ['agents'];
  const allowedSkills = resolveSkillSet(answers.profiles || ['all']);
  await copyPackagedAssets({ repoRoot, targetRoot, ides, allowedSkills });
  await writeUserConfig({
    targetRoot,
    answers: {
      language: answers.language,
      document_output_language: answers.document_output_language,
    },
  });

  const ideFolders = ides.map((ide) => {
    const map = { agents: '.agents/skills', claude: '.claude/skills', trae: '.trae/skills', codex: '.codex/skills' };
    return map[ide] || `.${ide}/skills`;
  });

  const dockerComposePath = path.join(targetRoot, 'docker', 'docker-compose.yml');
  const dockerInstalled = fs.existsSync(dockerComposePath);

  io.log('');
  io.log('════════════════════════════════════════════');
  io.log('  RTExit installed successfully!');
  io.log('════════════════════════════════════════════');
  io.log('');
  io.log(`  Skills: ${allowedSkills.size} skills → ${ideFolders.join(', ')}`);
  io.log('');

  if (dockerInstalled) {
    io.log('  ┌─ STEP 1: Start the Docker Lab ─────────────────────────────┐');
    io.log('  │                                                             │');
    io.log('  │  cd docker                                                  │');
    io.log('  │  docker compose up -d                                       │');
    io.log('  │                                                             │');
    io.log('  │  First run downloads ~4GB image. Subsequent starts: <5s    │');
    io.log('  └─────────────────────────────────────────────────────────────┘');
    io.log('');
    io.log('  ┌─ STEP 2: Enter the Shell ───────────────────────────────────┐');
    io.log('  │                                                             │');
    io.log('  │  docker exec -it rtexit-kali bash                          │');
    io.log('  │                                                             │');
    io.log('  │  300+ tools ready. Your project is at /workspace           │');
    io.log('  └─────────────────────────────────────────────────────────────┘');
    io.log('');
    io.log('  ┌─ STEP 3: Configure & Start ─────────────────────────────────┐');
    io.log('  │                                                             │');
    io.log('  │  1. Edit _rtexit/config.user.toml (client/project info)    │');
    io.log('  │  2. Open your AI IDE in this directory                     │');
    io.log('  │  3. Start with: rt-help                                    │');
    io.log('  │                                                             │');
    io.log('  └─────────────────────────────────────────────────────────────┘');
    io.log('');
    io.log('  Docker README: docker/README.md');
    io.log('  Build locally: see docker/README.md → "Build Locally" section');
  } else {
    io.log('  Next steps:');
    io.log('  1. Edit _rtexit/config.user.toml with your engagement details');
    io.log('  2. Open your AI IDE in this project');
    io.log('  3. Start with rt-help');
  }

  io.log('');
  io.log('════════════════════════════════════════════');
  io.log('');
}

module.exports = { installCommand };
