const Admin = require(
  "../models/Admin"
);

const bcrypt = require(
  "bcryptjs"
);

const jwt = require(
  "jsonwebtoken"
);

exports.login = async (
  req,
  res
) => {
  try {
    const { email, password } =
      req.body;

    const admin =
      await Admin.findOne({
        email,
      });

    if (!admin) {
      return res
        .status(401)
        .json({
          message:
            "Invalid Email",
        });
    }

    const match =
      await bcrypt.compare(
        password,
        admin.password
      );

    if (!match) {
      return res
        .status(401)
        .json({
          message:
            "Invalid Password",
        });
    }

    const token =
      jwt.sign(
        {
          id: admin._id,
        },
        process.env.JWT_SECRET,
        {
          expiresIn: "7d",
        }
      );

    res.json({
      token,
      admin,
    });
  } catch (error) {
    res
      .status(500)
      .json(error);
  }
};