const mariadb = require('mariadb/callback');
const MARIADB_HOST = process.env["GENIEACS_MARIADB_HOST"] || "10.100.80.100";
const MARIADB_USER = process.env["GENIEACS_MARIADB_USER"] || "genieacs";
const MARIADB_PASS = process.env["GENIEACS_MARIADB_PASS"] || "DnZHC3XezD7A8keEtaUocqPw";

const conn = mariadb.createConnection({host: MARIADB_HOST, user: MARIADB_USER, password: MARIADB_PASS});

function getWifi(args, callback) {
  const serial = args[0];
  conn.query(`select * from genieacs.wifi where serial = ?`, [serial], (err, res) => {
    if (err) return callback(err);
    callback(null, res.map(r => ({instance: r.instance, ssid: r.ssid, password: r.password, enable: r.enable})));
  });
}

function getVoip(args, callback) {
  const serial = args[0];
  conn.query(`select * from genieacs.wifi where serial = ?`, [serial], (err, res) => {
    if (err) return callback(err);
    callback(null, res.map(r => ({instance: r.instance, username: r.username, password: r.password, enable: r.enable})));
  });
}

exports.getWifi = getWifi;
exports.getVoip = getVoip;

/*
getWifi(["74DA88D3FEC4"], (err, res) => {
  console.dir(err);
  console.dir(res);
});
*/

