#!/usr/bin/env node
/**
 * LimeBot WhatsApp Bridge
 * 
 * This bridge connects WhatsApp Web to LimeBot's Python backend
 * via WebSocket. It handles authentication, message forwarding,
 * and reconnection logic.
 * 
 * Usage:
 *   npm run build && npm start
 *   
 * Or with custom settings:
 *   BRIDGE_PORT=3001 AUTH_DIR=./session npm start
 */

// Polyfill crypto for Baileys in ESM
import { webcrypto } from 'crypto';
if (!globalThis.crypto) {
  (globalThis as any).crypto = webcrypto;
}

import { BridgeServer } from './server.js';
import { join } from 'path';

const PORT = parseInt(process.env.BRIDGE_PORT || '3000', 10);
const AUTH_DIR = process.env.AUTH_DIR || join(process.cwd(), 'session');

console.log('🍋 LimeBot WhatsApp Bridge');
console.log('=========================\n');

const server = new BridgeServer(PORT, AUTH_DIR);

// Handle graceful shutdown
process.on('SIGINT', async () => {
  console.log('\n\nShutting down...');
  await server.stop();
  process.exit(0);
});

process.on('SIGTERM', async () => {
  await server.stop();
  process.exit(0);
});

// Start the server
server.start().catch((error) => {
  console.error('Failed to start bridge:', error);
  process.exit(1);
});
