const cors = require('cors');
const express = require('express');
const path = require('path');
const authRoutes = require('./routes/authRoutes');
const customerRoutes = require('./routes/customerRoutes');
const dashboardRoutes = require('./routes/dashboardRoutes');
const loanRoutes = require('./routes/loanRoutes');
const privilegeRoutes = require('./routes/privilegeRoutes');
const reportRoutes = require('./routes/reportRoutes');
const userRoutes = require('./routes/userRoutes');
const {errorHandler, notFound} = require('./middleware/errorHandler');
const {success} = require('./utils/apiResponse');

const app = express();

app.use(cors());
app.use(express.json({limit: '10mb'}));
app.use(express.urlencoded({extended: true, limit: '10mb'}));

app.get('/health', (req, res) => success(res, {status: 'ok'}, 'Backend is running'));
app.use('/uploads', express.static(path.resolve(__dirname, '../uploads')));

app.use('/auth', authRoutes);
app.use('/dashboard', dashboardRoutes);
app.use('/customers', customerRoutes);
app.use('/loans', loanRoutes);
app.use('/reports', reportRoutes);
app.use('/users', userRoutes);
app.use('/privileges', privilegeRoutes);

app.use(notFound);
app.use(errorHandler);

module.exports = {app};
