import 'dotenv/config';
import express from 'express';
import path from 'path';
import 'express-async-errors';
import cors from 'cors';

import './database/connection';
import errorHandler from './errors/handler';
import routes from './routes';

const app = express();

app.use(cors());
app.use(express.json());

app.use('/api', routes);
const uploadsPath = path.join(process.cwd(), 'uploads');
app.use('/uploads', express.static(uploadsPath));
app.use(routes);
app.use(errorHandler);

app.listen(process.env.PORT || 3101, () => {
    console.log(`Serving static files for /uploads from: ${uploadsPath}`);
    console.log('Server is running on http://localhost:' + (process.env.PORT || 3101));
    console.log('Environment:', process.env.NODE_ENV || 'development');
});