import express from "express";
import mongoose from "mongoose";
import mysql from "mysql2";
import cors from "cors";
import morgan from "morgan";
import session from "express-session";
import userRouter from "./routes/user.js";
import propertyRouter from "./routes/property.js";
import mysqlRouter from "./routes/mysqlproperty.js";
import dotenv from "dotenv";

const app = express();
dotenv.config();

app.use(morgan("dev"));
app.use(express.json({ limit: "30mb", extended: true }));
app.use(express.urlencoded({ limit: "30mb", extended: true }));
app.use(cors());

// app.use(cors({
//   origin: 'http://67.225.129.127:81', // Allow requests from your frontend
//   methods: 'GET,POST', // Specify allowed methods
// }));
app.use(express.static('dist'));

app.use(
  session({
    secret: process.env.SECRET_KEY,
    resave: false,
    saveUninitialized: true,
  })
);

app.use("/users", userRouter);
app.use("/properties", propertyRouter);
app.use("/mysql", mysqlRouter); // Use MySQL routes



app.get("/", (req, res) => {
  res.send("Welcome to EF-API");
});

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

// Connect to the database
const dbConnectionPromise = mongoose.connect(process.env.DB_ACCESS, {
  // useNewUrlParser: true,
  // useUnifiedTopology: true,
});

// Start HTTP server
const startHttpServer = () => {
  app.listen(port, () => {
    console.log(`Server is running on port ${port} (HTTP)`);
  });
}; startHttpServer();

// Handle database connection success/failure
dbConnectionPromise
  .then(() => {
    console.log("Connected to the MONGO database");
  })
  .catch((error) => {
    console.log(`${error} did not connect`);
  });

