import dotenv from 'dotenv';
dotenv.config();

import app from './app.js';
import { connectDB } from './config/database.js';
import { notificationService } from './services/notificationService.js';

const startServer = async () => {
    try {
        console.log('🔄 Connecting to database...');
        await connectDB();

        const PORT = parseInt(process.env.PORT || '3000', 10);
        
        const server = app.listen(PORT, '0.0.0.0', () => {
            console.log(`🚀 Server running on port ${PORT}`);
            console.log('✅ Conectado ao Supabase (GPI schema)');
        });

        server.timeout = 60000;
        
    } catch (error) {
        console.error('Failed to start server:', error);
        process.exit(1);
    }
};

startServer();

process.on('uncaughtException', (err) => {
    console.error('Uncaught Exception:', err);
});

process.on('unhandledRejection', (reason) => {
    console.error('Unhandled Rejection:', reason);
});
