const db = require("../../../managers/dbConfig");
const bcryptjs = require("bcryptjs");
//const jwt = require("jsonwebtoken");
const { jwbToken1 } = require("../../../managers/jwtManager");
const sendEmail = require("../../../managers/mailManager");
const { getFormattedDateTime12h } = require("../../../managers/formatDateTime");

const bpLogin = async (req, res) => {
  let { email, password, phoneNumber } = req.body;
  if (!phoneNumber & !email) throw "email or phone Number must be provided";
  if (!password) throw "Password must be provided";

  const sql = "SELECT * FROM staffs WHERE email = ? OR phone_num =?";
  const [rows] = await db.query(sql, [email, phoneNumber]);

  let getUser = rows[0];
  if (!getUser) throw "User NOT FOUND";

  //   if (getUser.cadre != "accountant")
  //     throw `You're not permitted to Login here. Please go back to login as ${getUser.cadre}`;

  const comparedPassword = await bcryptjs.compare(password, getUser.password);

  if (comparedPassword === false)
    throw `You entered wrong password for ${email}`;

  const firstName = getUser.firstName;
  const lastName = getUser.lastName;
  const dateTime = getFormattedDateTime12h();
  const token = jwbToken1(getUser);
  delete getUser.password;
  delete getUser.id;
  // const data = ["password", "email"].forEach(field => delete getUser[field]);

  const message = `
     <!DOCTYPE html>
        <html>
        <head>
            <meta charset="UTF-8">
            <title>Login Notification</title>
            <style>
                body { font-family: Arial, sans-serif; background-color: #f4f4f4; padding: 20px; }
                .container {
            max-width: 500px;
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            margin: 20px auto; /* Reducing top margin to fix extra space */
        }
                .header { text-align: center; font-size: 20px; font-weight: bold; color: #333; }
                .content { margin-top: 15px; font-size: 16px; color: #555; }
                .message { font-size: 18px; color: #007bff; text-align: center;
                           margin: 20px 0; padding: 10px; background: #d1ecf1; border-radius: 5px; }
                .footer { margin-top: 20px; font-size: 14px; color: #777; text-align: center; }
            </style>
        </head>
        <body>
            <div class="container">
                <div class="header">Login Notification</div>
                <div class="content">
                    <p>Hi <strong>${firstName} ${lastName}</strong>,</p>
                    <p>You have successfully logged into your account on <strong>${dateTime}</strong>.</p>
                    <div class="message">If this wasn't you, please secure your account immediately.</div>
                    <p>If this was you, no further action is needed.</p>
                </div>
                <div class="footer">
                    &copy; ${new Date().getFullYear()} ${
    process.env.hosp_name
  }. All rights reserved.
                </div>
            </div>
        </body>
        </html>.`;
  const subject = `NOTIFICATION`;
  let sEmail = getUser.email;

  // await sendEmail(sEmail, subject, message);

  res.status(200).json({
    message: "Login successful",
    data: getUser,
    token,
  });
};

module.exports = bpLogin;
