import './config/env.js';
import app from './app.js';
import { startAllCronJobs } from './cron/index.js';
import { logger } from './utils/logger.js';
import { testConnection } from './config/database.js';

const port = process.env.PORT || 5000;

const start = async () => {
  try {
    // Test database connection
    await testConnection();
    logger.success('Database connected');

    // Start server - listen on all interfaces for network access
    app.listen(port, '0.0.0.0', () => {
      logger.info(`Server running on http://0.0.0.0:${port}`);
      startAllCronJobs();
    });
  } catch (error) {
    logger.error('Failed to start server', error);
    process.exit(1);
  }
};

start();
