/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  poweredByHeader: false,

  // Allow images from Google Maps (in case we ever render Static Maps)
  images: {
    remotePatterns: [
      { protocol: 'https', hostname: 'maps.googleapis.com' },
      { protocol: 'https', hostname: 'maps.gstatic.com' },
    ],
  },

  // For dev: proxy API calls. In prod, Nginx handles /api/.
  async rewrites() {
    if (process.env.NODE_ENV !== 'development') return [];
    const apiBase = process.env.NEXT_PUBLIC_API_BASE_URL ?? 'http://localhost:4001';
    return [
      { source: '/api/:path*', destination: `${apiBase}/api/:path*` },
      { source: '/health', destination: `${apiBase}/health` },
    ];
  },
};

// next-pwa wrapper - disabled in dev, enabled in prod build
const withPWA = require('next-pwa')({
  dest: 'public',
  disable: process.env.NODE_ENV === 'development',
  register: true,
  skipWaiting: true,
  // Don't cache API responses - always fresh
  runtimeCaching: [
    {
      urlPattern: /^\/api\//,
      handler: 'NetworkOnly',
    },
    {
      urlPattern: /\.(?:png|jpg|jpeg|svg|webp|woff2?)$/,
      handler: 'CacheFirst',
      options: {
        cacheName: 'static-assets',
        expiration: { maxEntries: 100, maxAgeSeconds: 30 * 24 * 60 * 60 },
      },
    },
  ],
});

module.exports = withPWA(nextConfig);
