import { Request, Response } from "express";
import { BookingService } from "../services/bookingService";
import { logger } from "../utils/logger";

const bookingService = BookingService.getInstance();

/**
 * 모든 예약 조회
 */
export const getBookings = async (req: Request, res: Response): Promise<void> => {
  try {
    const { status, priority } = req.query;

    const result = await bookingService.getAllBookings({
      status: status as string,
      priority: priority as string,
    });

    res.status(200).json({
      success: true,
      data: result.bookings,
      newCount: result.newCount,
    });
  } catch (error) {
    logger.error("❌ 예약 목록 조회 실패:", error);
    res.status(500).json({
      success: false,
      message: "예약 목록 조회에 실패했습니다.",
      error: error instanceof Error ? error.message : String(error),
    });
  }
};

/**
 * 예약 수락
 */
export const acceptBooking = async (req: Request, res: Response): Promise<void> => {
  try {
    const { id } = req.params;
    const booking = await bookingService.acceptBooking(id);

    res.status(200).json({
      success: true,
      data: booking,
      message: "예약이 수락되었습니다.",
    });
  } catch (error) {
    logger.error("❌ 예약 수락 실패:", error);
    res.status(500).json({
      success: false,
      message: "예약 수락에 실패했습니다.",
      error: error instanceof Error ? error.message : String(error),
    });
  }
};

/**
 * 예약 거절
 */
export const rejectBooking = async (req: Request, res: Response): Promise<void> => {
  try {
    const { id } = req.params;
    const { reason } = req.body;
    const booking = await bookingService.rejectBooking(id, reason);

    res.status(200).json({
      success: true,
      data: booking,
      message: "예약이 거절되었습니다.",
    });
  } catch (error) {
    logger.error("❌ 예약 거절 실패:", error);
    res.status(500).json({
      success: false,
      message: "예약 거절에 실패했습니다.",
      error: error instanceof Error ? error.message : String(error),
    });
  }
};

