// src/routes/index.js
const express = require("express");
const router = express.Router();
const path = require("path");

const robots = require("../controllers/robotsController");
const csrfToken = require("../middleware/csrfToken");
const errorPage = require("../controllers/errorPageController");
const admin = require("./admin");
const tags = require("./tags");
const resume = require("./resume");

const contact = require("./contact");
const sitemap = require("./sitemap");
const { blogPost, blogIndex } = require("../controllers/blogControllers");
const pages = require("./pages");
const projects = require("./projects");
const docs = require("./docs");
const rssFeedController = require("../controllers/rssFeedController");
const HttpError = require("../utils/HttpError");

const stack = require("../controllers/techkStackController");
const { meta } = require("../config/loader");

const favicon = require("serve-favicon");
const faviconsPath = path.join(__dirname, "..", "..", "public", "favicons");
const faviconFile = path.resolve(faviconsPath, "favicon.ico");

const hexascriptDocs = require("../../content/docs/hexascript/src/script.js");

router.head("/health", (req, res) => {
  res.sendStatus(200);
});

const { winstonLogger } = require("../utils/logging");
//winstonLogger.warn("Log?", hexascriptDocs());
router.use("/hexa-docs", hexascriptDocs);
// router.use(hexascriptDocs);
// hexascriptDocs;
winstonLogger.info(hexascriptDocs);

router.get("/error/:code", errorPage); // Landing page after error is logged

router.get("/error", errorPage); // Landing page after error is logged

router.use(admin);

router.use(
  "/static",
  express.static("public", {
    dotfiles: "deny",
    index: false,
    extensions: false,
    fallthrough: false,
    setHeaders: (res) => {
      if (meta.node_env == "production") {
        // Doesn't expire
        res.set("Cache-Control", "public, max-age=31536000, immutable");
      } else {
        // Live long enough for the page to load
        res.set("Cache-Control", "public, max-age=30, must-revalidate");
      }
    },
  }),
);

router.use(
  "/media",
  express.static("content/images", {
    dotfiles: "deny",
    index: false,
    extensions: false,
    fallthrough: false,
    setHeaders: (res) => {
      if (meta.node_env == "production") {
        // Cache for 1 day, allow revalidation
        res.set("Cache-Control", "public, max-age=86400, must-revalidate");
      } else {
        // Minimal caching for dev
        res.set("Cache-Control", "public, max-age=30, must-revalidate");
      }
    },
  }),
);

router.use("/favicons", express.static(faviconsPath));
router.use(favicon(faviconFile));

router.use(contact, csrfToken);
router.use("/resume", resume);
router.use(sitemap);
router.use(pages);
router.use(projects);
router.use(tags);
router.use("/docs", docs);

router.get("/blog/:year/:month/:name", blogPost);
router.get("/blog", blogIndex);

router.get("/stack", stack);

router.get("/robots.txt", robots);
router.get("/rss-feed.xml", rssFeedController);

router.get("/", (req, res) => {
  res.customRedirect("/blog", 301);
});

router.use((req, res, next) => {
  req.log.warn(req.url);
  next(new HttpError("Page not found", 404));
});

module.exports = router;
