import express from "express";
import dotenv from "dotenv";
import authRouter from "./routes/authRoutes";
import { authenticateToken } from "./middleware/authenticateToken";
import { mongoClient } from "./services/mongoDb";
import cors from "cors";
import cookieParser from "cookie-parser";

//test
import { model, Schema, Model, Document } from "mongoose";
import productRouter from "./routes/productRoutes";
import trackingRouter from "./routes/trackingRoutes";

const app = express();

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

dotenv.config();
mongoClient.connect();

app.use(express.json());
app.use(express.urlencoded({ extended: true }));

app.use("/api/auth", authRouter);
app.use("/api/product", productRouter);
app.use("/api/tracking", authenticateToken, trackingRouter);

app.use(function (err, req, res, next) {
  console.error(err.stack);
  res.status(500).send("Something broke!");
});

const hostname = "0.0.0.0";
const port = 8080;

app.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});
