import path from "path";
import { fileURLToPath } from "url";
import { withContentlayer } from "next-contentlayer";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const nextConfig = {
  // ===============================
  // 🚀 生产优化 —— 最关键的三行
  // ===============================
  compress: true,         // Gzip 压缩输出（确保小体积网络传输）
  basePath: "/insight",    // 👈 所有的路由都会带有 /insight 前缀
  trailingSlash: true,    // 👈 配合 Caddy 强制使用末尾斜杠，防止 308 重定向死循环

  // 配置允许的外部图片域名
  images: {
    remotePatterns: [
      {
        protocol: 'https',
        hostname: 'dl.svc.plus',
      },
      {
        protocol: 'https',
        hostname: 'www.svc.plus',
      },
      {
        protocol: 'https',
        hostname: 'images.unsplash.com',
      },
    ],
  },

  webpack: (config) => {
    // 添加 YAML 文件支持
    config.module.rules.push({
      test: /\.ya?ml$/i,
      type: 'asset/source',
    });

    // 显式 alias，保证 Turbopack 也能解析
    config.resolve.alias = {
      ...(config.resolve.alias ?? {}),
      "@components": path.join(__dirname, "src", "components"),
      "@i18n": path.join(__dirname, "src", "i18n"),
      "@lib": path.join(__dirname, "src", "lib"),
      "@types": path.join(__dirname, "types"),
      "@server": path.join(__dirname, "src", "server"),
      "@modules": path.join(__dirname, "src", "modules"),
      "@extensions": path.join(__dirname, "src", "modules", "extensions"),
      "@theme": path.join(__dirname, "src", "components", "theme"),
      "@templates": path.join(__dirname, "src", "modules", "templates"),
      "@src": path.join(__dirname, "src"),
      "@": path.join(__dirname, "src"),
    };

    // 添加模块搜索路径
    config.resolve.modules = [
      ...(config.resolve.modules || []),
      __dirname,
      path.join(__dirname, "src"),
    ];

    return config;
  },
  async headers() {
    return [
      {
        source: "/api/:path*",
        headers: [
          { key: "Access-Control-Allow-Credentials", value: "true" },
          { key: "Access-Control-Allow-Origin", value: process.env.CORS_ALLOWED_ORIGINS || "https://console.svc.plus,http://localhost:3000" },
          { key: "Access-Control-Allow-Methods", value: "GET,POST,PUT,PATCH,DELETE,OPTIONS" },
          { key: "Access-Control-Allow-Headers", value: "Content-Type, Authorization, X-Requested-With, X-Account-Session" },
        ],
      },
    ];
  },

  reactStrictMode: true,
  typedRoutes: false,
  turbopack: {
    root: path.resolve(__dirname),
  },
};

export async function redirects() {
  return [
    {
      source: '/XStream',
      destination: '/xstream',
      permanent: true,
    },
    {
      source: '/Xstream',
      destination: '/xstream',
      permanent: true,
    },
    {
      source: '/XScopeHub',
      destination: '/xscopehub',
      permanent: true,
    },
    {
      source: '/XCloudFlow',
      destination: '/xcloudflow',
      permanent: true,
    },
  ];
}

export async function rewrites() {
  return [
    {
      source: '/editor',
      destination: 'http://localhost:4000',
    },
    {
      source: '/editor/:path*',
      destination: 'http://localhost:4000/:path*',
    },
  ];
}

export default withContentlayer(nextConfig);
