/* API V2 Exclusive Controller */

const ldb = require("../services/ldb.services");
const find = require("../services/find.services");

async function getReasonCode(req, res, next) {
  try {
    const code = req.params.code;
    if (code === "all") {
      res.json(await ldb.getReasonCodeList());
      next;
    }
    res.json(await ldb.getReasonCode(code));
    next;
  } catch (err) {
    console.error("ERROR", err.message);
    err.status = 500;
    next(err);
  }
}

async function getLocationReference(req, res, next) {
  try {
    const searchType = req.params.searchType;
    const id = req.params.id;
    switch (searchType) {
      case "name":
        res.json(await find.name(id));
        break;
      case "crs": // Same as 3alpha
      case "3alpha":
        res.json(await find.crs(id));
        break;
      case "nlc":
        res.json(await find.nlc(id));
        break;
      case "tiploc":
        res.json(await find.tiploc(id));
        break;
      case "stanox":
        res.json(await find.stanox(id));
        break;
    }
  } catch (err) {
    console.error("ERROR", err.message);
    err.status = 500;
    next(err);
  }
}

module.exports = {
  getReasonCode,
  getLocationReference,
};
