import type { NextConfig } from "next";

// Same-origin 'self' covers scripts, styles, and fonts from /_next/static,
// and images from /media, /_next/image, and /_next/static.
// Inline scripts and styles stay allowed: Next inlines the page payload and
// JSON-LD, and the pages use style attributes. Remote script hosts stay blocked.
const contentSecurityPolicy = [
  "default-src 'self'",
  "script-src 'self' 'unsafe-inline'",
  "style-src 'self' 'unsafe-inline'",
  "img-src 'self'",
  "font-src 'self'",
  "connect-src 'self'",
  "object-src 'none'",
  "base-uri 'self'",
  "form-action 'self'",
  "frame-ancestors 'self'",
  "frame-src 'none'",
].join("; ");

const nextConfig: NextConfig = {
  reactStrictMode: true,
  poweredByHeader: false,
  images: {
    formats: ["image/webp"],
    deviceSizes: [640, 750, 828, 1080, 1200, 1920, 2048, 2560],
    imageSizes: [64, 128, 200, 320, 480],
    qualities: [95],
    minimumCacheTTL: 60 * 60 * 24 * 30,
  },
  async headers() {
    return [
      {
        source: "/:path*",
        headers: [
          { key: "X-Content-Type-Options", value: "nosniff" },
          { key: "Referrer-Policy", value: "strict-origin-when-cross-origin" },
          { key: "X-Frame-Options", value: "SAMEORIGIN" },
          { key: "Permissions-Policy", value: "camera=(), microphone=(), geolocation=()" },
          { key: "Content-Security-Policy", value: contentSecurityPolicy },
        ],
      },
    ];
  },
  async redirects() {
    return ["portfolio2", "portfolio3", "portfolio4", "portfolio5"].map((path) => ({
      source: `/${path}`,
      destination: "/portfolio",
      permanent: true,
    }));
  },
};

export default nextConfig;
