const express = require("express");
const cors = require("cors");
const mongoose = require("mongoose");
const path = require("path");

mongoose.set("strictQuery", true);

const app = express();

require("dotenv").config({ path: ".env" });

app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, "public")));

// admin routes
app.use("/admin/v1/auth", require("./routes/admin/auth"));
app.use("/admin/v1/cms", require("./routes/admin/cms"));

// user routes
app.use("/user/v1/", require("./routes/user/userRoutes"));
app.use("/", (req, res) => {
  return res.send("Hello world");
});

const port = process.env.PORT || 5000;

if (mongoose.connections[0].readyState) {
  console.log("Mongodb already connected");
} else {
  mongoose
    .connect(process.env.MONGO_URI)
    .then(() => console.log("Mongodb connected successfully!"))
    .catch(() => console.log("Something went wrong in connecting Mongodb"));
}

app.listen(port, () => {
  console.log(`App is listening at port ${port}`);
});
