// Import core modules
const express = require("express");
const cors = require("cors");
const bodyParser = require("body-parser");
const path = require("path");
const compression = require("compression");
const cookieParser = require("cookie-parser");
const http = require("http");
const { Server } = require("socket.io");

// Load environment variables
require("dotenv").config({
  path: path.resolve(process.cwd(), `.env.${process.env.NODE_ENV || "development"}`)
});

// Import custom utilities and handlers
const logger = require("./server/utils/logger");

// Express app and server setup
const app = express();
const port = process.env.PORT || 5000;
const server = http.createServer(app);
const io = new Server(server, {
  path: "/ws",
  cors: {
    origin: [
      "https://test.imex.online",
      "https://www.test.imex.online",
      "http://localhost:3000",
      "https://imex.online",
      "https://www.imex.online",
      "https://romeonline.io", //Added in all RO and PM routes to simplyify setup.
      "https://www.romeonline.io",
      "https://beta.test.romeonline.io",
      "https://www.beta.test.romeonline.io",
      "https://beta.romeonline.io",
      "https://www.beta.romeonline.io",
      "https://beta.test.imex.online",
      "https://www.beta.test.imex.online",
      "https://beta.imex.online",
      "https://www.beta.imex.online",
      "https://www.test.promanager.web-est.com",
      "https://test.promanager.web-est.com",
      "https://www.promanager.web-est.com",
      "https://www.promanager.web-est.com"
    ],
    methods: ["GET", "POST"],
    credentials: true,
    exposedHeaders: ["set-cookie"]
  }
});
exports.io = io;

require("./server/web-sockets/web-socket");

// Middleware
app.use(compression());
app.use(cookieParser());
app.use(bodyParser.json({ limit: "50mb" }));
app.use(bodyParser.urlencoded({ limit: "50mb", extended: true }));
app.use(cors({ credentials: true, exposedHeaders: ["set-cookie"] }));

// Helper middleware
app.use((req, res, next) => {
  req.logger = logger;
  next();
});

// Route groupings
app.use("/", require("./server/routes/miscellaneousRoutes"));
app.use("/notifications", require("./server/routes/notificationsRoutes"));
app.use("/render", require("./server/routes/renderRoutes"));
app.use("/mixdata", require("./server/routes/mixDataRoutes"));
app.use("/accounting", require("./server/routes/accountingRoutes"));
app.use("/qbo", require("./server/routes/qboRoutes"));
app.use("/media", require("./server/routes/mediaRoutes"));
app.use("/sms", require("./server/routes/smsRoutes"));
app.use("/job", require("./server/routes/jobRoutes"));
app.use("/scheduling", require("./server/routes/schedulingRoutes"));
app.use("/utils", require("./server/routes/utilRoutes"));
app.use("/data", require("./server/routes/dataRoutes"));
app.use("/adm", require("./server/routes/adminRoutes"));
app.use("/tech", require("./server/routes/techRoutes"));
app.use("/intellipay", require("./server/routes/intellipayRoutes"));
app.use("/cdk", require("./server/routes/cdkRoutes"));
app.use("/csi", require("./server/routes/csiRoutes"));
app.use("/payroll", require("./server/routes/payrollRoutes"));

// Default route for forbidden access
app.get("/", (req, res) => {
  res.status(200).send("Access Forbidden.");
});

const main = async () => {
  await server.listen(port);
};

// Start server
main()
  .then(() => {
    logger.log(`[${process.env.NODE_ENV || "DEVELOPMENT"}] Server started on port ${port}`, "INFO", "api");
  })
  .catch((error) => {
    logger.log(
      `[${process.env.NODE_ENV || "DEVELOPMENT"}] Server failed to start on port ${port}`,
      "ERROR",
      "api",
      error
    );
  });
