import express from 'express';
import cors from 'cors';
import 'dotenv/config';
import cookieParser from 'cookie-parser';
import { connectDb } from './db/connectDb.js';
import userRoutes from './routes/userRoutes.js';
import productsRoutes from './routes/productsRoutes.js';
import warehouseRoutes from './routes/warehouseRoutes.js';
import stocktransactionRoutes from './routes/stocktransactionRoutes.js';
import reportRoutes from './routes/reportRoutes.js';
import { errorHandler, notFound } from './middleware/errorMiddleware.js';

export const app = express();
const PORT = process.env.PORT || 5000;

await connectDb();

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

app.use('/api/user', userRoutes);
app.use('/api/products', productsRoutes);
app.use('/api/warehouses', warehouseRoutes);
app.use('/api/stocktransactions', stocktransactionRoutes);
app.use('/api/reports', reportRoutes);

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

app.listen(PORT, () => {
  console.log(`Server running on http://localhost:${PORT}`);
});