"use strict";

const express = require("express");
const glob = require("glob");
const path = require("path");
const { apicache } = require("../middlewares/cache");

const modulesDir = path.resolve(__dirname + "/../modules/");
const globOptions = {
  cwd: modulesDir,
  nocase: true,
  nodir: true,
  absolute: true,
};

module.exports = function () {
  const router = express.Router({ mergeParams: true });

  router.get("/v3/_health", (req, res, next) => {
    res.json({
      code: 200,
      message: "success",
      description: "Welcome, this is the API v3 for the application.",
    });
  });

  // APICACHE

  // route to display cache index
  router.get("/v3/cache/index", (req, res) => {
    res.json(apicache.getIndex());
  });

  // route to clear target group
  router.get("/v3/cache/clear/:target?", (req, res) => {
    res.json(apicache.clear(req.params.target));
  });

  // Resto de rutas

  glob.sync("**/*.routes.js", globOptions).forEach(function (file) {
  router.use("/v3", require(file));
 });

  glob.sync("**/*.routes.v4.js", globOptions).forEach(function (file) {
    router.use("/v4", require(file));
  });

  return router;
};
