#!/usr/bin/env node

const path = require('path');
const fs = require('fs');

// Path to the standalone server
// When installed via npm, __dirname is usually node_modules/forge-ai-terminal/bin
const serverPath = path.join(__dirname, '../.next/standalone/server.js');

if (!fs.existsSync(serverPath)) {
  console.error('Error: Build artifacts not found.');
  console.error('Please ensure the package was correctly built and installed.');
  console.error('Expected path:', serverPath);
  process.exit(1);
}

// Next.js standalone server uses process.env.PORT or defaults to 3000
process.env.NODE_ENV = 'production';

// Load environment variables from .env if it exists in the current working directory
const envPath = path.join(process.cwd(), '.env');
if (fs.existsSync(envPath)) {
  require('dotenv').config({ path: envPath });
}

// Start the server
console.log('Starting Forge AI...');
require(serverPath);
