const nodemailer = require("nodemailer");
const moment = require("moment");
const Meeting = require("../models/Meeting");
const DepartmentEmail = require("../models/DepartmentEmail");

const sendEmail = async (req, res) => {
  try {
    const { program, department, subject, body, agenda, date, startTime, endTime, recipients } = req.body;
    const files = req.files;

    console.log({ program, department, subject, body, agenda, date, startTime, endTime, recipients, files });

    if (!program || !department || !subject || !body || !date || !startTime || !endTime || !recipients) {
      return res.status(400).json({ error: "All fields are required!" });
    }

    const departmentEmails = await DepartmentEmail.findOne({ department });
    if (!departmentEmails) {
      return res.status(404).json({ error: "Department emails not found" });
    }

    const allRecipients = [...new Set([...departmentEmails.emails, ...recipients])];

    const conflict = await Meeting.findOne({
      date,
      startTime: { $lt: endTime },
      endTime: { $gt: startTime },
    });

    const formattedDate = moment(date, "YYYY-MM-DD").format("dddd, MMMM Do YYYY");
    const formattedStartTime = moment(startTime, "HH:mm").format("hh:mm A");
    const formattedEndTime = moment(endTime, "HH:mm").format("hh:mm A");

    const formattedStart = moment(`${date} ${startTime}`, "YYYY-MM-DD HH:mm").utc().format("YYYYMMDDTHHmmss[Z]");
    const formattedEnd = moment(`${date} ${endTime}`, "YYYY-MM-DD HH:mm").utc().format("YYYYMMDDTHHmmss[Z]");

    const zoomLink = "https://us05web.zoom.us/j/2655616202?pwd=S3d6RjVPNnhmQ3AzTlZsRC9GYmNHQT09";
    const calendarLink = `https://calendar.google.com/calendar/render?action=TEMPLATE&text=${encodeURIComponent(
      subject
    )}&details=${encodeURIComponent(`${body}\nJoin Zoom Meeting: ${zoomLink}`)}&dates=${formattedStart}/${formattedEnd}`;

    const formattedAgenda = Array.isArray(agenda) && agenda.length > 0
      ? agenda.map((item, index) => `<li>${index + 1}. ${item}</li>`).join("")
      : "<li>No agenda provided</li>";

    const newMeeting = new Meeting({
      program,
      department,
      subject,
      body,
      agenda,
      date,
      startTime,
      endTime,
      recipients,
      attachments: files.map((file) => ({ filename: file.filename, path: file.path })),
    });
    await newMeeting.save();

    let conflictDetails = null;

    if (conflict) {
      conflictDetails = {
        department: conflict.department,
        date: conflict.date,
        startTime: conflict.startTime,
        endTime: conflict.endTime,
        recipients: conflict.recipients,
        subject: conflict.subject,
        agenda: conflict.agenda || "No agenda provided",
      };
      console.log("Conflict detected! Details:", conflictDetails);
    }

    const transporter = nodemailer.createTransport({
      service: "gmail",
      auth: {
        user: process.env.EMAIL,
        pass: process.env.EMAIL_PASSWORD,
      },
    });

    const mailOptions = {
      from: process.env.EMAIL,
      to: recipients,
      subject,
      html: `
        <p><strong>Respected Board Members,</strong></p>
        <p><strong>Greetings from Somaiya Vidyavihar University !!</strong></p>
        <p><strong>We are glad to invite you to the Board of Studies meeting: </strong></p>
        <p>${body}</p>
        <p><strong>Program:</strong> ${program}</p>
        <p><strong>Department:</strong> ${department}</p>
        <p><strong>Date:</strong> ${formattedDate}</p>
        <p><strong>Time:</strong> ${formattedStartTime} - ${formattedEndTime}</p>
        <p><strong>Zoom Meeting Details:</strong></p>
        <p><strong>Meeting ID: </strong>265 561 6202</p>
        <p><strong>Meeting Password: </strong>123456</p>
        <p><strong>Zoom Meeting Link: </strong><a href="${zoomLink}" target="_blank">Join the Meeting</a></p>
        <p><strong>Agenda:</strong></p>
        <ul>${formattedAgenda}</ul>
        <p><a href="${calendarLink}">📅 Click here to add this event to Google Calendar</a></p>
        ${
          conflictDetails
            ? `<p><strong>Conflict detected!</strong></p>
               <p><strong>Conflicting Meeting Details:</strong></p>
               <ul>
                 <li><strong>Department:</strong> ${conflictDetails.department}</li>
                 <li><strong>Date:</strong> ${conflictDetails.date}</li>
                 <li><strong>Time:</strong> ${conflictDetails.startTime} - ${conflictDetails.endTime}</li>
                 <li><strong>Subject:</strong> ${conflictDetails.subject}</li>
                 <li><strong>Agenda:</strong> ${conflictDetails.agenda}</li>
               </ul>`
            : ""
        }
      `,
      attachments: files.map((file) => ({
        filename: file.filename,
        path: file.path,
      })),
    };

    await transporter.sendMail(mailOptions);

    res.status(200).json({
      message: conflictDetails ? "Conflict detected! Email sent successfully." : "Email sent successfully!",
      conflictDetails,
      calendarLink,
    });
  } catch (error) {
    console.error("Error sending email:", error);
    res.status(500).json({ error: "Error sending email" });
  }
};

module.exports = { sendEmail };
