import type { NextConfig } from 'next';

function lanDevOrigins() {
  try {
    const host = new URL(process.env.APP_URL ?? 'http://localhost:3000').hostname;
    if (host && host !== 'localhost' && host !== '127.0.0.1') return [host];
  } catch {
    /* ignore invalid APP_URL during config load */
  }
  return [];
}

const config: NextConfig = {
  // Allow phones/devices on the LAN IP to load /_next assets in development.
  allowedDevOrigins: lanDevOrigins(),
  // Hide the Next.js “N” route indicator in development.
  devIndicators: false,
  poweredByHeader: false,
  images: { qualities: [75, 85] },
  async headers() {
    return [
      { source: '/:path*', headers: [
        { key: 'X-Content-Type-Options', value: 'nosniff' },
        { key: 'Referrer-Policy', value: 'strict-origin-when-cross-origin' },
        { key: 'Permissions-Policy', value: 'camera=(), microphone=(), geolocation=()' },
        { key: 'X-Frame-Options', value: 'DENY' },
        ...(process.env.NODE_ENV === 'production' ? [{ key: 'Strict-Transport-Security', value: 'max-age=31536000' }] : []),
      ] },
      { source: '/sw.js', headers: [{ key: 'Cache-Control', value: 'no-cache, no-store, must-revalidate' }, { key: 'Service-Worker-Allowed', value: '/' }] },
    ];
  },
};
export default config;
