import type { NextConfig } from 'next';
import fs from 'fs';
import path from 'path';
import dotenv from 'dotenv';

// 加载 .env 到 process.env（内部会用 dotenv.populate 注入）
dotenv.config({ path: '.env', override: true });

let projectId = '';
// 尝试从根目录 .project 读取 project_id / user_id 注入到环境变量
try {
  const projectPath = path.join(process.cwd(), '.project');
  const raw = fs.readFileSync(projectPath, 'utf8');
  const data = JSON.parse(raw) as {
    project_id?: string;
  };

  // 直接复用 dotenv.populate，把 .project 内容也注入到 process.env
  projectId = data?.project_id ?? '';
} catch {
  // 没有 .project 或解析失败时静默忽略，保持兼容
}

const nextConfig: NextConfig = {
  reactStrictMode: false,
  turbopack: {},
  typescript: {
    ignoreBuildErrors: true,
  },
  env: {
    PROJECT_ID: projectId,
  },
  serverExternalPackages: [
    'discord.js',
    '@discordjs/ws',
    '@discordjs/rest',
    'dingtalk-stream',
    'ws',
  ],
};

export default nextConfig;

