const express = require("express");
const http = require("http");
const cors = require("cors");
const path = require("path");

const app = express();
const server = http.createServer(app);
const PORT = 5000 || process.env.PORT;
const connectDB = require("./db/config");
require("dotenv").config();

app.use(cors({
    origin: process.env.AllowedOrigin
}));
app.use(express.json());

app.get("/", (req, res)=> {
    res.send("Sipline-Backend Is Working")
});

app.use("/",
    express.static( path.resolve(__dirname, "./public/admin/"))
);

app.use("/", 
    express.static( path.resolve(__dirname, "./public/user/"))
);

app.use("/", 
    express.static( path.resolve(__dirname, "./public/shop/"))
);

app.use("/",
    express.static( path.resolve(__dirname, "./public/product/"))
);

app.use("/api", require("./routes/admin"));
app.use("/api", require("./routes/user"));
app.use("/api", require("./routes/superAdmin"));

const start = async () => {
    try {
        connectDB();
        server.listen(PORT, ()=>{
            console.log(`Server is Running on PORT: ${PORT}`);
        })
    } catch (error) {
        console.log("Having Errors Running the Server: ", error)
    }
};

start();