/** @type {import('next').NextConfig} */
// 服务端 rewrites 转发目标；浏览器端 Base 由 NEXT_PUBLIC_API_URL 控制（见 src/lib/client.ts）
const API_URL = process.env.API_URL || 'http://localhost:8000';

const nextConfig = {
    output: 'standalone',
    experimental: {
        optimizePackageImports: [
            '@lobehub/icons',
            'lucide-react',
            'framer-motion',
            '@radix-ui/react-dialog',
            '@radix-ui/react-dropdown-menu',
            '@radix-ui/react-select',
            '@radix-ui/react-tabs',
        ],
        // Increase proxy timeout for long-running AI operations
        proxyTimeout: 300000, // 5 minutes
    },
    // Server response timeout
    serverExternalPackages: [],
    images: {
        // 本地开发环境下关闭 Next 图片优化，避免在受限网络环境中请求 OSS 导致 _next/image 500
        unoptimized: process.env.NODE_ENV === 'development',
        dangerouslyAllowSVG: true,
        contentSecurityPolicy: "default-src 'self'; script-src 'none'; sandbox;",
        remotePatterns: [
            {
                protocol: 'https',
                hostname: '**.aliyuncs.com',
            },
            {
                protocol: 'http',
                hostname: '**.aliyuncs.com',
            },
            {
                protocol: 'http',
                hostname: 'localhost',
            },
            {
                protocol: 'https',
                hostname: 'api.dicebear.com',
            }
        ]
    },
    async rewrites() {
        return [
            {
                source: '/api/:path*',
                destination: `${API_URL}/api/:path*`,
            },
            {
                source: '/files/:path*',
                destination: `${API_URL}/files/:path*`,
            },
            {
                source: '/uploads/:path*',
                destination: `${API_URL}/uploads/:path*`,
            },
            {
                source: '/config/:path*',
                destination: `${API_URL}/config/:path*`,
            },
            {
                source: '/projects/:path*',
                destination: `${API_URL}/projects/:path*`,
            },
            {
                source: '/generations/:path*',
                destination: `${API_URL}/generations/:path*`,
            },
            {
                source: '/storage/:path*',
                destination: `${API_URL}/storage/:path*`,
            },
            {
                source: '/chat/:path*',
                destination: `${API_URL}/chat/:path*`,
            },
            {
                source: '/health',
                destination: `${API_URL}/health`,
            }
        ];
    },
};

export default nextConfig;
