var express = require("express");
const cors = require("cors");
// require('./utils/dbfunction').connect();
const path = require("path");
var app = express();
var db = require("./utils/database");

//MiddleWares
app.use(cors());
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(express.static("public"));

// app.use(function(req, res, next) {
//   res.header("Access-Control-Allow-Origin", "https://defecthandling.finehhdigital.com"); // update to match the domain you will make the request from
//   res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
//   next();
// });

// app.use((req, res, next) => {
// //   res.header('Access-Control-Allow-Origin', '*');
//   res.header('Access-Control-Allow-Origin', 'https://defecthandling.finehhdigital.com');
//   next();
// });

//Routes Varaibles

// for Defect
const defectCreation = require("./Apis/defect/createDefect");
const defectSelection = require("./Apis/defect/selectDefect");
const updateDefect = require("./Apis/defect/updateDefect");
const deleteDefect = require("./Apis/defect/deleterDefect");
const selectMachine = require("./Apis/defect/selectDefect");

//for Defect filter
const filterDefect = require("./Apis/defect/FilterDefect");
//For Users
const signup = require("./Apis/usersApis/signup");
const login = require("./Apis/usersApis/login");
const selectUser = require("./Apis/usersApis/selectUser");
const createUser = require("./Apis/usersApis/createUser");
const updateUser = require("./Apis/usersApis/updateUser");
const deleteUser = require("./Apis/usersApis/deleteUser");
const changePass = require("./Apis/usersApis/userPasswordReset");
//for Department
const dep = require("./Apis/departments/departments");
const updateDepartment = require("./Apis/departments/Update");
const deleteDepartment = require("./Apis/departments/Delete");
//for Machine
const machines = require("./Apis/machines/machineType");
const updateMachine = require("./Apis/machines/Update");
const deleteMachine = require("./Apis/machines/Delete");

// for Line
// const insertLine = require('./apis/linesApis/createLines');

// for system
const systemCreation = require("./Apis/Systems/Create");
const systemSelection = require("./Apis/Systems/Select");
const updatesystem = require("./Apis/Systems/Update");
const deletesystem = require("./Apis/Systems/Delete");

// for defect Type
const dtCreation = require("./Apis/DefectTypes/Create");
const dtSelection = require("./Apis/DefectTypes/Select");
const updatedt = require("./Apis/DefectTypes/Update");
const deletedt = require("./Apis/DefectTypes/Delete");

// for defect Parts
const dpCreation = require("./Apis/DefectParts/Create");
const dpSelection = require("./Apis/DefectParts/Select");
const updatedp = require("./Apis/DefectParts/Update");
const deletedp = require("./Apis/DefectParts/Delete");

//for userPriority parts
const upCreation = require("./Apis/userPriorities/create");
const upDelete = require("./Apis/userPriorities/delete");
const upSelection = require("./Apis/userPriorities/select");
const upUpdate = require("./Apis/userPriorities/update");

//Routes
//for Defect
app.use("/api/defects", defectCreation);
app.use("/api/defects", defectSelection);
app.use("/api/defects", updateDefect);
app.use("/api/defects", deleteDefect);
app.use("/api/defects", selectMachine);

//for Defect filter
app.use("/api/defectFilter", filterDefect);
//for users
app.use("/api/users", signup);
app.use("/api/users", login);
app.use("/api/users", selectUser);
app.use("/api/users", createUser);
app.use("/api/users", updateUser);
app.use("/api/users", deleteUser);
app.use("/api/password", changePass);
// for Department
app.use("/api/departments", dep);
app.use("/api/departments", updateDepartment);
app.use("/api/departments", deleteDepartment);
//for Machine
app.use("/api/machines", machines);
app.use("/api/machines", updateMachine);
app.use("/api/machines", deleteMachine);

// for Line
// app.use("/api/lines",insertLine);

// for systems
app.use("/api/systems", systemCreation);
app.use("/api/systems", systemSelection);
app.use("/api/systems", updatesystem);
app.use("/api/systems", deletesystem);

// for defect type(dt)
app.use("/api/defectTypes", dtCreation);
app.use("/api/defectTypes", dtSelection);
app.use("/api/defectTypes", updatedt);
app.use("/api/defectTypes", deletedt);

// for defect part(dp)
app.use("/api/defectParts", dpCreation);
app.use("/api/defectParts", dpSelection);
app.use("/api/defectParts", updatedp);
app.use("/api/defectParts", deletedp);

// for userPriority part(up)
app.use("/api/userPriority", upCreation);
app.use("/api/userPriority", upDelete);
app.use("/api/userPriority", upSelection);
app.use("/api/userPriority", upUpdate);

//Test Api
app.get("/", (req, res) => {
  res.send("CODE: PLUTO");
});
app.get("/test", (req, res) => {
  res.send("CODE: PLUTO");
});
app.get("/get", (req, res) => {
  db.getConnection((err, connection) => {
    if (err) {
      console.log("database not connected error: >>>", err);
      throw err;
    }
    console.log("get Connection");
    res.send("with connection limits");
    console.log("release Connection");
  });
});

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