import express from 'express';
import cors from 'cors';
import passport from 'passport';
import authRoutes from './authentication/routes';
import { connectToDatabaseServer } from './db-utils';
import sendGridMail from '@sendgrid/mail';
import { userProfileRoutes } from './user-profile/profile-routes';
import { categoryRoutes } from './user-profile/category-routes';
import { shelfRoutes } from './user-profile/shelf-routes';
import { libraryRoutes } from './library/library-routes';
import { recollectionRoutes } from './revision/revision-routes';
import { recollectionHistoryRoutes } from './user-profile/recollection-history-routes';

const SENDGRID_API_KEY = process.env.SENDGRID_API_KEY || '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';

sendGridMail.setApiKey(SENDGRID_API_KEY);

const app = express();
app.use(cors());
app.use(express.json());
app.use(passport.initialize());
app.set('port', process.env.PORT || 8001);
app.use('/auth/', authRoutes);
app.use('/user/', userProfileRoutes);
app.use('/category/', categoryRoutes);
app.use('/shelf/', shelfRoutes);
app.use('/library/', libraryRoutes);
app.use('/recollection/', recollectionRoutes);
app.use('/recollection-history/', recollectionHistoryRoutes);

app.get('/', (request, response) => {
    response.send('Server running @ port' + app.get('port'));
});

const server = app.listen(app.get('port'), () => {
    connectToDatabaseServer();
    sendGridMail.setApiKey(SENDGRID_API_KEY);
});