import { imageHosts } from './image-hosts.config.js';

/** @type {import('next').NextConfig} */
const nextConfig = {
  productionBrowserSourceMaps: true,
  distDir: process.env.DIST_DIR || '.next',

  typescript: {
      ignoreBuildErrors: true,
    },

  eslint: {
    ignoreDuringBuilds: true,
  },

  images: {
    remotePatterns: imageHosts,
    formats: ['image/avif', 'image/webp'],
    minimumCacheTTL: 86400,
    deviceSizes: [640, 750, 828, 1080, 1200, 1920],
    imageSizes: [16, 32, 48, 64, 96, 128, 256, 384],
    // Critical: serve optimized images INLINE, not as attachments. Default
    // changed to 'attachment' in Next 14 for SVG XSS protection — that
    // broke our banner ads (browsers tried to download instead of render).
    contentDispositionType: 'inline',
  },

  // Performance: compress responses
  compress: true,

  // Performance: power headers for caching static assets
  async headers() {
    return [
      {
        source: '/assets/:path*',
        headers: [
          { key: 'Cache-Control', value: 'public, max-age=31536000, immutable' },
        ],
      },
      {
        source: '/_next/static/:path*',
        headers: [
          { key: 'Cache-Control', value: 'public, max-age=31536000, immutable' },
        ],
      },
      {
        source: '/_next/image/:path*',
        headers: [
          { key: 'Cache-Control', value: 'public, max-age=86400, stale-while-revalidate=604800' },
        ],
      },
      {
        // Homepage and news routes must never be cached at edge — the hero
        // and feed are DB-driven via force-dynamic, but Next.js sets a
        // long s-maxage on prerendered HTML which CF then caches for a year.
        source: '/',
        headers: [
          { key: 'Cache-Control', value: 'no-store, must-revalidate' },
          { key: 'CDN-Cache-Control', value: 'no-store' },
        ],
      },
      {
        source: '/news/:path*',
        headers: [
          { key: 'Cache-Control', value: 'no-store, must-revalidate' },
          { key: 'CDN-Cache-Control', value: 'no-store' },
        ],
      },
    ];
  },

  async redirects() {
    return [
      // Homepage canonical: '/' is the real route now (src/app/page.tsx
      // re-exports from src/app/home-page/page.tsx). The legacy
      // /home-page URL gets a 301 permanent redirect to '/' so any
      // crawler entry for /home-page consolidates link equity onto
      // the canonical root.
      {
        source: '/home-page',
        destination: '/',
        permanent: true,
      },
      // Advertise page is replaced by the canonical media kit on RMP.
      {
        source: '/about/advertise',
        destination: 'https://relevantmediaproperties.com/broadcast-beat---media-kit',
        permanent: true,
      },
      // WordPress admin redirects for avbeat.com (301 permanent)
      {
        source: '/wp-admin',
        destination: '/admin',
        permanent: true,
      },
      {
        source: '/wp-admin/',
        destination: '/admin',
        permanent: true,
      },
      {
        source: '/wp-admin/post-new.php',
        destination: '/contributor',
        permanent: true,
      },
      {
        source: '/wp-admin/edit.php',
        destination: '/admin/articles',
        permanent: true,
      },
      {
        source: '/wp-admin/profile.php',
        destination: '/account',
        permanent: true,
      },
      {
        source: '/wp-login.php',
        destination: '/client-login',
        permanent: true,
      },
      {
        source: '/wp-login.php/:path*',
        destination: '/client-login',
        permanent: true,
      },
      // Catch-all for any other wp-admin sub-paths
      {
        source: '/wp-admin/:path*',
        destination: '/admin',
        permanent: true,
      },
      // AV Beat dashboard redirect (avbeat.com/wp-admin/ → unified dashboard)
      // These are handled by the same Next.js app when serving avbeat.com
      {
        source: '/avbeat/wp-admin',
        destination: '/admin',
        permanent: true,
      },
      {
        source: '/avbeat/wp-admin/',
        destination: '/admin',
        permanent: true,
      },
      {
        source: '/avbeat/wp-login.php',
        destination: '/client-login',
        permanent: true,
      },
      {
        source: '/avbeat/wp-login.php/:path*',
        destination: '/client-login',
        permanent: true,
      },
      // /login is a real BB-direct sign-in page (used by admin backend).
      // Do NOT redirect it to /client-login anymore.
    ];
  }
};

export default nextConfig;
