import Event from "../../../../models/event.js";
import Member from "../../../../models/member.js";
import { connectDB } from "../../../../utils/mongoose.js";
import {
  calculateTicketPrice,
  reserveTicket,
} from "../../../../utils/tickets.js";
import mongoose from "mongoose";

/**
 * POST /api/events/[id]/tickets/reserve
 * Temporarily reserve a ticket during checkout process
 * Body: { email }
 */
export default defineEventHandler(async (event) => {
  try {
    await connectDB();
    const identifier = getRouterParam(event, "id");
    const body = await validateBody(event, ticketReserveSchema);

    if (!identifier) {
      throw createError({
        statusCode: 400,
        statusMessage: "Event identifier is required",
      });
    }

    // Fetch the event
    let eventData;
    if (mongoose.Types.ObjectId.isValid(identifier)) {
      eventData = await Event.findById(identifier);
    }
    if (!eventData) {
      eventData = await Event.findOne({ slug: identifier });
    }

    if (!eventData) {
      throw createError({
        statusCode: 404,
        statusMessage: "Event not found",
      });
    }

    // Check if user is a member
    const member = await Member.findOne({ email: body.email.toLowerCase() });

    // Calculate ticket type
    const ticketInfo = calculateTicketPrice(eventData, member);

    if (!ticketInfo) {
      throw createError({
        statusCode: 400,
        statusMessage: "No tickets available for your membership status",
      });
    }

    // Reserve the ticket
    const reservation = await reserveTicket(eventData, ticketInfo.ticketType);

    if (!reservation.success) {
      throw createError({
        statusCode: 400,
        statusMessage: reservation.reason || "Failed to reserve ticket",
      });
    }

    return {
      success: true,
      reservation: {
        id: reservation.reservationId,
        expiresAt: reservation.expiresAt,
        ticketType: ticketInfo.ticketType,
      },
    };
  } catch (error) {
    console.error("Error reserving ticket:", error);

    if (error.statusCode) {
      throw error;
    }

    throw createError({
      statusCode: 500,
      statusMessage: "Failed to reserve ticket",
    });
  }
});
