process.env.TZ = "Asia/Colombo";
require('dotenv').config();
const express = require("express");
const router = require("./routes");
const cors = require("cors");
const { ERROR_PAGE } = require("./utils/utility");
const paginationHandler = require('./utils/paginationHandler');
const successHandler = require('./utils/successHandler');
const errorHandler = require('./utils/errorHandler');
global.__basedir = __dirname;

const app = express();
app.use(express.json());
app.use(cors({ credentials: true, origin: true }));
app.use(express.urlencoded({ extended: true, }));
app.use(successHandler);
app.use(paginationHandler);


//Routes for the Path
app.use("/api", router);
app.use("/uploads", express.static("uploads"));

//Error Handle for unknown requests
app.all("*", (req, res, next) => {
  res.status(403).send(ERROR_PAGE);
});

app.use(errorHandler);

module.exports = app;


// DELETE `tbl_customer`
// FROM `tbl_customer`
// WHERE NOT EXISTS (
//     SELECT 1
//     FROM `areas`
//     WHERE `tbl_customer`.`area_id` = `areas`.`tbl_ref_id`
// )
// OR NOT EXISTS (
//     SELECT 1
//     FROM `landmarks`
//     WHERE `tbl_customer`.`land_mark` = `landmarks`.`tbl_ref_id`
// );

// DELETE FROM `tbl_advance`
// WHERE `sales_id` IN (
//     SELECT `sales_id`
//     FROM (
//         SELECT `sales_id`
//         FROM `tbl_advance`
//         WHERE NOT EXISTS (
//             SELECT 1
//             FROM `orders`
//             WHERE `tbl_advance`.`sales_id` = `orders`.`tbl_ref_id`
//         )
//         LIMIT 10000
//     ) AS subquery
// );

