//Setting up the project
import express, { Express } from "express";
import dotenv from "dotenv";
import cors from "cors";
import { Server } from "http";

import { PORT, CORS_ORIGIN } from "./constants";
import ioServer from "./io";

dotenv.config();

const app: Express = express();
const http = new Server(app);
const corsOptions = {
  origin: CORS_ORIGIN,
  optionsSuccessStatus: 200,
};

app.use(express.json());
app.use(cors(corsOptions));
ioServer(http);

app.get("/", async (req, res) => {
  // const passWirus = await bcrypt.hash("hi&kjA@D&g9L@sA9@iaC6t", 10);
  // const passYouro = await bcrypt.hash("7AxMKo7#pkc@MqeLHyFdKX", 10);
  res.json({
    message: "Hello world",
  });
});

app.post("/login", async (req, res) => {
  if (req.body?.username === "Youro" && req.body?.password === "7AxMKo7#pkc@MqeLHyFdKX") {
    res.json({ success: "OK" });
  } else if (req.body?.username === "Wirus" && req.body?.password === "hi&kjA@D&g8L@sA2@iaC6t") {
    res.json({ success: "OK" });
  } else {
    res.status(401);
    res.json({ error: "Incorrect username or password." });
  }
});

http.listen(PORT, () => {
  console.log(`Server listening on ${PORT}`);
});
