'use strict';
// Reuse the shared mysql2 promise pool (same module the PNR service uses).
// Confirm this relative depth resolves to busapi/configs/dbConnection from utils/.
const db = require('../../../busapi/configs/dbConnection');

const TABLE = 'iata_bsp_active_airlines';

// Returns Map: IATA(UPPER) -> { ticketingAuthority: boolean, airlineName: string|null }
// Throws on DB failure; the service turns that into a fail-closed response.
const loadAirlineMap = async () => {
  const [rows] = await db.execute(`SELECT iata_code, ticketing_authority, airline_name FROM ${TABLE}`);
  const map = new Map();
  for (const r of rows) {
    if (!r.iata_code) continue;
    map.set(String(r.iata_code).trim().toUpperCase(), {
      ticketingAuthority: String(r.ticketing_authority ?? '').trim().toUpperCase() === 'YES',
      airlineName: r.airline_name || null,
    });
  }
  return map;
};

module.exports = { loadAirlineMap };