const mysql = require("mysql2/promise");

// create the connection to database
const db = mysql.createPool({
    host: 'localhost',
    user: 'bobros_7639236966',
    database: 'hweels',
    password: '1434@Humming',
  });



const airportCode = async (req, res) => {
  const namePrefix = req.query.name?.trim();
  console.log("name prefix:", namePrefix);

  if (namePrefix.length < 3) {
    return res.status(400).json({
      error: 'Please enter at least 3 characters'
    });
  }

  try {
    const [rows] = await db.execute(
      `SELECT a.location_code, a.name
       FROM airports a
       JOIN travelport_cities t
         ON a.A_code = t.city_code
       WHERE t.city_name LIKE CONCAT(?, '%')
          OR a.name LIKE CONCAT(?, '%')`,
      [namePrefix, namePrefix]
    );
    console.log("rows :", rows);
    res.json(rows);
  } catch (error) {
    res.status(500).json({ error: 'Something went wrong' });
  }
};



module.exports= airportCode;
