require("dotenv").config();
const express = require("express");
const cors = require("cors");

const healthRoutes = require("./routes/health.routes");
const deviceRoutes = require("./routes/device.routes");
const sessionRoutes = require("./routes/session.routes");
const errorHandler = require("./middleware/errorHandler");
const { generalLimiter } = require("./middleware/rateLimit");

const app = express();
app.use(cors());
app.use(express.json({ limit: "1mb" }));
app.use(generalLimiter);

app.use(healthRoutes);
app.use(deviceRoutes);
app.use(sessionRoutes);

// error handler must be last
app.use(errorHandler);

const PORT = process.env.PORT || 8080;
app.listen(PORT, () => console.log(`✅ Backend running on port ${PORT}`));
