#!/usr/bin/env node
/**
 * ═══════════════════════════════════════════════════════════════════════════════
 * overmind-install-native — Lanceur cross-platform du script install natif
 * ═══════════════════════════════════════════════════════════════════════════════
 * Délègue au bon script selon l'OS :
 *   - Linux/macOS  → bin/install-overmind-native.sh
 *   - Windows      → instructions pour WSL (le mode natif est Linux-only)
 *
 * Utilisation :
 *   npx overmind-install-native
 *   overmind-install-native --yes   (auto-install sans prompt)
 * ═══════════════════════════════════════════════════════════════════════════════
 */

import { spawnSync } from 'child_process';
import { existsSync } from 'fs';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const ROOT = join(__dirname, '..');

const COLORS = {
  cyan: '\x1b[36m', green: '\x1b[32m', yellow: '\x1b[33m',
  red: '\x1b[31m', reset: '\x1b[0m'
};
const c = (col, s) => `${COLORS[col]}${s}${COLORS.reset}`;

function header() {
  console.log('');
  console.log(c('cyan', '╔════════════════════════════════════════════════════════════════╗'));
  console.log(c('cyan', '║  🚀 OverMind-MCP — Installation NATIVE (sans Docker)          ║'));
  console.log(c('cyan', '╚════════════════════════════════════════════════════════════════╝'));
  console.log('');
}

function launchUnix() {
  const script = join(ROOT, 'bin', 'install-overmind-native.sh');
  if (!existsSync(script)) {
    console.error(c('red', `❌ Script introuvable: ${script}`));
    process.exit(1);
  }
  console.log(c('yellow', '→ Lancement bin/install-overmind-native.sh'));
  console.log(c('yellow', '  (sudo est requis pour apt, systemd, create DB)'));
  console.log('');

  // Nécessite sudo pour systemd, apt, postgres. Si root déjà, lancer tel quel.
  const isRoot = process.getuid?.() === 0;
  const cmd = isRoot ? 'bash' : 'sudo';
  const args = isRoot
    ? [script, ...process.argv.slice(2)]
    : ['bash', script, ...process.argv.slice(2)];

  const r = spawnSync(cmd, args, { stdio: 'inherit' });
  process.exit(r.status ?? 1);
}

function windowsHelp() {
  console.log(c('yellow', '⚠ Le mode natif est conçu pour Linux (systemd + apt + pgvector).'));
  console.log('');
  console.log('Options sur Windows :');
  console.log('');
  console.log(c('green', '  1) WSL 2 (recommandé) — installer Ubuntu dans WSL :'));
  console.log('       wsl --install -d Ubuntu-26.04');
  console.log('       Puis dans WSL :');
  console.log('         curl -fsSL https://raw.githubusercontent.com/DeamonDev888/overmind-mcp/main/bin/install-overmind-native.sh | sudo bash');
  console.log('');
  console.log(c('green', '  2) Docker Desktop (par défaut) :'));
  console.log('       npm install -g overmind-mcp    # postinstall Docker automatique');
  console.log('');
  console.log(c('green', '  3) Installation manuelle dans WSL :'));
  console.log('       git clone https://github.com/DeamonDev888/overmind-mcp');
  console.log('       cd overmind-mcp');
  console.log('       sudo bash bin/install-overmind-native.sh');
  process.exit(0);
}

header();

if (process.platform === 'win32') {
  windowsHelp();
} else {
  launchUnix();
}
