import { withSentryConfig } from '@sentry/nextjs';

/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  // SaaS module loaded via iframe — keep output static-ish but App Router still works
  // with default settings. We disable SSR-by-default by using "use client" everywhere
  // (see src/app layout/page).
  output: 'standalone',
  typedRoutes: true,
  async redirects() {
    // 根路径跳召回工作台 —— 放在路由层(而非 HomePage 组件里 redirect()):
    // server 组件同步 redirect() 会让 Next dev 给未开始计时的 'HomePage' 打耗时 →
    // "Performance measure cannot have a negative time stamp" 运行时报错。路由层跳转
    // 根本不渲染该组件,从源头消除。
    return [{ source: '/', destination: '/plans', permanent: false }];
  },
  async headers() {
    // frame-ancestors is dynamically injected per-tenant in real deploys; here we
    // keep a permissive default for local dev. Production should set this from
    // platform config.
    return [
      {
        source: '/:path*',
        headers: [
          { key: 'X-Frame-Options', value: 'ALLOWALL' },
        ],
      },
    ];
  },
};

// Sentry 包装:注入客户端/服务端埋点 + (可选)构建期上传 source map 到自部署 Sentry。
// 自部署实例:sentryUrl 指 am.jwsmed.com;org/project 可用 env 覆盖。
// source map 上传仅在 SENTRY_AUTH_TOKEN 存在时进行(构建期 secret,不入库);无 token 构建照样成功。
export default withSentryConfig(nextConfig, {
  org: process.env.SENTRY_ORG ?? 'sentry',
  project: process.env.SENTRY_PROJECT ?? 'pac-web',
  sentryUrl: process.env.SENTRY_URL ?? 'https://am.jwsmed.com/',
  authToken: process.env.SENTRY_AUTH_TOKEN,
  silent: !process.env.CI,
  telemetry: false, // 自部署:不向 sentry.io 发遥测
  sourcemaps: { disable: !process.env.SENTRY_AUTH_TOKEN },
});
