const express = require("express");
const cors = require("cors");
const bodyParser = require("body-parser");
const errorHandler = require("./_helper/errorHandler")
const session = require("express-session")
const cookieParser = require("cookie-parser")

const app = express();
const PORT = 3000;

app.use(cookieParser("hokie"))
app.use(session({
  secret: 'hokie',
  resave: true,
  saveUninitialized:true
}))

// Existing user router
const userRouter = require("./routes/user.router");

// New: Item router
const itemRouter = require("./routes/item.router");

const chatRouter = require("./routes/chat.router");

app.get("/", (req, res) => {
  res.status(200);
  res.send("Hello World");
});

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
const corsOptions = {
  origin: "http://localhost:3001", // Replace with the actual origin of your frontend.
  methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
};

app.use(cors(corsOptions));
// Existing: User routes
app.use("/user", userRouter);

// New: Item routes
app.use("/item", itemRouter);


app.use("/chat", chatRouter);

app.use("/image", express.static("./uploads"))



app.use(errorHandler)

app.listen(PORT, (error) => {
  if (!error) {
    console.log(
      "Server is Successfully Running, and App is listening on port " + PORT
    );
  } else {
    console.log("Error occurred, server can't start", error);
  }
});
