// controllers/flightStatusController.js

const { getFlightStatus } = require('../models/flightScheduleModel');


/**
 * Controller for GET /v2/schedule/flights
 *
 * Expects these query parameters:
 *   - carrierCode             (string, required)
 *   - flightNumber            (string, required)
 *   - scheduledDepartureDate  (string, required, "YYYY-MM-DD")
 */
async function flightStatusControler(req, res) {
  try {
    const { carrierCode, flightNumber, scheduledDepartureDate } = req.query;
    //console.log(req.query);

    // Validate required params:
    if (!carrierCode || !flightNumber || !scheduledDepartureDate) {
      return res.status(400).json({
        error:
          'Missing required query parameters. Required: carrierCode, flightNumber, scheduledDepartureDate.',
      });
    }

    // Call the model function
    const statusData = await getFlightStatus(
      carrierCode,
      flightNumber,
      scheduledDepartureDate
    );

    return res.status(200).json({
      success: true,
      data: statusData,
    });
  } catch (error) {
    console.error('Error fetching flight schedule:', error);

    return res.status(500).json({
      success: false,
      error: error.message || 'An unexpected error occurred.',
    });
  }
}

module.exports = {
  flightStatusControler,
};
