// const AirTicketingService = require("../services/AirTicketing");

// const handleAirTicketing = async (req, res) => {
//   try {
//     console.log("✅ UPDATED CONTROLLER HIT");
//     console.log("👉 Incoming Body:", req.body);

//     const { pnr, pricingKeys, fop, traceId } = req.body;

//     console.log("👉 Extracted:", { pnr, pricingKeys });

//     if (!pnr || !pricingKeys || !Array.isArray(pricingKeys) || pricingKeys.length === 0) {
//       console.log("❌ Validation Failed");
//       return res.status(400).json({
//         success: false,
//         message: "Missing PNR or PricingKeys"
//       });
//     }

//     console.log("✅ Validation Passed");

//     const result = await AirTicketingService.issueTicket(
//       pnr,
//       pricingKeys,
//       { fop, traceId }
//     );

//     return res.status(200).json({
//       success: true,
//       traceId: result.traceId,
//       data: result.data
//     });

//   } catch (error) {
//     console.error(`[CONTROLLER-ERROR] TraceId: ${req.body.traceId} - ${error.message}`);
//     return res.status(500).json({
//       success: false,
//       message: error.message
//     });
//   }
// };





//update code 



const AirTicketingService = require("../services/AirTicketing");

const handleAirTicketing = async (req, res) => {
  try {
    console.log("✅ UPDATED CONTROLLER HIT");
    console.log("👉 Incoming Body:", req.body);

    const { pnr, fop, traceId } = req.body;
    console.log("👉 Extracted:", { pnr, fop, traceId });

    if (!pnr) {
      console.log("❌ Validation Failed - Missing PNR");
      return res.status(400).json({
        success: false,
        message: "Missing PNR"
      });
    }

    if (!traceId) {
      console.log("❌ Validation Failed - Missing traceId");
      return res.status(400).json({
        success: false,
        message: "Missing traceId"
      });
    }

    console.log("✅ Validation Passed");

    const result = await AirTicketingService.issueTicket(
      pnr,
      { fop, traceId }
    );

    return res.status(200).json({
      success: true,
      traceId: result.traceId,
      data: result.data
    });

  } catch (error) {
    console.error(`[CONTROLLER-ERROR] TraceId: ${req.body.traceId} - ${error.message}`);
    return res.status(500).json({
      success: false,
      message: error.message
    });
  }
};

module.exports = { handleAirTicketing };

