import type { NextConfig } from "next";
const redirects = require("./redirects");

const assetPrefix = process.env.NEXT_ASSET_PREFIX;

const nextConfig: NextConfig = {
  assetPrefix: assetPrefix || undefined,
  poweredByHeader: false,
  experimental: {
    inlineCss: process.env.NODE_ENV === "production",
  },
  images: {
    unoptimized: true,
    remotePatterns: [
      {
        protocol: "https",
        hostname: "teapot.ourdevspace.co.uk",
        pathname: "/wp/**",
      },
      {
        protocol: "https",
        hostname: "teapotwp.teapotdev.co.uk",
        pathname: "/wp-content/uploads/**",
      },
    ],
  },
  async redirects() {
    return redirects;
  },
  async headers() {
    return [
      {
        source: "/:path*\\.mp4",
        headers: [
          {
            key: "Cache-Control",
            value: "public, max-age=31536000, immutable",
          },
        ],
      },
      {
        source: "/:path*\\.webp",
        headers: [
          {
            key: "Cache-Control",
            value: "public, max-age=31536000, immutable",
          },
        ],
      },
      {
        source: "/:path*\\.woff2",
        headers: [
          {
            key: "Cache-Control",
            value: "public, max-age=31536000, immutable",
          },
        ],
      },
      {
        source: "/(.*)",
        headers: [
          {
            key: "Content-Security-Policy",
            value:
              "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://www.googletagmanager.com https://www.google.com https://www.google.co.uk https://www.gstatic.com https://googleads.g.doubleclick.net https://www.googleadservices.com https://pagead2.googlesyndication.com https://teapotcreative.us8.list-manage.com https://static.hotjar.com https://script.hotjar.com https://www.clarity.ms https://scripts.clarity.ms; style-src 'self' 'unsafe-inline'; img-src 'self' data: https://teapot.ourdevspace.co.uk https://teapotwp.teapotdev.co.uk https://www.google.com https://www.google.co.uk https://googleads.g.doubleclick.net https://*.google-analytics.com https://*.analytics.google.com https://www.googletagmanager.com https://*.g.doubleclick.net https://www.gstatic.com https://*.hotjar.com https://*.clarity.ms https://c.bing.com https://c.clarity.ms; media-src 'self' https://teapot.ourdevspace.co.uk https://teapotwp.teapotdev.co.uk; font-src 'self' data:; connect-src 'self' https://www.google.com https://www.google.co.uk https://googleads.g.doubleclick.net https://ad.doubleclick.net https://www.googletagmanager.com https://*.google-analytics.com https://*.analytics.google.com https://*.g.doubleclick.net https://pagead2.googlesyndication.com https://www.googleadservices.com https://*.hotjar.com https://*.hotjar.io wss://*.hotjar.com https://*.clarity.ms https://c.bing.com; frame-src 'self' https://www.google.com https://www.google.co.uk https://googleads.g.doubleclick.net https://www.googletagmanager.com https://vars.hotjar.com; object-src 'none'; base-uri 'self'; frame-ancestors 'self';",
          },
          {
            key: "X-Frame-Options",
            value: "SAMEORIGIN",
          },
        ],
      },
      {
        source: "/fonts/(.*)",
        headers: [
          {
            key: "Access-Control-Allow-Origin",
            value: "*",
          },
        ],
      },
    ];
  },
  async rewrites() {
    return {
      beforeFiles: [
        {
          source: "/:path((?!prestashop(?:/|$)|_next|api|image|fonts).*)",
          has: [{ type: "host", value: "prestashop.teapot.agency" }],
          destination: "/prestashop/:path",
        },
        {
          source: "/:path((?!prestashop(?:/|$)|_next|api|image|fonts).*)",
          has: [{ type: "host", value: "prestashop.localhost" }],
          destination: "/prestashop/:path",
        },
        {
          source: "/:path((?!prestashop(?:/|$)|_next|api|image|fonts).*)",
          has: [{ type: "host", value: "prestashop.localhost:3000" }],
          destination: "/prestashop/:path",
        },
      ],
    };
  },
  webpack(config) {
    config.module.rules.push({
      test: /\.svg$/,
      use: [
        {
          loader: "@svgr/webpack",
          options: {
            icon: true,
          },
        },
      ],
    });

    return config;
  },
};

export default nextConfig;
