text: const bcrypt = require("bcryptjs");
  const jwt = require("jsonwebtoken");
  
  const mongoose = require("mongoose");
  
  const Admin = mongoose.model("Admin");
  
  require("dotenv").config({ path: ".variables.env" });
  
  exports.register = async (req, res) => {
    try {
      let { email, password, passwordCheck, name } = req.body;
  
      if (!email || !password || !passwordCheck)
        return res.status(400).json({ msg: "Not all fields have been entered." });
      if (password.length < 5)
        return res
          .status(400)
          .json({ msg: "The password needs to be at least 5 characters long." });
      if (password !== passwordCheck)
        return res
          .status(400)
          .json({ msg: "Enter the same password twice for verification." });
  
      const existingAdmin = await Admin.findOne({ email: email });
      if (existingAdmin)
        return res
          .status(400)
          .json({ msg: "An account with this email already exists." });
  
      if (!name) name = email;
  
      const salt = await bcrypt.genSalt();
      const passwordHash = await bcrypt.hash(password, salt);
  
      const newAdmin = new Admin({
        email,
        password: passwordHash,
        name,
      });
      const savedAdmin = await newAdmin.save();
      res.status(200).send({
        success: true,
        admin: {
          id: savedAdmin._id,
          name: savedAdmin.name,
          surname: savedAdmin.surname,
        },
      });
    } catch (err) {
      res.status(500).json({
        success: false,
        result: null,
        message: err.message,
      });
    }
  };
  
  exports.login = async (req, res) => {
    try {
      const { email, password } = req.body;
      console.log(
        "🚀 ~ file: authJwtController .js ~ line 62 ~ exports.login= ~ req.body",
        req.body
      );
  
      // validate
      if (!email || !password)
        return res.status(400).json({
          success: false,
          result: null,
          message: "Not all fields have been entered.",
        });
  
      const admin = await Admin.findOne({ email: email, removed: false });
      // console.log(admin);
      if (!admin)
        return res.status(400).json({
          success: false,
          result: null,
          message: "No account with this email has been registered.",
        });
  
      const isMatch = await bcrypt.compare(password, admin.password);
      if (!isMatch)
        return res.status(400).json({
          success: false,
          result: null,
          message: "Invalid credentials.",
        });
  
      const token = jwt.sign(
        {
          id: admin._id,
        },
        process.env.JWT_SECRET,
        { expiresIn: "72h" }
      );
  
      const result = await Admin.findOneAndUpdate(
        { _id: admin._id },
        { isLoggedIn: true },
        {
          new: true,
        }
      ).exec();
  
      res.cookie("token", token, {
        maxAge: 72 * 60 * 60 * 1000,
        httpOnly: true,
        sameSite: "none",
        secure: true,
      });
  
      res.json({
        success: true,
        result: {
          token,
          admin: {
            id: result._id,
            name: result.name,
            isLoggedIn: result.isLoggedIn,
          },
        },
        message: "Successfully login admin",
      });
    } catch (err) {
      // res.status(500).json({ success: false, result:null, message: err.message });
      res
        .status(500)
        .json({ success: false, result: null, message: err.message, error: err });
    }
  };
  
  exports.isValidToken = async (req, res, next) => {
    try {
      const token = req.cookies.token;
      console.log(
        "🚀 ~ file: authJwtController .js ~ line 130 ~ exports.isValidToken= ~ token",
        token
      );
  
      if (!token)
        return res.status(401).json({
          success: false,
          result: null,
          message: "No authentication token, authorization denied.",
          jwtExpired: true,
        });
  
      const verified = jwt.verify(token, process.env.JWT_SECRET);
  
      if (!verified)
        return res.status(401).json({
          success: false,
          result: null,
          message: "Token verification failed, authorization denied.",
          jwtExpired: true,
        });
  
      const admin = await Admin.findOne({ _id: verified.id, removed: false });
      if (!admin)
        return res.status(401).json({
          success: false,
          result: null,
          message: "Admin doens't Exist, authorization denied.",
          jwtExpired: true,
        });
  
      if (admin.isLoggedIn === false)
        return res.status(401).json({
          success: false,
          result: null,
          message: "Admin is already logout try to login, authorization denied.",
          jwtExpired: true,
        });
      else {
        req.admin = admin;
        // console.log(req.admin);
        next();
      }
    } catch (err) {
      res.status(503).json({
        success: false,
        result: null,
        message: err.message,
        error: err,
      });
    }
  };
  
  exports.logout = async (req, res) => {
    const result = await Admin.findOneAndUpdate(
      { _id: req.admin._id },
      { isLoggedIn: false },
      {
        new: true,
      }
    ).exec();
  
    res.clearCookie("token");
    res.json({ isLoggedOut: true });
  };
  