/**
 * 🏥 DSS Status Command - Organism Vital Signs
 *
 * Check DSS design system organism's vital signs, consciousness state,
 * and sensory organ configuration.
 */

import chalk from 'chalk';
import { getConfig, getProjectRoot, hasProjectConfig } from '../lib/config.js';
import { isServerRunning, getServerPid } from '../lib/server.js';
import { getApiClient } from '../lib/api.js';

export async function statusCommand(): Promise<void> {
  const cwd = getProjectRoot();
  const config = getConfig();

  console.log('');
  console.log(chalk.cyan('  🏥 ORGANISM VITAL SIGNS'));
  console.log(chalk.dim('  ────────────────────────'));
  console.log('');

  // Organism status
  if (hasProjectConfig()) {
    console.log(chalk.green('  🧬 Organism:'), chalk.dim('Born and conscious'));
    console.log(chalk.dim(`    Home: ${cwd}`));
  } else {
    console.log(chalk.yellow('  🧬 Organism:'), chalk.dim('Not yet born'));
    console.log(chalk.dim('    Genesis: dss init'));
  }

  console.log('');

  // Consciousness status (server)
  const running = isServerRunning(cwd);
  const pid = getServerPid(cwd);
  const port = config.port || 3456;

  if (running) {
    console.log(chalk.green('  💚 Consciousness:'), chalk.dim(`Awake (PID: ${pid})`));
    console.log(chalk.dim(`    Neural port: http://localhost:${port}`));

    // Try to get health info
    try {
      const api = getApiClient({ port });
      const health = await api.health();
      console.log(chalk.dim(`    Awareness: ${health.figma_mode}`));
    } catch {
      console.log(chalk.yellow('    ⚠️  Unable to read consciousness'));
    }
  } else {
    console.log(chalk.yellow('  💚 Consciousness:'), chalk.dim('Sleeping'));
    console.log(chalk.dim('    Awaken: dss start'));
  }

  console.log('');

  // Sensory organs (Figma)
  if (config.figmaToken) {
    console.log(chalk.green('  👁️ Sensory Eyes:'), chalk.dim('Configured'));

    // Test connection if server is running
    if (running) {
      try {
        const api = getApiClient({ port });
        const test = await api.testFigmaConnection();
        if (test.success) {
          console.log(chalk.green('    Perception:'), chalk.dim(`Clear (${test.user})`));
        } else {
          console.log(chalk.red('    Perception:'), chalk.dim(test.error || 'Blinded'));
        }
      } catch {
        console.log(chalk.yellow('    Perception:'), chalk.dim('Cannot test'));
      }
    }
  } else {
    console.log(chalk.yellow('  👁️ Sensory Eyes:'), chalk.dim('Not configured'));
    console.log(chalk.dim('    Configure: dss config --set figmaToken=figd_xxxxx'));
  }

  if (config.figmaFileKey) {
    console.log(chalk.green('  📋 Genetic Blueprint:'), chalk.dim(config.figmaFileKey));
  } else {
    console.log(chalk.yellow('  Figma File:'), chalk.dim('Not configured'));
    console.log(chalk.dim('    Set: dss config --set figmaFileKey=abc123'));
  }

  console.log('');

  // Output config
  console.log(chalk.dim('  Output:'));
  console.log(chalk.dim(`    Format: ${config.tokenFormat || 'css'}`));
  console.log(chalk.dim(`    Framework: ${config.componentFramework || 'react'}`));
  console.log(chalk.dim(`    Directory: ${config.outputDir || '.dss/output'}`));

  console.log('');
}
