import path from 'node:path';
import { fileURLToPath } from 'node:url';

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

/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  // Emit a self-contained server bundle at .next/standalone/server.js so the
  // `madarx-os` CLI (bin/madarx-os.mjs) can boot the web tier from a global npm
  // install with no build step on the user's machine. The publish workflow
  // builds this and ships it in the package tarball.
  output: 'standalone',
  outputFileTracingRoot: __dirname,
  // Dev gets its own build dir so `next dev` never shares `.next` with the
  // production build/standalone output the `madarx` CLI serves. Mixing the two
  // wedges dev boot (socket binds, init promise never resolves). `next dev`
  // forces NODE_ENV=development; build/start force production.
  distDir: process.env.NODE_ENV === 'development' ? '.next-dev' : '.next',
  serverExternalPackages: ['node:sqlite', '@anthropic-ai/sdk', 'exceljs', 'playwright', 'playwright-core'],
  typedRoutes: false,
  webpack: (config, { dev }) => {
    if (dev) {
      config.watchOptions = {
        ...(config.watchOptions || {}),
        ignored: [
          '**/node_modules/**',
          '**/.next/**',
          '**/.next-dev/**',
          '**/.git/**',
          '**/data/**',
          '**/workspace/**',
          '**/.hermes/**',
          '**/.herenow/**',
          '**/.gstack/**',
          '**/coverage/**',
          '**/.tmp-tests/**'
        ]
      };
    }
    return config;
  }
};

export default nextConfig;
