import { z } from "zod";

const zoomMeetingBase = z.object({
  topic: z.string().min(1, "Topic is required").max(200, "Topic is too long").optional(),
  startTimeIso: z.string().datetime().optional(),
  durationMinutes: z.number().int().positive().max(480).optional(), // Max 8 hours
  hostUserId: z.string().trim().optional(),
  fullName: z.string().min(1, "Full name is required").max(200, "Name is too long"),
  email: z.string().email("Invalid email address").toLowerCase(),
  meetingType: z.string().min(1, "Meeting type is required"),
  preferredMeetingTime: z.string().min(1, "Preferred meeting time is required"),
  projectDescription: z.string().optional(),
  additionalNotes: z.string().optional(),
  attendees: z.array(z.object({
    name: z.string().optional(),
    email: z.string().email().optional(),
  })).optional(),
  status: z.enum(["Scheduled", "Completed", "Cancelled", "In Progress"]).optional(),
});

const createZoomMeetingSchema = zoomMeetingBase;
const updateZoomMeetingSchema = zoomMeetingBase.partial().extend({
  zoomMeetingId: z.string().optional(),
  joinUrl: z.string().url().optional(),
  startUrl: z.string().url().optional(),
  meetingNumber: z.string().optional(),
  password: z.string().optional(),
});

function normalizeZoomMeetingPayload(payload) {
  if (!payload || typeof payload !== "object") {
    return {};
  }
  
  const normalized = { ...payload };
  
  // Normalize email to lowercase
  if (normalized.email) {
    normalized.email = normalized.email.toLowerCase().trim();
  }
  
  // Normalize attendees emails
  if (Array.isArray(normalized.attendees)) {
    normalized.attendees = normalized.attendees.map(attendee => ({
      ...attendee,
      email: attendee.email ? attendee.email.toLowerCase().trim() : undefined,
    }));
  }
  
  // Ensure duration is a number
  if (normalized.durationMinutes !== undefined) {
    normalized.durationMinutes = Number(normalized.durationMinutes);
  }
  
  return normalized;
}

export {
  createZoomMeetingSchema,
  updateZoomMeetingSchema,
  normalizeZoomMeetingPayload,
};

