const db = require("../../../managers/dbConfig");
const sendEmail = require("../../../managers/mailManager");

const registerHosp = async (req, res) => {
  let { name, code_name, address, email, phone_num, slogan } = req.body;

  if (!name) throw "name is required";
  if (!address) throw "address is required";
  if (!email) throw "email is required";
  if (!phone_num) throw "phone number is required";

  slogan = slogan ?? null;
  code_name = code_name ?? null;
  const insertSql =
    "INSERT INTO hosp_setting(name,code_name, address, phone_num, email, slogan) VALUES(?,?,?,?,?,?)";
  const checkerSql = "SELECT * FROM hosp_setting WHERE email=? OR phone_num=?";

  const [existing] = await db.execute(checkerSql, [email, phone_num]);

  if (existing[0] !== undefined)
    throw `A facility with the email ${email} or Phone Number ${phone_num} already exist.`;
  await db.execute(insertSql, [
    name,
    code_name,
    address,
    phone_num,
    email,
    slogan,
  ]);

  const subject = "Welcome Onboard";
  const msg = `<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Welcome to G.O-EMR</title>
    <style>
        body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; }
        .container { max-width: 600px; margin: 0 auto; padding: 20px; border: 1px solid #ddd; border-radius: 5px; background-color: #f9f9f9; }
        .btn { display: inline-block; padding: 10px 15px; margin-top: 10px; color: #fff; background-color: #007bff; text-decoration: none; border-radius: 5px; }
        .btn:hover { background-color: #0056b3; }
    </style>
</head>
<body>
    <div class="container">
        <h2>Welcome to G.O-EMR!</h2>
        <p>Dear <strong>Admin Officer</strong>,</p>
        <p>We’re excited to have <strong>${name}</strong> on board! Our hospital management software is designed to streamline operations, enhance efficiency, and improve patient care.</p>
        
        <h3>Next Steps:</h3>
        <ul>
            <li><strong>Log in to Your Admin Dashboard:</strong> <a href="[Login URL]" class="btn">Access Your Dashboard With Your SECRET KEYS</a></li>
            <li><strong>Register Your Hospital Staff:</strong> As the admin, you are responsible for creating accounts for doctors, nurses, and other staff members.</li>
            <li><strong>Explore the Features:</strong> Manage patients, appointments, billing, and more from one easy-to-use platform.</li>
        </ul>

        <p>If you need any assistance, our support team is available at <a href="mailto:support@1link.com.ng">support@1link.com.ng</a> or <strong>08133667861</strong>.</p>

        <p>We look forward to supporting your hospital’s success!</p>

        <p>Best regards,</p>
        <p><strong>Abdullahi Olaniyan</strong><br>
        <strong>Chief Technical Officer</strong><br>
        <strong>G.O-Developers Group</strong><br>
        <a href="https://wa.me/2348133667861">Click here to contact Us On whatsApp</a></p>
    </div>
</body>
</html>
`;

  await sendEmail(email, subject, msg);

  res.status(201).json({
    status: "success",
  });
};

module.exports = registerHosp;
