import express from "express";
import path from "path";
import { fileURLToPath } from "url";
import authRouter from "./routes/auth.js";
import gameRouter from "./routes/game.js";

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const publicDir = path.join(__dirname, "..", "public");

const app = express();
app.use(express.json());
app.use(express.static(publicDir));

app.use("/api/auth", authRouter);
app.use("/api", gameRouter);

// Catch-all: serve index.html for non-API routes (SPA fallback)
app.get("*", (req, res) => {
  if (req.path.startsWith("/api")) {
    return res.status(404).json({ error: "not_found" });
  }
  res.sendFile(path.join(publicDir, "index.html"));
});

export default app;