import { createReadStream, existsSync, statSync } from "node:fs";
import { createServer } from "node:http";
import { dirname, extname, join, normalize } from "node:path";
import { fileURLToPath } from "node:url";

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const root = normalize(join(__dirname, "../../userfront/build/web"));

if (!existsSync(root) || !statSync(root).isDirectory()) {
  console.error(
    "[userfront-e2e] userfront/build/web not found. Run: cd userfront && flutter build web --wasm --release",
  );
  process.exit(1);
}

const port = Number.parseInt(process.env.PORT ?? "4173", 10);

const contentTypes = {
  ".css": "text/css; charset=utf-8",
  ".html": "text/html; charset=utf-8",
  ".ico": "image/x-icon",
  ".js": "application/javascript; charset=utf-8",
  ".json": "application/json; charset=utf-8",
  ".mjs": "application/javascript; charset=utf-8",
  ".png": "image/png",
  ".svg": "image/svg+xml; charset=utf-8",
  ".txt": "text/plain; charset=utf-8",
  ".wasm": "application/wasm",
  ".webmanifest": "application/manifest+json; charset=utf-8",
  ".woff": "font/woff",
  ".woff2": "font/woff2",
};

const server = createServer((req, res) => {
  const url = new URL(req.url ?? "/", "http://localhost");
  const pathname = decodeURIComponent(url.pathname);
  if (pathname === "/" && url.search === "") {
    res.statusCode = 302;
    res.setHeader("Location", "/ko/signin");
    res.setHeader("Cache-Control", "no-cache, max-age=0, must-revalidate");
    res.end();
    return;
  }
  const relative = pathname === "/" ? "/index.html" : pathname;
  const candidate = normalize(join(root, relative));

  if (!candidate.startsWith(root)) {
    res.statusCode = 403;
    res.end("Forbidden");
    return;
  }

  let filePath = candidate;
  let servesAppShellFallback = false;

  if (!existsSync(filePath) || statSync(filePath).isDirectory()) {
    if (extname(pathname)) {
      res.statusCode = 404;
      res.setHeader("Cache-Control", "no-cache, max-age=0, must-revalidate");
      res.end("Not Found");
      return;
    }

    // Flutter web 라우팅 경로(`/ko`, `/ko/signin`)도 index.html로 fallback 처리
    filePath = join(root, "index.html");
    servesAppShellFallback = true;
  }

  const acceptsBrotli = /\bbr\b/.test(req.headers["accept-encoding"] ?? "");
  const brotliPath = `${filePath}.br`;
  const servedPath =
    acceptsBrotli && existsSync(brotliPath) ? brotliPath : filePath;
  const ext = extname(filePath);
  const contentType = contentTypes[ext] ?? "application/octet-stream";
  const stats = statSync(servedPath);
  const etag = `"${stats.size.toString(16)}-${Math.trunc(stats.mtimeMs).toString(16)}"`;
  const cacheControl = cacheControlFor(
    pathname,
    filePath,
    servesAppShellFallback,
  );

  res.setHeader("Content-Type", contentType);
  res.setHeader("ETag", etag);
  res.setHeader("Last-Modified", stats.mtime.toUTCString());
  res.setHeader("Cache-Control", cacheControl);
  res.setHeader("Vary", "Accept-Encoding");

  // Flutter WASM requires SharedArrayBuffer which needs these COOP/COEP headers
  // to be cross-origin isolated in most modern browsers (WebKit, Firefox, etc.)
  res.setHeader("Cross-Origin-Opener-Policy", "same-origin");
  res.setHeader("Cross-Origin-Embedder-Policy", "require-corp");

  if (servedPath === brotliPath) {
    res.setHeader("Content-Encoding", "br");
  }

  if (req.headers["if-none-match"] === etag) {
    res.statusCode = 304;
    res.end();
    return;
  }

  createReadStream(servedPath)
    .on("error", () => {
      res.statusCode = 500;
      res.end("Internal Server Error");
    })
    .pipe(res);
});

function cacheControlFor(pathname, filePath, servesAppShellFallback) {
  const basename = filePath.split("/").pop() ?? "";

  if (
    servesAppShellFallback ||
    basename === "index.html" ||
    basename === "flutter_bootstrap.js" ||
    basename === "flutter_service_worker.js" ||
    basename === "version.json" ||
    basename === "manifest.json"
  ) {
    return "no-cache, max-age=0, must-revalidate";
  }

  if (/^\/canvaskit\/.*\.(?:js|wasm)$/i.test(pathname)) {
    return "public, max-age=31536000, immutable";
  }

  if (/^\/main\.dart\.[0-9a-f]{12}\.(?:js|mjs|wasm)$/i.test(pathname)) {
    return "public, max-age=31536000, immutable";
  }

  if (/\.(?:png|ico|svg|webp|woff|woff2)$/i.test(pathname)) {
    return "public, max-age=31536000, immutable";
  }

  if (/\.(?:js|css|json|mjs|wasm)$/i.test(pathname)) {
    return "no-cache, max-age=0, must-revalidate";
  }

  return "no-cache, max-age=0, must-revalidate";
}

server.listen(port, "127.0.0.1", () => {
  console.log(`[userfront-e2e] serving ${root} at http://127.0.0.1:${port}`);
});
