/** @type {import('next').NextConfig} */
const nextConfig = {
  // HAWKHOST cPanel Node.js Selector - ENABLED
  output: 'standalone',
  distDir: '.next',

  images: {
    domains: [
      'localhost',
      'bolafest.id',
      'res.cloudinary.com',
      'media.api-sports.io',
      'crests.football-data.org',
    ],
    remotePatterns: [
      {
        protocol: 'https',
        hostname: '**',
      },
    ],
    unoptimized: process.env.NODE_ENV === 'production' && process.env.DISABLE_IMAGE_OPTIMIZATION === 'true',
  },

  // Compress responses
  compress: true,

  // Power by header
  poweredByHeader: false,

  // Trailing slash (konsisten untuk semua URL)
  trailingSlash: false,

  // Environment variables yang di-expose ke client
  env: {
    NEXT_PUBLIC_APP_URL: process.env.NEXT_PUBLIC_APP_URL || 'https://bolafest.id',
    NEXT_PUBLIC_APP_NAME: 'AI Prediksi & Skor',
  },

  experimental: {
    serverComponentsExternalPackages: ['@prisma/client'],
  },

  // Headers keamanan
  async headers() {
    return [
      {
        source: '/(.*)',
        headers: [
          { key: 'X-Frame-Options', value: 'DENY' },
          { key: 'X-Content-Type-Options', value: 'nosniff' },
          { key: 'Referrer-Policy', value: 'strict-origin-when-cross-origin' },
          { key: 'X-XSS-Protection', value: '1; mode=block' },
        ],
      },
    ];
  },

 // Webpack config untuk production
  // PENTING: devtool false diperlukan untuk CloudLinux (memblokir eval())
  webpack: (config, { isServer }) => {
    // Disable eval() - required for CloudLinux/cPanel hosting
    config.devtool = false;

    // JANGAN bundle Prisma Client ke server chunks (WAJIB untuk deploy Linux!)
    if (isServer) {
      config.externals = config.externals || [];
      if (!Array.isArray(config.externals)) {
        config.externals = [config.externals];
      }
      config.externals.push('@prisma/client');
    }

    if (!isServer) {
      config.resolve.fallback = {
        ...config.resolve.fallback,
        fs: false,
        net: false,
        tls: false,
      };
    }
    return config;
  },
}

module.exports = nextConfig
