import type { NextConfig } from "next";

// Mirrors src/app/robots.ts's canonical-domain check — kept as a local literal (not imported
// from src/lib/site) since next.config.ts is loaded outside the app's normal module resolution.
const isCanonicalDomain = (process.env.NEXT_PUBLIC_SITE_URL || "https://iniyavalreturngifts.com") === "https://iniyavalreturngifts.com";

const nextConfig: NextConfig = {
  poweredByHeader: false,
  // Shared cPanel hosting caps concurrent processes (LVE limits) — cap Next's
  // internal build worker pool to 1 so `next build` doesn't fork multiple
  // workers and hit that ceiling (was crashing with EAGAIN/EPERM on spawn/kill).
  experimental: {
    cpus: 1,
    // Next's default Server Action body limit is 1MB, silently rejecting uploads with a 413
    // before the app's own MAX_IMAGE_SIZE (5MB) checks in products.ts/marketing.ts/etc. ever
    // run. Raise it above that ceiling so those checks are actually what decides.
    serverActions: {
      bodySizeLimit: "6mb",
    },
  },
  // Running with limited parallelism (see cpus:1 above, and RAYON_NUM_THREADS set low at
  // build time for the same reason) makes page generation on this constrained host slower
  // than Next's 60s-per-page default — raise it so slow pages get to finish instead
  // of failing the build.
  staticPageGenerationTimeout: 600,
  images: {
    // This host has no `sharp` installed and an old glibc that's already broken one other
    // native binary (Turbopack) — Next's built-in image optimizer falls back to a slow WASM
    // path without sharp, which fails intermittently on larger uploads. Serve images as-is
    // instead of risking the same class of failure sharp's native bindings would likely hit.
    unoptimized: true,
    remotePatterns: [
      { protocol: "https", hostname: "placehold.co" },
      { protocol: "https", hostname: "images.unsplash.com" },
    ],
  },
  async headers() {
    return [
      {
        source: "/:path*",
        headers: [
          { key: "X-Frame-Options", value: "SAMEORIGIN" },
          { key: "X-Content-Type-Options", value: "nosniff" },
          { key: "Referrer-Policy", value: "strict-origin-when-cross-origin" },
          { key: "Permissions-Policy", value: "camera=(), microphone=(), geolocation=(self)" },
          { key: "Strict-Transport-Security", value: "max-age=63072000; includeSubDomains" },
          // Belt-and-suspenders alongside robots.ts's disallow: a robots.txt disallow only
          // stops crawling, not indexing of a URL discovered some other way — this header
          // blocks indexing outright while NEXT_PUBLIC_SITE_URL isn't the real main domain.
          ...(isCanonicalDomain ? [] : [{ key: "X-Robots-Tag", value: "noindex, nofollow" }]),
        ],
      },
    ];
  },
};

export default nextConfig;
