const { getHotelDetails } = require("../services/newHotelDetailsService");

/**
 * Controller: Hotel Details
 */
/*exports.hotelDetails = async (req, res) => {
  console.log("==================================================");
  console.log("CONTROLLER: HotelDetails - START");

  try {
    const requestBody = req.body;

    if (!requestBody.hotelProperty) {
      return res.status(400).json({
        success: false,
        message: "hotelProperty is required",
      });
    }

    const response = await getHotelDetails(requestBody);

    console.log("CONTROLLER: HotelDetails - SUCCESS");

    res.status(200).json({
      success: true,
      message: "Hotel details fetched successfully",
      data: response,
    });

  } catch (error) {
    console.error("❌ CONTROLLER ERROR: HotelDetails");

    res.status(500).json({
      success: false,
      message: "Hotel details fetch failed",
      error: error.message,
    });
  }

  console.log("CONTROLLER: HotelDetails - END");
  console.log("==================================================");
};*/

//const { getHotelDetails } = require("../services/hotelDetailsService");

exports.hotelsDetails = async (req, res) => {
  console.log("==================================================");
  console.log("CONTROLLER: HotelDetails - START");

  try {
    const { hotelProperty, detailsModifiers } = req.body;

    // ✅ Validate required objects
    if (!hotelProperty) {
      return res.status(400).json({
        success: false,
        message: "hotelProperty is required",
      });
    }

    if (!detailsModifiers) {
      return res.status(400).json({
        success: false,
        message: "detailsModifiers is required",
      });
    }

    // ✅ Validate required fields inside hotelProperty
    const requiredHotelFields = [
      "hotelChain", "hotelCode", "hotelLocation",
      "name", "vendorLocationKey"
    ];

    for (const field of requiredHotelFields) {
      if (!hotelProperty[field]) {
        return res.status(400).json({
          success: false,
          message: `hotelProperty.${field} is required`,
        });
      }
    }

    // ✅ Validate required fields inside detailsModifiers
    const requiredModifierFields = ["checkin", "checkout"];

    for (const field of requiredModifierFields) {
      if (!detailsModifiers[field]) {
        return res.status(400).json({
          success: false,
          message: `detailsModifiers.${field} is required`,
        });
      }
    }

    // ✅ Flatten nested body → flat structure the service expects
    const servicePayload = {

      // From hotelProperty — required
      hotelChain:          hotelProperty.hotelChain,
      hotelCode:           hotelProperty.hotelCode,
      hotelLocation:       hotelProperty.hotelLocation,
      hotelName:           hotelProperty.name,            // "name" → "hotelName"
      vendorLocationKey:   hotelProperty.vendorLocationKey,

      // From hotelProperty — optional
      reserveRequirement:  hotelProperty.reserveRequirement,
      participationLevel:  hotelProperty.participationLevel,
      availability:        hotelProperty.availability,
      address:             hotelProperty.address,
      distance:            hotelProperty.distance,
      hotelRating:         hotelProperty.hotelRating,
      hotelTransportation: hotelProperty.hotelTransportation,

      // From detailsModifiers — required
      checkinDate:         detailsModifiers.checkin,      // "checkin"  → "checkinDate"
      checkoutDate:        detailsModifiers.checkout,     // "checkout" → "checkoutDate"

      // From detailsModifiers — optional
      numberOfAdults:      detailsModifiers.numberOfAdults,
      numberOfRooms:       detailsModifiers.numberOfRooms,
      rateRuleDetail:      detailsModifiers.rateRuleDetail,
      children:            detailsModifiers.children,
      rateCategory:        detailsModifiers.rateCategory,
      amenities:           detailsModifiers.amenities,
      corporateDiscountId: detailsModifiers.corporateDiscountId,
      promoCode:           detailsModifiers.promoCode,
      minRate:             detailsModifiers.minRate,
      maxRate:             detailsModifiers.maxRate,
      currency:            detailsModifiers.currency,
      roomType:            detailsModifiers.roomType,
      bedType:             detailsModifiers.bedType,
    };

    console.log("➡️ Service Payload:");
    console.log(JSON.stringify(servicePayload, null, 2));

    const response = await getHotelDetails(servicePayload);

    console.log("✅ CONTROLLER: HotelDetails - SUCCESS");

    res.status(200).json({
      success: true,
      message: "Hotel details fetched successfully",
      data: response,
    });

  } catch (error) {
    console.error("❌ CONTROLLER ERROR: HotelDetails");
    console.error(error.message);

    res.status(500).json({
      success: false,
      message: "Hotel details fetch failed",
      error: error.message,
    });
  }

  console.log("CONTROLLER: HotelDetails - END");
  console.log("==================================================");
};