const reg = require("../services/registration.services");

async function register(req, res, next) {
  try {
    let response = await reg.regUser(req.body);
    res.status(response.status).json(response);
  } catch (err) {
    console.error("Controller Error", err.message);
    next(err);
  }
}

async function request(req, res, next) {
  try {
    let response = await reg.createRegKey(req.body);
    res.status(response.status).json(response);
  } catch (err) {
    console.error(err);
    next(err);
  }
}

async function getUser(req, res, next) {
  try {
    let uuid = req.params.uuid;
    let data = await reg.getUser(uuid);
    if (data.status) {
      res.status(data.status).json(data);
    } else {
      res.json(data);
    }
  } catch (err) {
    console.error(err);
    next(err);
  }
}

async function checkAuth(req, res, next) {
  if (!req.isAuthed) {
    res.status(401).body("Not Authorised");
  } else {
    res.status(200).body("Authorised Successfully");
  }
  next();
}

module.exports = {
  register,
  request,
  getUser,
  checkAuth,
};
