const { getPriceAnalysis } = require("../models/priceAnalysisModel");

async function priceAnalysisHandler(req, res) {
  try {
    const { originIataCode, destinationIataCode, departureDate, currencyCode, oneWay } = req.query;

    if (!originIataCode || !destinationIataCode || !departureDate) {
      return res.status(400).json({
        error: "Missing required parameters: originIataCode, destinationIataCode, and departureDate",
      });
    }

    const priceData = await getPriceAnalysis({
      originIataCode,
      destinationIataCode,
      departureDate,
      currencyCode: currencyCode || 'INR',
      oneWay: oneWay === 'true',
    });

    res.json(priceData);
  } catch (error) {
    console.error("Error in priceAnalysisHandler:", error.message);
    res.status(500).json({
      error: "Failed to retrieve price analysis",
      details: error.message,
    });
  }
}

module.exports = { priceAnalysisHandler };
