import {
  createZoomMeeting,
  listZoomMeetings,
  getZoomMeetingById,
  getZoomMeetingByZoomId,
  updateZoomMeeting,
  deleteZoomMeeting,
  cancelZoomMeeting,
} from "../services/zoomMeeting.service.js";
import {
  createZoomMeetingSchema,
  updateZoomMeetingSchema,
  normalizeZoomMeetingPayload,
} from "../models/zoomMeeting.schema.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 normalized = normalizeZoomMeetingPayload(req.body);
    const parsed = createZoomMeetingSchema.safeParse(normalized);
    
    if (!parsed.success) {
      return errorResponse(
        res,
        "Validation failed",
        HTTP_STATUS.BAD_REQUEST,
        parsed.error.flatten()
      );
    }

    const meeting = await createZoomMeeting(parsed.data);

    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",
    });
  }
};

export const list = async (req, res, next) => {
  try {
    const filters = {
      status: req.query.status,
      email: req.query.email,
      startDate: req.query.startDate,
      endDate: req.query.endDate,
    };
    const items = await listZoomMeetings(filters);
    return successResponse(res, items, HTTP_STATUS.OK);
  } catch (err) {
    logger.error("Error listing zoom meetings:", err);
    next(err);
  }
};

export const getById = async (req, res, next) => {
  try {
    const { id } = req.params;
    if (!id) {
      return errorResponse(res, "Meeting ID is required", HTTP_STATUS.BAD_REQUEST);
    }
    const item = await getZoomMeetingById(id);
    if (!item) {
      return errorResponse(res, "Meeting not found", HTTP_STATUS.NOT_FOUND);
    }
    return successResponse(res, item, HTTP_STATUS.OK);
  } catch (err) {
    logger.error("Error getting zoom meeting by ID:", err);
    next(err);
  }
};

export const getByZoomId = async (req, res, next) => {
  try {
    const { zoomId } = req.params;
    if (!zoomId) {
      return errorResponse(res, "Zoom Meeting ID is required", HTTP_STATUS.BAD_REQUEST);
    }
    const item = await getZoomMeetingByZoomId(zoomId);
    if (!item) {
      return errorResponse(res, "Meeting not found", HTTP_STATUS.NOT_FOUND);
    }
    return successResponse(res, item, HTTP_STATUS.OK);
  } catch (err) {
    logger.error("Error getting zoom meeting by Zoom ID:", err);
    next(err);
  }
};

export const update = async (req, res, next) => {
  try {
    const { id } = req.params;
    if (!id) {
      return errorResponse(res, "Meeting ID is required", HTTP_STATUS.BAD_REQUEST);
    }
    const normalized = normalizeZoomMeetingPayload(req.body);
    const parsed = updateZoomMeetingSchema.safeParse(normalized);
    
    if (!parsed.success) {
      return errorResponse(
        res,
        "Validation failed",
        HTTP_STATUS.BAD_REQUEST,
        parsed.error.flatten()
      );
    }
    
    const updated = await updateZoomMeeting(id, parsed.data);
    if (!updated) {
      return errorResponse(res, "Meeting not found", HTTP_STATUS.NOT_FOUND);
    }
    logger.info(`Zoom meeting updated with ID: ${id}`);
    return successResponse(res, updated, HTTP_STATUS.OK);
  } catch (err) {
    logger.error("Error updating zoom meeting:", err);
    next(err);
  }
};

export const remove = async (req, res, next) => {
  try {
    const { id } = req.params;
    if (!id) {
      return errorResponse(res, "Meeting ID is required", HTTP_STATUS.BAD_REQUEST);
    }
    const ok = await deleteZoomMeeting(id);
    if (!ok) {
      return errorResponse(res, "Meeting not found", HTTP_STATUS.NOT_FOUND);
    }
    logger.info(`Zoom meeting deleted with ID: ${id}`);
    return res.status(HTTP_STATUS.NO_CONTENT).send();
  } catch (err) {
    logger.error("Error deleting zoom meeting:", err);
    next(err);
  }
};

export const cancel = async (req, res, next) => {
  try {
    const { id } = req.params;
    if (!id) {
      return errorResponse(res, "Meeting ID is required", HTTP_STATUS.BAD_REQUEST);
    }
    const deleteFromZoom = req.query.deleteFromZoom === "true";
    const cancelled = await cancelZoomMeeting(id, deleteFromZoom);
    if (!cancelled) {
      return errorResponse(res, "Meeting not found", HTTP_STATUS.NOT_FOUND);
    }
    logger.info(`Zoom meeting cancelled with ID: ${id}`);
    return successResponse(res, cancelled, HTTP_STATUS.OK);
  } catch (err) {
    logger.error("Error cancelling zoom meeting:", err);
    next(err);
  }
};

