import express, { type Express } from "express";
import path from "node:path";
import { fileURLToPath } from "node:url";

const __dirname = path.dirname(fileURLToPath(import.meta.url));

export function servirEstaticos(app: Express) {
  const distClient = path.resolve(__dirname, "../../dist");

  app.use(express.static(distClient));

  // Fallback do SPA — qualquer rota não-API cai no index.html
  app.get("*", (req, res, next) => {
    if (req.path.startsWith("/api") || req.path.startsWith("/uploads")) return next();
    res.sendFile(path.join(distClient, "index.html"));
  });
}
