/**
 * Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation.
 * This is especially useful for Docker builds.
 */

import { env } from "node:process";

/** @type {import("next").NextConfig} */
const nextConfig = {
  output: env.NODE_ENV === "production" ? "standalone" : undefined, // This is for docker builds
  reactStrictMode: true,
  i18n: {
    locales: ["it", "en"],
    defaultLocale: "it",
  },
  images: {
    remotePatterns: [
      {
        protocol: "https",
        hostname: "*.googleusercontent.com",
        port: "",
        pathname: "**",
      },
      {
        hostname: env.BACKENDSERVER_URL || "localhost:1323",
      },
    ],
    minimumCacheTTL: 86400, // 1 day in seconds
  },

  typescript: {
    ignoreBuildErrors: true,
  },
  eslint: {
    ignoreDuringBuilds: true,
  },
  transpilePackages: ["jotai-devtools"],
  devIndicators: false,
  rewrites: async () => {
    return [
      {
        source: "/go-api/images/get/:slug*",
        destination: env.BACKENDSERVER_URL + "/images/get/:slug*",
      },
      {
        source: "/go-api/storage/get/:slug*",
        destination: env.BACKENDSERVER_URL + "/storage/get/:slug*",
        has: [
          { type: "cookie", key: "access_token" },
          {
            type: "query",
            key: "token",
            value:
              "(?<token>^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$)",
          },
        ],
      },
      {
        source: "/go-api/storage/upload:slug*",
        destination: env.BACKENDSERVER_URL + "/storage/upload:slug*",
        has: [
          { type: "cookie", key: "access_token" },
          {
            type: "query",
            key: "token",
            value:
              "(?<token>^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$)",
          },
        ],
      },
    ];
  },
  headers: async () => {
    return [
      {
        source: "/:path*",
        headers: [
          {
            key: "X-Frame-Options",
            value: "",
          },
        ],
      },
    ];
  },
};

export default nextConfig;
