import type { NextConfig } from "next";
import createNextIntlPlugin from 'next-intl/plugin';
import path from 'path';

const withNextIntl = createNextIntlPlugin('./i18n/request.ts');

const basePath = process.env.NEXT_PUBLIC_BASE_PATH || '';
const apiBaseUrl = process.env.NEXT_PUBLIC_API_BASE_URL || '';

// connect-src: explicit API URL only — no broad http:/https: scheme
// Google Fonts are self-hosted by Next.js at build — no external font-src needed
// Stripe uses window.location redirects (JS navigation) — not restricted by connect-src/form-action
// All forms are React-controlled (no HTML form action attr) — form-action 'self' is safe
// LinkedIn Insight Tag: script from snap.licdn.com, conversion pixel to px.ads.linkedin.com
const connectSrc = apiBaseUrl
  ? `connect-src 'self' ${apiBaseUrl} https://www.googletagmanager.com https://www.google-analytics.com https://analytics.google.com https://px.ads.linkedin.com`
  : `connect-src 'self' https://www.googletagmanager.com https://www.google-analytics.com https://analytics.google.com https://px.ads.linkedin.com`;

const securityHeaders = [
  { key: 'X-Frame-Options', value: 'DENY' },
  { key: 'X-Content-Type-Options', value: 'nosniff' },
  { key: 'Referrer-Policy', value: 'strict-origin-when-cross-origin' },
  { key: 'Permissions-Policy', value: 'camera=(), microphone=(), geolocation=()' },
  { key: 'Strict-Transport-Security', value: 'max-age=63072000; includeSubDomains; preload' },
  // Isolates browsing context — window.open for Stripe portal still works (no opener access needed)
  { key: 'Cross-Origin-Opener-Policy', value: 'same-origin' },
  {
    key: 'Content-Security-Policy',
    value: [
      "default-src 'self'",
      // unsafe-inline/unsafe-eval required by Next.js App Router for hydration
      // Removing these requires nonce-based CSP configured per-request
      "script-src 'self' 'unsafe-inline' 'unsafe-eval' https://www.googletagmanager.com https://www.google-analytics.com https://snap.licdn.com",
      "style-src 'self' 'unsafe-inline'",
      "img-src 'self' data: https://images.unsplash.com https://*.cloudfront.net https://flagcdn.com https://www.googletagmanager.com https://www.google-analytics.com https://px.ads.linkedin.com",
      connectSrc,
      "font-src 'self'",
      "object-src 'none'",
      "base-uri 'self'",
      "form-action 'self'",
      "frame-ancestors 'none'",
    ].join('; '),
  },
];

const nextConfig: NextConfig = {

  basePath: basePath,

  assetPrefix: basePath,

  // output: 'standalone',

  trailingSlash: false,

  // Fix for multiple lockfiles warning
  outputFileTracingRoot: path.join(__dirname, './'),

  async headers() {
    return [{ source: '/(.*)', headers: securityHeaders }];
  },

  images: {
    unoptimized: !!basePath,
    remotePatterns: [
      {
        protocol: 'https',
        hostname: 'images.unsplash.com',
      },
      {
        protocol: 'https',
        hostname: 'dbuhhw38l3ir7.cloudfront.net',
      },
    ],
  },
};

export default withNextIntl(nextConfig);
