const express = require("express");
const path = require("path");
const { staticDir } = require("./config/env");
const apiRoutes = require("./routes");

function createApp() {
  const app = express();
  app.use(express.json());
  app.use("/api", apiRoutes);
  app.get("/health", (_req, res) => res.redirect(307, "/api/health"));

  app.use(express.static(staticDir));
  app.get("/{*path}", (req, res, next) => {
    if (req.path.startsWith("/api/")) {
      return next();
    }
    return res.sendFile(path.join(staticDir, "index.html"));
  });

  return app;
}

module.exports = { createApp };
