const express = require('express');
const initiateResources = require('./utils/initiate-resources');
const dbManager = require('./database/DatabaseManager');
const SchedulerService = require('./services/SchedulerService');

class Server {
    _port;
    _app;

    constructor(port) {
        this._port = port;
        this._app = express();
    }

    async start() {
        try {
            // Initialisiere Datenbank
            console.log('🔄 Initialisiere Datenbank...');
            await dbManager.initialize();
            console.log('✓ Datenbank bereit');

            // Starte Express Server
            initiateResources(this._app);
            this._app.use('/upload', express.static( __dirname + '/upload'));
            this._app.listen(this._port, () => {
                console.log(`✅ Server läuft auf Port ${this._port}`);
                console.log(`📊 SQLite Datenbank aktiv`);
                
                // Starte Scheduler für automatisches Cleanup
                SchedulerService.start();
            });
        } catch (error) {
            console.error('💥 Fehler beim Serverstart:', error);
            process.exit(1);
        }
    }
}

module.exports = Server;