/**
 * Sentry Server Configuration
 *
 * Configures Sentry for server-side (Node.js) error tracking in production.
 * Loaded via instrumentation.ts when NEXT_RUNTIME === 'nodejs'.
 *
 * To enable: set SENTRY_DSN in .env.local
 * Without the DSN, this file is a no-op.
 */

import * as Sentry from "@sentry/nextjs";

const SENTRY_DSN = process.env.SENTRY_DSN || process.env.NEXT_PUBLIC_SENTRY_DSN;

if (SENTRY_DSN) {
  Sentry.init({
    dsn: SENTRY_DSN,

    // Only enable in production
    enabled: process.env.NODE_ENV === "production",

    // Environment tag
    environment: process.env.NODE_ENV || "development",

    // Performance Monitoring - sample 10% of transactions
    tracesSampleRate: 0.1,

    // Filter out sensitive data before sending
    beforeSend(event) {
      if (event.request) {
        delete event.request.cookies;
        if (event.request.headers) {
          const sensitiveHeaders = [
            "authorization",
            "cookie",
            "x-csrf-token",
            "x-api-key",
          ];
          for (const header of sensitiveHeaders) {
            delete event.request.headers[header];
          }
        }
      }
      return event;
    },

    // Ignore common non-actionable errors
    ignoreErrors: [
      "ECONNRESET",
      "ECONNREFUSED",
      "ETIMEDOUT",
      "UND_ERR_CONNECT_TIMEOUT",
    ],
  });
}

export {};
