const express = require("express");
const app = express();
const { config } = require("dotenv");
const flash = require("express-flash");
config();
const PORT = process.env.PORT;
const cookieParser = require("cookie-parser");
const sessions = require("express-session");
var cors = require("cors");
const path = require("path");
const { db } = require("./utils");
var cron = require("node-cron");
const {
  checkFeaturedPropertiesExpiry,
  checkPlanExpiry,
} = require("./controllers/web/CronJobController");
const oneDay = 1000 * 60 * 60 * 24;

app.use(cors());

app.use(
  sessions({
    secret: "thisismysecrctekeyfhrgfgrfrty84fwir767",
    saveUninitialized: true,
    cookie: { maxAge: oneDay },
    resave: false,
  })
);

app.use(flash());
app.use(express.json({ limit: "50mb" }));
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.set("view engine", "ejs");
app.use(express.static(path.join(__dirname, "public")));

app.use("/", require("./routes"));

app.get("/test", function (req, res) {
  return res.send({ status: true, msg: "server working perfectly" });
});

cron.schedule("0 23 * * *", () => {
  console.log("running a task every mid night");
  checkFeaturedPropertiesExpiry();
  checkPlanExpiry();
});

db.sync({ sync: true }).then((req) => {
  app.listen(PORT, () =>
    console.log(`Example app is listening on port ${PORT}.`)
  );
});
