const express = require("express");
const router = express.Router();


router.post("/send", (req, res) => {

    const { name, email, subject, contact, message } = req.body;

    //NodeMailer
    var nodemailer = require('nodemailer');

    let transporter = nodemailer.createTransport({
        service: 'gmail',
        auth: {
            user: 'randyruch5@gmail.com',
            pass: 'luxlrqakfenxkyzi'
        }
    });

    let mailOptions = {
        from: 'randyruch5@gmail.com',
        to: 'pasindupeiris829@gmail.com', // Replace with the system admin's email address
        subject: 'New Contact Form Submission',
        html: `
        <html>
        <body style="font-family: Arial, sans-serif; color: #333;">
            <div style="max-width: 600px; margin: auto; padding: 20px; border: 1px solid #ddd; border-radius: 10px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);">
                <h2 style="color: #0056b3; border-bottom: 2px solid #0056b3; padding-bottom: 10px;">Contact Form Submission</h2>
                
                <div style="padding: 10px; border-left: 5px solid #0056b3; margin-bottom: 10px; background-color: #f0f8ff;">
                    <p style="font-size: 16px; line-height: 1.5;"><strong>Name:</strong> ${name}</p>
                </div>
                
                <div style="padding: 10px; border-left: 5px solid #28a745; margin-bottom: 10px; background-color: #e6ffee;">
                    <p style="font-size: 16px; line-height: 1.5;"><strong>Email:</strong> ${email}</p>
                </div>
                
                <div style="padding: 10px; border-left: 5px solid #ffc107; margin-bottom: 10px; background-color: #fff9e6;">
                    <p style="font-size: 16px; line-height: 1.5;"><strong>Subject:</strong> ${subject}</p>
                </div>
                
                <div style="padding: 10px; border-left: 5px solid #dc3545; margin-bottom: 10px; background-color: #ffe6e6;">
                    <p style="font-size: 16px; line-height: 1.5;"><strong>Contact Number:</strong> ${contact}</p>
                </div>
                
                <div style="padding: 10px; border-left: 5px solid #17a2b8; margin-bottom: 10px; background-color: #e6f9ff;">
                    <p style="font-size: 16px; line-height: 1.5;"><strong>Message:</strong></p>
                    <p style="font-size: 16px; line-height: 1.5; white-space: pre-line;">${message}</p>
                </div>
                
                <div style="background-color: #f7f7f7; padding: 10px; border-top: 1px solid #ccc; text-align: center;">
                    <p style="font-size: 12px; color: #777;">&copy; 2024 Education App. All rights reserved.</p>
                </div>
                
            </div>
        </body>
        </html>
    `
    };


    transporter.sendMail(mailOptions, function (error, info) {
        if (error) {
            console.log(error);
        } else {
            return res.send({ msg: "Success" })
        }
    });




})

module.exports = router;