// Parse and return a find request

const db = require("../services/dbAccess.services");
const san = require("../utils/sanitizer.utils");

import { logger } from "../utils/logger.utils";

// DB Query: query(collection, query)

// Define collection as all queries are for the "corpus" collection.
const col = "corpus";

async function name(id) {
  logger.debug(`findServices.name: Finding station name: ${id}`);
  var name = san.cleanApiEndpointTxt(id.toUpperCase());
  let query = { NLCDESC: name };
  //var data = await db.query(col,query)
  return await db.query(col, query);
}

async function crs(id) {
  logger.debug(`findServices.crs: Finding crs: ${id}`);
  var crs = san.cleanApiEndpointTxt(id.toUpperCase());
  let query = { "3ALPHA": crs };
  //var data = await db.query(col,query)
  return await db.query(col, query);
}

async function nlc(id) {
  logger.debug(`findServices.nlc: Finding nlc: ${id}`);
  var nlc = san.cleanApiEndpointNum(id);
  let query = { NLC: parseInt(nlc) };
  logger.trace(`findServices.nlc: NLC Converted to int: ${query}`);
  //var data = await db.query(col,query)
  return await db.query(col, query);
}

async function tiploc(id) {
  logger.debug(`findServices.tiploc: Finding tiploc: ${id}`);
  var tiploc = san.cleanApiEndpointTxt(id.toUpperCase());
  let query = { TIPLOC: tiploc };
  //var data = await db.query(col,query)
  return await db.query(col, query);
}

async function stanox(id) {
  logger.debug(`findServices.stanox: Finding stanox: ${id}`);
  var stanox = san.cleanApiEndpointNum(id);
  let query = { STANOX: String(stanox) };
  //var data = await db.query(col,query)
  return await db.query(col, query);
}

module.exports = {
  name,
  crs,
  nlc,
  tiploc,
  stanox,
};
