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

    // Optimisations pour la production
    compress: true,
    poweredByHeader: false,
    generateEtags: false,

    // Désactiver la génération statique pour éviter les erreurs useSearchParams
    skipTrailingSlashRedirect: true,

    // Configuration des images optimisées
    images: {
        domains: ['btpxpress.lions.dev', 'api.lions.dev', 'security.lions.dev'],
        formats: ['image/webp', 'image/avif'],
        minimumCacheTTL: 60,
        dangerouslyAllowSVG: true,
        contentSecurityPolicy: "default-src 'self'; script-src 'none'; sandbox;",
    },

    // Optimisations de performance
    experimental: {
        optimizeCss: true,
        optimizePackageImports: ['primereact', 'primeicons'],
    },

    // Configuration Turbopack (stable)
    turbopack: {
        rules: {
            '*.svg': {
                loaders: ['@svgr/webpack'],
                as: '*.js',
            },
        },
    },

    // Configuration du bundler simplifiée
    webpack: (config, { dev }) => {
        // Alias pour optimiser les imports
        config.resolve.alias = {
            ...config.resolve.alias,
            '@': require('path').resolve(__dirname),
            '@components': require('path').resolve(__dirname, 'components'),
            '@services': require('path').resolve(__dirname, 'services'),
            '@types': require('path').resolve(__dirname, 'types'),
            '@utils': require('path').resolve(__dirname, 'utils'),
        };

        return config;
    },

    // Headers de sécurité
    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'
                    }
                ]
            }
        ];
    },

    async redirects() {
        return [
            {
                source: '/apps/mail',
                destination: '/apps/mail/inbox',
                permanent: true
            }
        ];
    },

    // Configuration des variables d'environnement
    env: {
        NEXT_PUBLIC_API_URL: process.env.NEXT_PUBLIC_API_URL,
        NEXT_PUBLIC_KEYCLOAK_URL: process.env.NEXT_PUBLIC_KEYCLOAK_URL,
        NEXT_PUBLIC_KEYCLOAK_REALM: process.env.NEXT_PUBLIC_KEYCLOAK_REALM,
        NEXT_PUBLIC_KEYCLOAK_CLIENT_ID: process.env.NEXT_PUBLIC_KEYCLOAK_CLIENT_ID,
        NEXT_PUBLIC_APP_ENV: process.env.NEXT_PUBLIC_APP_ENV,
    }
};

module.exports = nextConfig;
