import { createZoomMeeting } from "../services/zoomMeeting.service.js";
import { successResponse, errorResponse } from "../utils/response.js";
import { HTTP_STATUS } from "../constants/index.js";
import logger from "../utils/logger.js";

export const create = async (req, res, next) => {
  try {
    const {
      topic,
      startTimeIso,
      durationMinutes,
      attendees = [],
      hostUserId,
      fullName,
      email,
      meetingType,
      preferredMeetingTime,
      projectDescription,
      additionalNotes,
    } = req.body;

    // Validate required fields
    if (!fullName || !email || !meetingType || !preferredMeetingTime) {
      return res.status(400).json({
        ok: false,
        error: "Missing required fields: fullName, email, meetingType, preferredMeetingTime",
      });
    }

    const meeting = await createZoomMeeting({
      topic,
      startTimeIso,
      durationMinutes,
      hostUserId,
      fullName,
      email,
      meetingType,
      preferredMeetingTime,
      projectDescription,
      additionalNotes,
      attendees,
    });

    logger.info(`Zoom meeting created: ${meeting.topic}`);

    // Return response in the format expected by the frontend
    return res.status(200).json({
      ok: true,
      meeting,
      message: "Meeting created. Admin and user notified via email.",
    });
  } catch (err) {
    logger.error("Error creating Zoom meeting:", err);
    
    // Return error in the format expected by the frontend
    return res.status(500).json({
      ok: false,
      error: err.response?.data || err.message || "Failed to create meeting",
    });
  }
};

