const { applyMiddlewares } = require('../middlewares');
const { renderRoutes } = require('../routes/index');
const removeImages = require('./remove-images');
const fs = require('fs');
const path = require('path');
const { endpoints, UPLOAD_FS_DIR } = require('../constants');


const initiateResources = (app) => {
    applyMiddlewares(app);

    renderRoutes(app);

    // Ensure upload images directory exists: backend/src/../data/images
    const imagesDir = path.join(__dirname, '..', UPLOAD_FS_DIR);
    if (!fs.existsSync(imagesDir)){
        fs.mkdirSync(imagesDir, { recursive: true });
    }

    // Ensure db directory exists: backend/src/../data/db
    const dbDir = path.join(__dirname, '..', 'data', 'db');
    if (!fs.existsSync(dbDir)){
        fs.mkdirSync(dbDir, { recursive: true });
    }

    if(process.env.REMOVE_IMAGES === 'true') {
        removeImages();
    };
};

module.exports = initiateResources;