import type { NextConfig } from "next";
import path from "node:path";
import { fileURLToPath } from "node:url";

const projectRoot = path.dirname(fileURLToPath(import.meta.url));

const nextConfig: NextConfig = {
  poweredByHeader: false,
  async headers() {
    return [
      {
        source: '/:path*',
        headers: [
          { key: 'X-Content-Type-Options', value: 'nosniff' },
          { key: 'Referrer-Policy', value: 'strict-origin-when-cross-origin' },
          { key: 'Content-Security-Policy', value: "object-src 'none'; base-uri 'self'; frame-ancestors 'self'" },
          ...(process.env.NODE_ENV === 'production' ? [{ key: 'Strict-Transport-Security', value: 'max-age=31536000' }] : []),
        ],
      },
      {
        source: '/api/auth/:path*',
        headers: [{ key: 'Cache-Control', value: 'private, no-store, max-age=0' }],
      },
      {
        source: '/api/dashboard/auth/:path*',
        headers: [{ key: 'Cache-Control', value: 'private, no-store, max-age=0' }],
      },
    ];
  },
  async redirects() {
    return [
      {
        source: "/communication-across-cultures/resources/:path*",
        destination: "/cvcac/resources/:path*",
        permanent: true,
      },
      {
        source: "/voice-and-accent-learning-hub/:path*",
        destination: "/cvat/resources/:path*",
        permanent: true,
      },
      {
        source: "/blog/teacher-training/a-complete-guide-to-tesol-in-india-2024-costs-courses-career-paths",
        destination: "/blog/teacher-training/tesol-certification-in-india-complete-guide-2026-costs-courses-and-careers",
        permanent: true,
      },
      {
        source: "/blog/ielts/how-can-i-become-ielts-certified-trainer",
        destination: "/blog/ielts/how-can-i-become-an-ielts-certified-trainer",
        permanent: true,
      },
      {
        source: "/algebra-one",
        destination: "/online-algebra-1-tutoring",
        permanent: true,
      },
      {
        source: "/writing-analyser/:path*",
        destination: "/tools/writing-analyser/:path*",
        permanent: true,
      },
      {
        source: "/tools/writing-analyser/task/:path*",
        destination: "/tools-dashboard/writing-analyser/task/:path*",
        permanent: true,
      },
      {
        source: "/tools/writing-analyser/write-essay/:path*",
        destination: "/tools-dashboard/writing-analyser/write-essay/:path*",
        permanent: true,
      },
      {
        source: "/tools/writing-analyser/result/:path*",
        destination: "/tools-dashboard/writing-analyser/result/:path*",
        permanent: true,
      },
      {
        source: "/tools/writing-analyser/glossary/:path*",
        destination: "/tools-dashboard/writing-analyser/glossary/:path*",
        permanent: true,
      },
      {
        source: "/tools/dashboard",
        destination: "/speech-dashboard",
        permanent: true,
      },
      {
        source: "/tools/active-plan",
        destination: "/speech-dashboard/active-plan",
        permanent: true,
      },
      {
        source: "/tools/attempts-exhausted",
        destination: "/speech-dashboard/attempts-exhausted",
        permanent: true,
      },
      {
        source: "/tools/cancel-subscription",
        destination: "/speech-dashboard/cancel-subscription",
        permanent: true,
      },
      {
        source: "/tools/landing",
        destination: "/tools/speech-analyser",
        permanent: true,
      },
      {
        source: "/speech-dashboard/landing",
        destination: "/tools/speech-analyser",
        permanent: true,
      },
      {
        source: "/tools/practice-with-ella",
        destination: "/tools/practice-accent-with-ella",
        permanent: true,
      },
      {
        source: "/tools/practice-with-emma",
        destination: "/tools/practice-ielts-with-emma",
        permanent: true,
      },
      {
        source: "/tools/practice-with-eva",
        destination: "/tools/practice-english-with-eva",
        permanent: true,
      },
      {
        source: "/tools/practice-with-ellie",
        destination: "/tools/practice-tesol-with-ellie",
        permanent: true,
      },
      {
        source: "/tools/no-active-plan",
        destination: "/speech-dashboard/no-active-plan",
        permanent: true,
      },
      {
        source: "/tools/past-results",
        destination: "/speech-dashboard/past-results",
        permanent: true,
      },
      {
        source: "/tools/payment",
        destination: "/speech-dashboard/payment",
        permanent: true,
      },
      {
        source: "/tools/payment-history",
        destination: "/speech-dashboard/payment-history",
        permanent: true,
      },
      {
        source: "/tools/payment-success",
        destination: "/speech-dashboard/payment-success",
        permanent: true,
      },
      {
        source: "/tools/reset-password",
        destination: "/speech-dashboard/reset-password",
        permanent: true,
      },
      {
        source: "/tools/result",
        destination: "/speech-dashboard/result",
        permanent: true,
      },
      {
        source: "/tools/score-details",
        destination: "/speech-dashboard/score-details",
        permanent: true,
      },
      {
        source: "/tools/signin",
        destination: "/speech-dashboard/signin",
        permanent: true,
      },
      {
        source: "/tools/speech-analyser-result/:path*",
        destination: "/speech-dashboard/speech-analyser-result/:path*",
        permanent: true,
      },
      {
        source: "/tools/speech-analyser-result-details/:path*",
        destination: "/speech-dashboard/speech-analyser-result-details/:path*",
        permanent: true,
      },
    ];
  },
  deploymentId: process.env.NEXT_DEPLOYMENT_ID || process.env.DEPLOYMENT_VERSION,
  // The speech routes execute this Python analyzer at runtime. Because the
  // path is resolved dynamically, Next.js cannot discover it automatically
  // while producing the server trace for a production deployment.
  outputFileTracingIncludes: {
    "/api/speech/*": [
      "./scripts/speech_pitch_analysis.py",
      "./scripts/requirements-speech.txt",
      "./data/GeoLite2-Country.mmdb",
      "./data/GeoLite2-Country_*/GeoLite2-Country.mmdb",
    ],
    "/api/geo": [
      "./data/GeoLite2-Country.mmdb",
      "./data/GeoLite2-Country_*/GeoLite2-Country.mmdb",
    ],
    "/speech-dashboard/landing": [
      "./data/GeoLite2-Country.mmdb",
      "./data/GeoLite2-Country_*/GeoLite2-Country.mmdb",
    ],
    "/speech-dashboard/speech-analyser": [
      "./data/GeoLite2-Country.mmdb",
      "./data/GeoLite2-Country_*/GeoLite2-Country.mmdb",
    ],
    "/tools/speech-analyser": [
      "./data/GeoLite2-Country.mmdb",
      "./data/GeoLite2-Country_*/GeoLite2-Country.mmdb",
    ],
  },
  webpack(config, { dev }) {
    if (dev && config.cache && typeof config.cache === "object") {
      config.cache = {
        ...config.cache,
        compression: false,
      };
    }

    return config;
  },
  images: {
    remotePatterns: [
      {
        protocol: "https",
        hostname: "img.youtube.com",
      },
      {
        protocol: "https",
        hostname: "media.base44.com",
      },
      {
        protocol: "https",
        hostname: "cambridgeveritas.com",
      },
    ],
  },
  turbopack: {
    root: projectRoot,
  },
  typescript: {
    ignoreBuildErrors: true,
  },
};

export default nextConfig;
