const express = require('express');
const path = require('path');
const logger = require('morgan');
const cors = require('cors');
const helmet = require('helmet');
const apiLimiter = require('./helpers/apiLimiter');
const chatsRouter = require('./routes/chats');
const messagesRouter = require('./routes/messages');
const contactsRouter = require('./routes/contacts');
const userRoute = require('./routes/user');
const app = express();
const DIR_FILES = process.env.DIR_FILES;
const DIR_NOTIFICATIONS = process.env.DIR_NOTIFICATIONS;
const formatsLogger = app.get('env') === 'development' ? 'dev' : 'short';
app.use(helmet());
app.use(logger(formatsLogger));
app.use(cors());
app.use(express.json());
app.use(express.static(path.join(__dirname, DIR_FILES)));
app.use(express.static(path.join(__dirname, DIR_NOTIFICATIONS)));
app.use('/api/chats', apiLimiter, chatsRouter);
app.use('/api/messages', apiLimiter, messagesRouter);
app.use('/api/contacts', apiLimiter, contactsRouter);
app.use('/api', apiLimiter, userRoute);
app.use((_req, res) => res.status(404).json({ message: 'Not found' }));
app.use((err, _req, res, _next) =>
	res.status(500).json({ message: err.message })
);

module.exports = app;
