var express = require("express");
const cors = require("cors");
const port = 5500;
// require('./utils/dbfunction').connect();
const path = require("path");
var app = express();
//
//MiddleWares
app.use(cors());
// app.use(express.urlencoded({ extended: true }));
// app.use(express.json())
app.use(express.json({ limit: "50mb" }));
app.use(express.urlencoded({ limit: "50mb", extended: true }));
app.use(express.static("public"));

//Routes Varaibles =========================================================================================>

// for setCIL
const createCIL = require("./Apis/setCIL/createCIL");
const selectCIL = require("./Apis/setCIL/selectCIL");
const updateCIL = require("./Apis/setCIL/updateCIL");
const deleteCIL = require("./Apis/setCIL/deleteCIL");
//for CIL filter
// const filterCIL = require("./Apis/ConfigCIL/FilterCIL")

//for feedback
const createFeedback = require("./Apis/Feedback/createFeedback");
const selectFeedback = require("./Apis/Feedback/selectFeedback");
const getDepartment = require("./Apis/Feedback/selectFeedback");
const getArea = require("./Apis/Feedback/selectFeedback");
const getSubAssembly = require("./Apis/Feedback/selectFeedback");
const selectMachineName = require("./Apis/Feedback/selectFeedback");

//for configration
const createConfig = require("./Apis/Configration/createConfig");
const updateConfig = require("./Apis/Configration/updateConfig");
const selectConfig = require("./Apis/Configration/selectConfig");
const deleteConfig = require("./Apis/Configration/deleteConfig");

//for setOperationDescription
const createOD = require("./Apis/setOperationDescription/createOD");
const selectOD = require("./Apis/setOperationDescription/selectOD");
const updateOD = require("./Apis/setOperationDescription/updateOD");
const deleteOD = require("./Apis/setOperationDescription/deleteOD");

//for select CILData according to the machine
// const selectCILData = require("./Apis/ConfigCIL/selectCIL")

//Routes======================================================================================================>
//for setCIL
app.use("/api/setCIL", createCIL);
app.use("/api/setCIL", selectCIL);
app.use("/api/setCIL", updateCIL);
app.use("/api/setCIL", deleteCIL);

//for feedback
app.use("/api/feedback", createFeedback);
app.use("/api/feedback", selectFeedback);
app.use("/api/feedback", getDepartment);
app.use("/api/feedback", selectMachineName);
app.use("/api/feedback", getArea);
app.use("/api/feedback", getSubAssembly);

//for configration
app.use("/api/config", createConfig);
app.use("/api/config", updateConfig);
app.use("/api/config", deleteConfig);
app.use("/api/config", selectConfig);

//for setOperationDescription
app.use("/api/setOperationDescription", createOD);
app.use("/api/setOperationDescription", selectOD);
app.use("/api/setOperationDescription", updateOD);
app.use("/api/setOperationDescription", deleteOD);

//Test Api
app.get("/", (req, res) => {
  res.send("CODE: PLUTO");
});
app.get("/test", (req, res) => {
  db.getConnection((err, connection) => {
    if (err) {
      console.log("database not connected error: >>>", err);
      res.send(500).status({
        msg: "Error from database",
        err: err,
      });
    }
    console.log("get connection");
    res.send("CODE: PLUTO");
    console.log("release connection");
  });
});

//Server Listining
app.listen(port, "0.0.0.0", () => {
  console.log(`Example app listening at http://localhost:${port}`);
});
