#!/usr/bin/env node

import { AtlasServer } from './server.js';

async function main() {
  const args = process.argv.slice(2);
  
  // Handle basic CLI commands
  if (args.includes('--help') || args.includes('-h')) {
    console.log(`
Atlas - Comprehensive Startup Project Management

Usage:
  atlas [options]

Options:
  --help, -h        Show this help message
  --version, -v     Show version information
  --config <path>   Use custom config file
  --verbose         Enable verbose logging

The MCP server will start and listen for connections via stdio.
Connect this to Claude Code or other MCP clients to use the tools.

Available modules:
  • Kanban Management - Visual task boards
  • Agile Management - Sprint planning and tracking  
  • Business Guidance - Market analysis and planning
  • Documentation - Automated documentation generation
  • Memory Management - Project knowledge storage
  • Development Tools - TDD enforcement and code quality
  • Repository Setup - Git best practices
  • Project Initialization - Template-based project creation

For more information, see: https://github.com/boundless-oss/atlas
`);
    process.exit(0);
  }
  
  if (args.includes('--version') || args.includes('-v')) {
    const packageJson = await import('../package.json', { with: { type: 'json' } });
    console.log(`atlas v${packageJson.default.version}`);
    process.exit(0);
  }
  
  // Enable verbose logging if requested
  if (args.includes('--verbose')) {
    process.env.ATLAS_VERBOSE = 'true';
  }
  
  // Custom config file
  const configIndex = args.indexOf('--config');
  if (configIndex !== -1 && args[configIndex + 1]) {
    process.env.ATLAS_CONFIG = args[configIndex + 1];
  }
  
  try {
    console.error('🚀 Starting Atlas Server...');
    
    const server = new AtlasServer();
    await server.run();
    
    // The server will run indefinitely, handling MCP requests
    console.error('✅ Atlas Server is running and ready for connections');
    
  } catch (error) {
    console.error('❌ Failed to start Atlas Server:', error);
    process.exit(1);
  }
}

// Handle graceful shutdown
process.on('SIGINT', () => {
  console.error('\n👋 Shutting down Atlas Server...');
  process.exit(0);
});

process.on('SIGTERM', () => {
  console.error('\n👋 Shutting down Atlas Server...');
  process.exit(0);
});

main().catch((error) => {
  console.error('💥 Unexpected error:', error);
  process.exit(1);
});