// // const axios = require("axios");
// // const https = require("https");

// // const httpsAgent = new https.Agent({
// //   keepAlive: true,
// //   maxSockets: 100,
// //   maxFreeSockets: 10,
// //   keepAliveMsecs: 30000
// // });

// // const travelportClient = axios.create({
// //   httpsAgent,
// //   timeout: 60000,
// //   headers: {
// //     "Content-Type": "text/xml;charset=UTF-8",
// //     Connection: "Keep-Alive",
// //      "Accept-Encoding": "gzip,deflate"
// //   },
// //   auth: {
// //     username: process.env.TRAVELPORT_USERNAME,
// //     password: process.env.TRAVELPORT_PASSWORD
// //   }
// // });

// // module.exports = travelportClient;
// const axios = require("axios");
// const https = require("https");
// const http = require("http");

// const httpsAgent = new https.Agent({
//   keepAlive: true,
//   maxSockets: 100,
//   maxFreeSockets: 10,
//   keepAliveMsecs: 30000,
//   scheduling: 'lifo',
//   rejectUnauthorized: process.env.NODE_ENV === 'production',
//   socketReceiveBufferSize: 512 * 1024 // ✅ 500K buffer
// });

// const travelportClient = axios.create({
//   httpsAgent,
//   timeout: 60000,
//   headers: {
//     "Content-Type": "text/xml;charset=UTF-8",
//     "Connection": "Keep-Alive",
//     "Accept-Encoding": "gzip, deflate",
//     "Expect": "", // ✅ Prevent 100-continue
//   },
//   auth: {
//     username: process.env.TRAVELPORT_USERNAME,
//     password: process.env.TRAVELPORT_PASSWORD
//   },
//   maxRedirects: 0,
//   decompress: true, // ✅ Auto-decompress gzip
//   validateStatus: (status) => status < 500 // Don't throw on 4xx/5xx
// });

// // Request interceptor to log and track
// travelportClient.interceptors.request.use((config) => {
//   config.metadata = { startTime: Date.now() };
//   return config;
// });

// // Response interceptor to log timing
// travelportClient.interceptors.response.use(
//   (response) => {
//     response.duration = Date.now() - response.config.metadata.startTime;
//     return response;
//   },
//   (error) => {
//     if (error.config) {
//       error.duration = Date.now() - error.config.metadata.startTime;
//     }
//     return Promise.reject(error);
//   }
// );





// const axios = require("axios");
// const https = require("https");

// const httpsAgent = new https.Agent({
//   keepAlive: true,
//   maxSockets: 100,
//   maxFreeSockets: 10,
//   keepAliveMsecs: 30000
// });

// const travelportClient = axios.create({
//   httpsAgent,
//   timeout: 60000,
//   headers: {
//     "Content-Type": "text/xml;charset=UTF-8",
//     Connection: "Keep-Alive",
//      "Accept-Encoding": "gzip,deflate"
//   },
//   auth: {
//     username: process.env.TRAVELPORT_USERNAME,
//     password: process.env.TRAVELPORT_PASSWORD
//   }
// });

// module.exports = travelportClient;
const axios = require("axios");
const https = require("https");
const http = require("http");

const httpsAgent = new https.Agent({
  keepAlive: true,
  maxSockets: 100,
  maxFreeSockets: 10,
  keepAliveMsecs: 30000,
  scheduling: 'lifo',
  rejectUnauthorized: process.env.NODE_ENV === 'production',
  socketReceiveBufferSize: 512 * 1024 // ✅ 500K buffer
});

const travelportClient = axios.create({
  httpsAgent,
  timeout: 60000,
  headers: {
    "Content-Type": "text/xml;charset=UTF-8",
    "Connection": "Keep-Alive",
    "Accept-Encoding": "gzip, deflate",
    "Expect": "", // ✅ Prevent 100-continue
  },
  auth: {
    username: process.env.TRAVELPORT_USERNAME,
    password: process.env.TRAVELPORT_PASSWORD
  },
  maxRedirects: 0,
  decompress: true, // ✅ Auto-decompress gzip
  validateStatus: (status) => status < 500 // Don't throw on 4xx/5xx
});

// Request interceptor to log and track
travelportClient.interceptors.request.use((config) => {
  config.metadata = { startTime: Date.now() };
  return config;
});

// Response interceptor to log timing
travelportClient.interceptors.response.use(
  (response) => {
    response.duration = Date.now() - response.config.metadata.startTime;
    return response;
  },
  (error) => {
    if (error.config) {
      error.duration = Date.now() - error.config.metadata.startTime;
    }
    return Promise.reject(error);
  }
);

module.exports = travelportClient;

// module.exports = travelportClient;