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

// Dev: Swagger UI (mount only in non-production) — require lazily
let swaggerUi = null;
try {
    swaggerUi = require('swagger-ui-express');
} catch (e) {
    swaggerUi = null;
}

class Server {
    _port;
    _app;

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

    async generateOpenApiSpecIfNeeded() {
        if (process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'test') {
            return;
        }

        try {
            console.log('🔄 Generating OpenAPI specification...');
            await generateOpenApi();
            console.log('✓ OpenAPI spec generated');
        } catch (error) {
            console.warn('⚠️  Failed to generate OpenAPI spec:', error.message);
        }
    }

    loadSwaggerDocument() {
        try {
            const specPath = path.join(__dirname, '..', 'docs', 'openapi.json');
            const raw = fs.readFileSync(specPath, 'utf8');
            return JSON.parse(raw);
        } catch (error) {
            console.warn('⚠️  Unable to load Swagger document:', error.message);
            return null;
        }
    }

    async start() {
        try {
            await this.generateOpenApiSpecIfNeeded();

            // 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.use('/api/previews', express.static( __dirname + '/data/previews'));

            // Mount Swagger UI in dev only when available
            if (process.env.NODE_ENV !== 'production' && swaggerUi) {
                const swaggerDocument = this.loadSwaggerDocument();
                if (swaggerDocument) {
                    this._app.use('/api/docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
                    console.log('ℹ️  Swagger UI mounted at /api/docs (dev only)');
                }
            }
            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);
        }
    }

    // Expose app for testing
    getApp() {
        return this._app;
    }

    // Initialize app without listening (for tests)
    async initializeApp() {
        await dbManager.initialize();
        initiateResources(this._app);
        this._app.use('/upload', express.static( __dirname + '/upload'));
        this._app.use('/api/previews', express.static( __dirname + '/data/previews'));
        
        if (process.env.NODE_ENV !== 'production' && swaggerUi && swaggerDocument) {
            this._app.use('/api/docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
        }
        return this._app;
    }
}

module.exports = Server;