const { getFlightChoicePrediction } = require('../models/flightChoicePredictionModel');
const { getFlightOffers } = require('../models/flightOfferSearchModel');
const amadeus = require('../config/amadeusClient');

async function flightChoicePredictionHandler(req, res) {


  try {
    console.log("request recived");
    const {
      originLocationCode,
      destinationLocationCode,
      departureDate,
      returnDate,
      adults,
      max,
      currencyCode = "INR",
      travelTime, // "asc" or "desc"
      sortBy // "earlyDeparture" | "lateDeparture" | "earlyArrival" | "lateArrival"
    } = req.query;
    console.log(req.query);
    
    
    
    // Validate required parameters
    const missingParams = [];
    if (!originLocationCode) missingParams.push('originLocationCode');
    if (!destinationLocationCode) missingParams.push('destinationLocationCode');
    if (!departureDate) missingParams.push('departureDate');
    if (!adults) missingParams.push('adults');

    if (missingParams.length > 0) {
      return res.status(400).json({
        error: `Missing required query parameter(s): ${missingParams.join(', ')}`
      });
    }

    const adultsNum = parseInt(adults, 10);
    if (Number.isNaN(adultsNum) || adultsNum < 1) {
      return res.status(400).json({
        error: `'adults' must be a positive integer. Received: ${adults}`
      });
    }

    let maxNum;
    if (typeof max !== 'undefined') {
      maxNum = parseInt(max, 10);
      if (Number.isNaN(maxNum) || maxNum < 1) {
        return res.status(400).json({
          error: `If provided, 'max' must be a positive integer. Received: ${max}`
        });
      }
    }

    const returnDateParam = returnDate || null;

    // Fetch offers from model
    // const flightOffersResponse = await getFlightOffers(
    //   originLocationCode,
    //   destinationLocationCode,
    //   departureDate,
    //   returnDateParam,
    //   adultsNum,
    //   maxNum,
    //   currencyCode
    // );

    const flightOffersResponse = await amadeus.shopping.flightOffersSearch.get({
      originLocationCode: "HYD",
      destinationLocationCode: "DEL",
      departureDate: "2025-11-01",
      returnDate: "2025-11-11",
      adults: "1",
    });
    return res.json(flightOffersResponse);
    console.log("request 1 complete");
    const response = await amadeus.shopping.flightOffers.prediction.post(
      flightOffersResponse
    );
    console.log(response)
    res.json(response);


    
  } catch (error) {
    console.error('Error getting flight choice prediction:', error);
    res.status(500).json({
      error: error.message || 'Internal Server Error',
    });
  }
}


module.exports = {
  flightChoicePredictionHandler,
};
