#!/usr/bin/env node
const fs = require('fs');
const path = require('path');

// Load env from ../../.env (best-effort)
try {
  const envPath = path.resolve(__dirname, '..', '..', '.env');
  if (fs.existsSync(envPath)) {
    const content = fs.readFileSync(envPath, 'utf8');
    content.split(/\n/).forEach(line => {
      const m = line.match(/^([^#=]+)=([\s\S]*)$/);
      if (m) process.env[m[1].trim()] = m[2].trim();
    });
  }
} catch (err) {
  // ignore
}

try {
  // Prefer ts-node/register to allow requiring .ts directly
  require.resolve('ts-node/register');
  require('ts-node/register');
  require('./app.ts');
} catch (err) {
  console.error('ts-node not found. Please run this inside the project container where dev dependencies are installed.');
  console.error('Original error:', err && err.message ? err.message : err);
  process.exit(1);
}
